"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/rouge_ext.rb" (1 Jun 2019, 1486 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 require 'rouge' unless defined? Rouge.version
    3 
    4 module Asciidoctor; module RougeExt; module Formatters
    5   class HTMLTable < ::Rouge::Formatter
    6     def initialize delegate, opts
    7       @delegate = delegate
    8       @start_line = opts[:start_line] || 1
    9     end
   10 
   11     def stream tokens
   12       formatted_code = @delegate.format tokens
   13       formatted_code += LF unless formatted_code.end_with? LF, HangingEndSpanTagCs
   14       last_lineno = (first_lineno = @start_line) + (formatted_code.count LF) - 1 # assume number of newlines is constant
   15       lineno_format = %(%#{(::Math.log10 last_lineno).floor + 1}i)
   16       formatted_linenos = ((first_lineno..last_lineno).map {|lineno| sprintf lineno_format, lineno } << '').join LF
   17       yield %(<table class="linenotable"><tbody><tr><td class="linenos gl"><pre class="lineno">#{formatted_linenos}</pre></td><td class="code"><pre>#{formatted_code}</pre></td></tr></tbody></table>)
   18     end
   19   end
   20 
   21   class HTMLLineHighlighter < ::Rouge::Formatter
   22     def initialize delegate, opts
   23       @delegate = delegate
   24       @lines = opts[:lines] || []
   25     end
   26 
   27     def stream tokens
   28       lineno = 0
   29       token_lines tokens do |tokens_in_line|
   30         yield (@lines.include? lineno += 1) ? %(<span class="hll">#{@delegate.format tokens_in_line}#{LF}</span>) : %(#{@delegate.format tokens_in_line}#{LF})
   31       end
   32     end
   33   end
   34 
   35   LF = ?\n
   36   HangingEndSpanTagCs = %(#{LF}</span>)
   37 
   38   private_constant :HangingEndSpanTagCs, :LF
   39 end; end; end