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