"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/AsciiDoc/highlight-treeprocessor_mod.rb" (12 May 2020, 5083 Bytes) of package /windows/www/highlight-3.57-x64.zip:


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 =begin
    2 
    3 highlight-treeprocessor_mod.rb"         v1.3.0 | 2019/03/30 | by Tristano Ajmone
    4 ================================================================================
    5 
    6                         Highlight Treeprocessor Extension
    7 
    8 ================================================================================
    9 A treeprocessor that highlights source code using André Simon's Highlight:
   10 
   11   http://www.andre-simon.de
   12 
   13 Usage:
   14 
   15   :source-highlighter: highlight
   16 
   17   [source,ruby]
   18   ----
   19   puts 'Hello, World!'
   20   ----
   21 
   22 --------------------------------------------------------------------------------
   23 Adapted by Tristano Ajmone from the original "highlight-treeprocessor.rb" taken
   24 from the Asciidoctor Extensions Lab (commit 18bdf62), Copyright (C) 2014-2016
   25 The Asciidoctor Project, released under MIT License:
   26 
   27     https://github.com/asciidoctor/asciidoctor-extensions-lab/
   28 --------------------------------------------------------------------------------
   29 The extension was modified (trimmed down) in order to:
   30 - enforce `:highlight-css: class` without requiring attribute settings.
   31 - disable the `:highlight-style:` option (we use custom CSS in this context).
   32 - enable substitutions
   33 - correctly handle the `linenums` option.
   34 - Enforce $HIGHLIGHT_DATADIR via "--data-dir=" option (if defined).
   35 --------------------------------------------------------------------------------
   36 =end
   37 
   38 require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
   39 require 'open3'
   40 
   41 include Asciidoctor
   42 
   43 
   44 Extensions.register do
   45   # =============
   46   # TreeProcessor
   47   # =============
   48   # Processes the Asciidoctor::Document (AST) once parsing is complete.
   49   # ----------------------------------------------------------------------------
   50   treeprocessor do
   51     process do |document|
   52       document.find_by context: :listing, style: 'source' do |src|
   53         # TODO handle callout numbers
   54 
   55         #-----------------------------------------------------------------------
   56         # ** SUBSTITUTIONS ** were enabled by commenting out the following line:
   57         # ~~~~~~~~~~~~~~
   58         # src.subs.clear
   59         # ~~~~~~~~~~~~~~
   60         # If no `subs` are specified in the listing block, the default setting
   61         # will be to substitute special characters, which breaks up the code.
   62         # Therefore, we check if the `subs` array contains `:specialcharacters`
   63         # and eliminate it if it does:
   64         spchindx = src.subs.index(:specialcharacters)
   65         src.subs.delete_at(spchindx) if spchindx
   66         #-----------------------------------------------------------------------
   67         # ** ENFORCE HIGHLIGHT DATADIR **
   68         #
   69         # If the HIGHLIGHT_DATADIR env var is defined we'll enforce it via
   70         # Highlight option "--data-dir=<HIGHLIGHT_DATADIR>" so that it gets the
   71         # highest override priority:
   72 
   73         if ENV['HIGHLIGHT_DATADIR'] != nil
   74           hlDataDir=" --data-dir=" + ENV['HIGHLIGHT_DATADIR']
   75         else
   76           hlDataDir=""
   77         end
   78 
   79         # This ensures that custom langDefs will always override the predefined
   80         # ones, whereas usually the the path of the HIGHLIGHT_DATADIR env var
   81         # would be searched after Highlight installation folder.
   82         #-----------------------------------------------------------------------
   83 
   84         lang = src.attr 'language', 'text', false
   85         highlight = document.attr 'highlight', 'highlight'
   86         cmd = %(#{highlight} -f -O html --src-lang #{lang} #{hlDataDir})
   87         cmd = %(#{cmd} -l -j 2) if src.attr? 'linenums', nil, false
   88         Open3.popen3 cmd do |stdin, stdout, stderr, wait_thr|
   89           stdin.write src.source
   90           stdin.close
   91           result = []
   92           while (line = stdout.gets)
   93             result << line.chomp
   94           end
   95           src.lines.replace result
   96           wait_thr.value
   97         end
   98       end if document.attr? 'source-highlighter', 'highlight'
   99       nil
  100     end
  101   end
  102 end
  103 
  104 =begin
  105 --------------------------------------------------------------------------------
  106                                    ChangeLog
  107 --------------------------------------------------------------------------------
  108 v1.3.0 (2019/03/30)
  109   Enforce $HIGHLIGHT_DATADIR:
  110   - If the HIGHLIGHT_DATADIR env var is defined then enforce it via:
  111 
  112         --data-dir=<HIGHLIGHT_DATADIR>
  113 
  114     This ensures that custom langDefs will always override same-named files in
  115     Highlight installation folder.
  116 
  117 v1.2.0 (2019/03/24)
  118   Fixes the problems introduced in v1.1.0:
  119   - Added code to handle defaults in listing with unspecified `subs`.
  120   - No longer mandatory to specify `subs` in listing block.
  121   - Option `linenums` works correctly again.
  122 
  123 v1.1.0 (2019/03/13)
  124   - Enabled substitutions, but the `linenums` option doesn't work anymore.
  125   - Requires mandatory declaration of `subs` in code listings (even if only
  126     `subs=none`) otherwise the sourcedcode breaks up in the listing block.
  127 
  128 v1.0.0 (2018/10/04)
  129   First mod, based commit 18bdf62:
  130   - Enforce `:highlight-css: class` without requiring attribute settings.
  131   - Disable the `:highlight-style:` option (we use custom CSS in this context).
  132 
  133 =end