"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/AsciiDoc/highlight-treeprocessor.rb" (12 May 2020, 2112 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 require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
    2 require 'open3'
    3 
    4 include Asciidoctor
    5 
    6 # A treeprocessor (and companion docinfo processor) that highlights source
    7 # blocks using the highlight command.
    8 #
    9 # Usage
   10 #
   11 #   :source-highlighter: highlight
   12 #
   13 #   [source,ruby]
   14 #   ----
   15 #   puts 'Hello, World!'
   16 #   ----
   17 #
   18 Extensions.register do
   19   treeprocessor do
   20     process do |document|
   21       document.find_by context: :listing, style: 'source' do |src|
   22         # TODO handle callout numbers
   23         src.subs.clear
   24         lang = src.attr 'language', 'text', false
   25         highlight = document.attr 'highlight', 'highlight'
   26         if (document.attr 'highlight-css', 'class') == 'class'
   27           cmd = %(#{highlight} -f -O html --src-lang #{lang})
   28         else
   29           style = document.attr 'highlight-style', 'edit-eclipse'
   30           cmd = %(#{highlight} -f -O html --inline-css -s #{style} --src-lang #{lang})
   31         end
   32         cmd = %(#{cmd} -l -j 2) if src.attr? 'linenums', nil, false
   33         Open3.popen3 cmd do |stdin, stdout, stderr, wait_thr|
   34           stdin.write src.source
   35           stdin.close
   36           result = []
   37           while (line = stdout.gets)
   38             result << line.chomp
   39           end
   40           src.lines.replace result
   41           wait_thr.value
   42         end
   43       end if document.attr? 'source-highlighter', 'highlight'
   44       nil
   45     end 
   46   end
   47 
   48   docinfo_processor do
   49     at_location :footer
   50     process do |document|
   51       if (document.attr? 'source-highlighter', 'highlight') &&
   52           (document.attr 'highlight-css', 'class') == 'class'
   53         style = document.attr 'highlight-style', 'edit-eclipse'
   54         highlight = document.attr 'highlight', 'highlight'
   55         css = %x(#{highlight} -c stdout --print-style -s #{style}).rstrip
   56         if /^pre\.hl\s+{(?:\s*color:(.+?);)?(?:\s+background-color:(.+?);).*}$/ =~ css
   57           fg, bg = $1, $2
   58           css.sub! /^pre\.hl\s+{.*?}/, %(.listingblock pre.highlight { background-color:#{bg}; }\npre.highlight>code { color: #{fg}; })
   59         end
   60         ['<style>', css, '</style>'] * "\n"
   61       end
   62     end
   63   end
   64 end