"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/syntax_highlighter/coderay.rb" (1 Jun 2019, 2310 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::CodeRayAdapter < SyntaxHighlighter::Base
    4   register_for 'coderay'
    5 
    6   def initialize *args
    7     super
    8     @pre_class = 'CodeRay'
    9     @requires_stylesheet = nil
   10   end
   11 
   12   def highlight?
   13     library_available?
   14   end
   15 
   16   def highlight node, source, lang, opts
   17     @requires_stylesheet = true if (css_mode = opts[:css_mode]) == :class
   18     lang = lang ? (::CodeRay::Scanners[lang = lang.to_sym] && lang rescue :text) : :text
   19     highlighted = ::CodeRay::Duo[lang, :html,
   20       css: css_mode,
   21       line_numbers: (line_numbers = opts[:number_lines]),
   22       line_number_start: opts[:start_line_number],
   23       line_number_anchors: false,
   24       highlight_lines: opts[:highlight_lines],
   25       bold_every: false,
   26     ].highlight source
   27     if line_numbers == :table && opts[:callouts]
   28       [highlighted, (idx = highlighted.index CodeCellStartTagCs) ? idx + CodeCellStartTagCs.length : nil]
   29     else
   30       highlighted
   31     end
   32   end
   33 
   34   def docinfo? location
   35     @requires_stylesheet && location == :footer
   36   end
   37 
   38   def docinfo location, doc, opts
   39     if opts[:linkcss]
   40       %(<link rel="stylesheet" href="#{doc.normalize_web_path stylesheet_basename, (doc.attr 'stylesdir', ''), false}"#{opts[:self_closing_tag_slash]}>)
   41     else
   42       %(<style>
   43 #{read_stylesheet}
   44 </style>)
   45     end
   46   end
   47 
   48   def write_stylesheet? doc
   49     @requires_stylesheet
   50   end
   51 
   52   def write_stylesheet doc, to_dir
   53     ::File.write (::File.join to_dir, stylesheet_basename), read_stylesheet, mode: FILE_WRITE_MODE
   54   end
   55 
   56   module Loader
   57     private
   58 
   59     def library_available?
   60       (@@library_status ||= load_library) == :loaded ? true : nil
   61     end
   62 
   63     def load_library
   64       (defined? ::CodeRay::Duo) ? :loaded : (Helpers.require_library 'coderay', true, :warn).nil? ? :unavailable : :loaded
   65     end
   66   end
   67 
   68   module Styles
   69     include Loader
   70 
   71     def read_stylesheet
   72       @@stylesheet_cache ||= (::File.read (::File.join Stylesheets::STYLESHEETS_DIR, stylesheet_basename), mode: FILE_READ_MODE).rstrip
   73     end
   74 
   75     def stylesheet_basename
   76       'coderay-asciidoctor.css'
   77     end
   78   end
   79 
   80   extend Styles # exports static methods
   81   include Loader, Styles # adds methods to instance
   82 
   83   CodeCellStartTagCs = '<td class="code"><pre>'
   84 
   85   private_constant :CodeCellStartTagCs
   86 end
   87 end