"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/syntax_highlighter/rouge.rb" (1 Jun 2019, 4050 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. See also the last Fossies "Diffs" side-by-side code changes report for "rouge.rb": 2.0.6_vs_2.0.7.

    1 # frozen_string_literal: true
    2 module Asciidoctor
    3 class SyntaxHighlighter::RougeAdapter < SyntaxHighlighter::Base
    4   register_for 'rouge'
    5 
    6   def initialize *args
    7     super
    8     @requires_stylesheet = @style = nil
    9   end
   10 
   11   def highlight?
   12     library_available?
   13   end
   14 
   15   def highlight node, source, lang, opts
   16     lexer = (::Rouge::Lexer.find_fancy lang) || ::Rouge::Lexers::PlainText
   17     lexer_opts = lexer.tag == 'php' && !(node.option? 'mixed') ? { start_inline: true } : {}
   18     @style ||= (style = opts[:style]) && (style_available? style) || DEFAULT_STYLE
   19     if opts[:css_mode] == :class
   20       @requires_stylesheet = true
   21       formatter = ::Rouge::Formatters::HTML.new inline_theme: @style
   22     else
   23       formatter = ::Rouge::Formatters::HTMLInline.new (::Rouge::Theme.find @style).new
   24     end
   25     if (highlight_lines = opts[:highlight_lines])
   26       formatter = RougeExt::Formatters::HTMLLineHighlighter.new formatter, lines: highlight_lines
   27     end
   28     if opts[:number_lines]
   29       formatter = RougeExt::Formatters::HTMLTable.new formatter, start_line: opts[:start_line_number]
   30       if opts[:callouts]
   31         return [(highlighted = formatter.format lexer.lex source, lexer_opts), (idx = highlighted.index CodeCellStartTagCs) ? idx + CodeCellStartTagCs.length : nil]
   32       end
   33     end
   34     formatter.format lexer.lex source, lexer_opts
   35   end
   36 
   37   def format node, lang, opts
   38     if (query_idx = lang && (lang.index '?'))
   39       lang = lang.slice 0, query_idx
   40     end
   41     if opts[:css_mode] != :class && (@style = (style = opts[:style]) && (style_available? style) || DEFAULT_STYLE) &&
   42         (pre_style_attr_val = base_style @style)
   43       opts[:transform] = proc {|pre| pre['style'] = pre_style_attr_val }
   44     end
   45     super
   46   end
   47 
   48   def docinfo? location
   49     @requires_stylesheet && location == :footer
   50   end
   51 
   52   def docinfo location, doc, opts
   53     if opts[:linkcss]
   54       %(<link rel="stylesheet" href="#{doc.normalize_web_path (stylesheet_basename @style), (doc.attr 'stylesdir', ''), false}"#{opts[:self_closing_tag_slash]}>)
   55     else
   56       %(<style>
   57 #{read_stylesheet @style}
   58 </style>)
   59     end
   60   end
   61 
   62   def write_stylesheet? doc
   63     @requires_stylesheet
   64   end
   65 
   66   def write_stylesheet doc, to_dir
   67     ::File.write (::File.join to_dir, (stylesheet_basename @style)), (read_stylesheet @style), mode: FILE_WRITE_MODE
   68   end
   69 
   70   module Loader
   71     private
   72 
   73     def library_available?
   74       (@@library_status ||= load_library) == :loaded ? true : nil
   75     end
   76 
   77     def load_library
   78       (defined? RougeExt) ? :loaded : (Helpers.require_library %(#{::File.dirname __dir__}/rouge_ext), 'rouge', :warn).nil? ? :unavailable : :loaded
   79     end
   80   end
   81 
   82   module Styles
   83     include Loader
   84 
   85     def read_stylesheet style
   86       library_available? ? @@stylesheet_cache[style || DEFAULT_STYLE] : '/* Rouge CSS disabled because Rouge is not available. */'
   87     end
   88 
   89     def stylesheet_basename style
   90       %(rouge-#{style || DEFAULT_STYLE}.css)
   91     end
   92 
   93     private
   94 
   95     def base_style style
   96       library_available? ? @@base_style_cache[style || DEFAULT_STYLE] : nil
   97     end
   98 
   99     def style_available? style
  100       (::Rouge::Theme.find style) && style
  101     end
  102 
  103     @@base_style_cache = ::Hash.new do |cache, key|
  104       base_style = (theme = ::Rouge::Theme.find key).base_style
  105       (val = base_style[:fg]) && ((style ||= []) << %(color: #{theme.palette val}))
  106       (val = base_style[:bg]) && ((style ||= []) << %(background-color: #{theme.palette val}))
  107       @@base_style_cache = cache.merge key => (resolved_base_style = style && (style.join ';'))
  108       resolved_base_style
  109     end
  110     @@stylesheet_cache = ::Hash.new do |cache, key|
  111       @@stylesheet_cache = cache.merge key => (stylesheet = ((::Rouge::Theme.find key).render scope: BASE_SELECTOR))
  112       stylesheet
  113     end
  114 
  115     DEFAULT_STYLE = 'github'
  116     BASE_SELECTOR = 'pre.rouge'
  117 
  118     private_constant :BASE_SELECTOR
  119   end
  120 
  121   extend Styles # exports static methods
  122   include Loader, Styles # adds methods to instance
  123 
  124   CodeCellStartTagCs = '<td class="code">'
  125 
  126   private_constant :CodeCellStartTagCs
  127 end
  128 end