"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/syntax_highlighter/pygments.rb" (1 Jun 2019, 5503 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 module Asciidoctor
    3 class SyntaxHighlighter::PygmentsAdapter < SyntaxHighlighter::Base
    4   register_for 'pygments'
    5 
    6   def initialize *args
    7     super
    8     @requires_stylesheet = nil
    9     @style = nil
   10   end
   11 
   12   def highlight?
   13     library_available?
   14   end
   15 
   16   def highlight node, source, lang, opts
   17     lexer = (::Pygments::Lexer.find_by_alias lang) || (::Pygments::Lexer.find_by_mimetype 'text/plain')
   18     @requires_stylesheet = true unless (noclasses = opts[:css_mode] != :class)
   19     highlight_opts = {
   20       classprefix: TOKEN_CLASS_PREFIX,
   21       cssclass: WRAPPER_CLASS,
   22       nobackground: true,
   23       noclasses: noclasses,
   24       startinline: lexer.name == 'PHP' && !(node.option? 'mixed'),
   25       stripnl: false,
   26       style: (@style ||= (style = opts[:style]) && (style_available? style) || DEFAULT_STYLE),
   27     }
   28     if (highlight_lines = opts[:highlight_lines])
   29       highlight_opts[:hl_lines] = highlight_lines.join ' '
   30     end
   31     if (linenos = opts[:number_lines]) && (highlight_opts[:linenostart] = opts[:start_line_number]) && (highlight_opts[:linenos] = linenos) == :table
   32       if (highlighted = lexer.highlight source, options: highlight_opts)
   33         highlighted = highlighted.sub StyledLinenoColumnStartTagsRx, LinenoColumnStartTagsCs if noclasses
   34         highlighted = highlighted.sub WrapperTagRx, PreTagCs
   35         opts[:callouts] ? [highlighted, (idx = highlighted.index CodeCellStartTagCs) ? idx + CodeCellStartTagCs.length : nil] : highlighted
   36       else
   37         node.sub_specialchars source # handles nil response from ::Pygments::Lexer#highlight
   38       end
   39     elsif (highlighted = lexer.highlight source, options: highlight_opts)
   40       highlighted = highlighted.gsub StyledLinenoSpanTagRx, LinenoSpanTagCs if linenos && noclasses
   41       highlighted.sub WrapperTagRx, '\1'
   42     else
   43       node.sub_specialchars source # handles nil response from ::Pygments::Lexer#highlight
   44     end
   45   end
   46 
   47   def format node, lang, opts
   48     if opts[:css_mode] != :class && (@style = (style = opts[:style]) && (style_available? style) || DEFAULT_STYLE) &&
   49         (pre_style_attr_val = base_style @style)
   50       opts[:transform] = proc {|pre| pre['style'] = pre_style_attr_val }
   51     end
   52     super
   53   end
   54 
   55   def docinfo? location
   56     @requires_stylesheet && location == :footer
   57   end
   58 
   59   def docinfo location, doc, opts
   60     if opts[:linkcss]
   61       %(<link rel="stylesheet" href="#{doc.normalize_web_path (stylesheet_basename @style), (doc.attr 'stylesdir', ''), false}"#{opts[:self_closing_tag_slash]}>)
   62     else
   63       %(<style>
   64 #{read_stylesheet @style}
   65 </style>)
   66     end
   67   end
   68 
   69   def write_stylesheet? doc
   70     @requires_stylesheet
   71   end
   72 
   73   def write_stylesheet doc, to_dir
   74     ::File.write (::File.join to_dir, (stylesheet_basename @style)), (read_stylesheet @style), mode: FILE_WRITE_MODE
   75   end
   76 
   77   module Loader
   78     private
   79 
   80     def library_available?
   81       (@@library_status ||= load_library) == :loaded ? true : nil
   82     end
   83 
   84     def load_library
   85       (defined? ::Pygments::Lexer) ? :loaded : (Helpers.require_library 'pygments', 'pygments.rb', :warn).nil? ? :unavailable : :loaded
   86     end
   87   end
   88 
   89   module Styles
   90     include Loader
   91 
   92     def read_stylesheet style
   93       library_available? ? @@stylesheet_cache[style || DEFAULT_STYLE] || '/* Failed to load Pygments CSS. */' : '/* Pygments CSS disabled because Pygments is not available. */'
   94     end
   95 
   96     def stylesheet_basename style
   97       %(pygments-#{style || DEFAULT_STYLE}.css)
   98     end
   99 
  100     private
  101 
  102     def base_style style
  103       library_available? ? @@base_style_cache[style || DEFAULT_STYLE] : nil
  104     end
  105 
  106     def style_available? style
  107       (((@@available_styles ||= ::Pygments.styles.to_set).include? style) rescue nil) && style
  108     end
  109 
  110     @@base_style_cache = ::Hash.new do |cache, key|
  111       if BaseStyleRx =~ @@stylesheet_cache[key]
  112         @@base_style_cache = cache.merge key => (style = $1.strip)
  113         style
  114       end
  115     end
  116     @@stylesheet_cache = ::Hash.new do |cache, key|
  117       if (stylesheet = ::Pygments.css BASE_SELECTOR, classprefix: TOKEN_CLASS_PREFIX, style: key)
  118         @@stylesheet_cache = cache.merge key => stylesheet
  119         stylesheet
  120       end
  121     end
  122 
  123     DEFAULT_STYLE = 'default'
  124     BASE_SELECTOR = 'pre.pygments'
  125     TOKEN_CLASS_PREFIX = 'tok-'
  126 
  127     BaseStyleRx = /^#{BASE_SELECTOR.gsub '.', '\\.'} +\{([^}]+?)\}/
  128 
  129     private_constant :BASE_SELECTOR, :TOKEN_CLASS_PREFIX, :BaseStyleRx
  130   end
  131 
  132   extend Styles # exports static methods
  133   include Loader, Styles # adds methods to instance
  134 
  135   CodeCellStartTagCs = '<td class="code">'
  136   LinenoColumnStartTagsCs = '<td class="linenos"><div class="linenodiv"><pre>'
  137   LinenoSpanTagCs = '<span class="lineno">\1</span>'
  138   PreTagCs = '<pre>\1</pre>'
  139   StyledLinenoColumnStartTagsRx = /<td><div class="linenodiv" style="[^"]+?"><pre style="[^"]+?">/
  140   StyledLinenoSpanTagRx = %r(<span style="background-color: #f0f0f0; padding: 0 5px 0 5px">( *\d+ )</span>)
  141   WRAPPER_CLASS = 'lineno' # doesn't appear in output; Pygments appends "table" to this value to make nested table class
  142   # NOTE <pre> has style attribute when pygments-css=style
  143   # NOTE <div> has trailing newline when pygments-linenums-mode=table
  144   # NOTE initial <span></span> preserves leading blank lines
  145   WrapperTagRx = %r(<div class="#{WRAPPER_CLASS}"><pre\b[^>]*?>(.*)</pre></div>\n*)m
  146 
  147   private_constant :CodeCellStartTagCs, :LinenoColumnStartTagsCs, :LinenoSpanTagCs, :PreTagCs, :StyledLinenoColumnStartTagsRx, :StyledLinenoSpanTagRx, :WrapperTagRx, :WRAPPER_CLASS
  148 end
  149 end