-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlight-treeprocessor_mod.rb
139 lines (114 loc) · 5.29 KB
/
highlight-treeprocessor_mod.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
=begin
highlight-treeprocessor_mod.rb" v1.4.0 | 2019/12/31 | by Tristano Ajmone
================================================================================
Highlight Treeprocessor Extension
================================================================================
A treeprocessor that highlights source code using André Simon's Highlight:
http://www.andre-simon.de
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 c9ce3ab), 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 settings.
- disable the `:highlight-style:` option (we use custom CSS in this context).
- enable substitutions
- correctly handle the `linenums` option.
- Enforce $HIGHLIGHT_DATADIR via "--data-dir=" option (if defined).
--------------------------------------------------------------------------------
=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').each do |src|
# TODO handle callout numbers
#-----------------------------------------------------------------------
# ** SUBSTITUTIONS ** were enabled by commenting out the following line:
# ~~~~~~~~~~~~~~
# src.subs.clear
# ~~~~~~~~~~~~~~
# If no `subs` are specified in the listing block, the default setting
# will be to substitute special characters, which breaks up the code.
# Therefore, we check if the `subs` array contains `:specialcharacters`
# and eliminate it if it does:
spchindx = src.subs.index(:specialcharacters)
src.subs.delete_at(spchindx) if spchindx
#-----------------------------------------------------------------------
# ** ENFORCE HIGHLIGHT DATADIR **
#
# If the HIGHLIGHT_DATADIR env var is defined we'll enforce it via
# Highlight option "--data-dir=<HIGHLIGHT_DATADIR>" so that it gets the
# highest override priority:
if ENV['HIGHLIGHT_DATADIR'] != nil
hlDataDir=" --data-dir=" + ENV['HIGHLIGHT_DATADIR']
else
hlDataDir=""
end
# This ensures that custom langDefs will always override the predefined
# ones, even on Windows where the path of the HIGHLIGHT_DATADIR env var
# would be searched after Highlight installation folder (unlike on Linux
# and Mac).
#-----------------------------------------------------------------------
lang = src.attr 'language', 'text', false
highlight = document.attr 'highlight', 'highlight'
cmd = %(#{highlight} -f -O html --src-lang #{lang} #{hlDataDir})
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
end
=begin
--------------------------------------------------------------------------------
ChangeLog
--------------------------------------------------------------------------------
v1.4.0 (2019/12/31)
Integrate upstream changes from commit 'c9ce3ab' (2018/10/26):
https://github.com/asciidoctor/asciidoctor-extensions-lab/commit/c9ce3ab
- Iterate on return value instead of using filter block.
v1.3.0 (2019/03/30)
Enforce $HIGHLIGHT_DATADIR:
- If the HIGHLIGHT_DATADIR env var is defined then enforce it via:
--data-dir=<HIGHLIGHT_DATADIR>
This ensures that custom langDefs will always override same-named files in
Highlight installation folder, even under Windows (where HIGHLIGHT_DATADIR
has a lower search priority).
v1.2.0 (2019/03/24)
Fixes the problems introduced in v1.1.0:
- Added code to handle defaults in listing with unspecified `subs`.
- No longer mandatory to specify `subs` in listing block.
- Option `linenums` works correctly again.
v1.1.0 (2019/03/13)
- Enabled substitutions, but the `linenums` option doesn't work anymore.
- Requires mandatory declaration of `subs` in code listings (even if only
`subs=none`) otherwise the sourcedcode breaks up in the listing block.
v1.0.0 (2018/10/04)
First mod, based commit 18bdf62:
- Enforce `:highlight-css: class` without requiring attribute settings.
- Disable the `:highlight-style:` option (we use custom CSS in this context).
=end