-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight-treeprocessor_mod.rb
More file actions
68 lines (56 loc) · 2.33 KB
/
Copy pathhighlight-treeprocessor_mod.rb
File metadata and controls
68 lines (56 loc) · 2.33 KB
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
64
65
66
67
68
=begin highlight-treeprocessor_mod.rb" v1.0.0 (2018-10-04)
================================================================================
Highlight Treprocessor Extension
================================================================================
A treeprocessor that highlights source cdoe blocks using Highlight.
Usage:
:source-highlighter: highlight
[source,ruby]
----
puts 'Hello, World!'
----
--------------------------------------------------------------------------------
Adapted by Tristano Ajmone from the original "highlight-treeprocessor.rb" taken
from the Asciidoctor Extensions Lab (commit 18bdf62), Copyright (C) 2014-2016
The Asciidoctor Project, released under MIT License:
https://github.com/asciidoctor/asciidoctor-extensions-lab/
--------------------------------------------------------------------------------
The extension was modified (trimmed down) in order to:
- enforce ':highlight-css: class' without requiring attribute settingss.
- disable the ':highlight-style:' option (we use custom CSS in this context).
--------------------------------------------------------------------------------
=end
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
require 'open3'
include Asciidoctor
Extensions.register do
# =============
# TreeProcessor
# =============
# Processes the Asciidoctor::Document (AST) once parsing is complete.
# ----------------------------------------------------------------------------
treeprocessor do
process do |document|
document.find_by context: :listing, style: 'source' do |src|
# TODO handle callout numbers
src.subs.clear
lang = src.attr 'language', 'text', false
highlight = document.attr 'highlight', 'highlight'
# highlight = document.attr 'highlight', 'highlight'
cmd = %(#{highlight} -f -O html --src-lang #{lang})
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'
nil
end
end
end