-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasciidoctor_wrapper.rb
executable file
·214 lines (182 loc) · 6.02 KB
/
asciidoctor_wrapper.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/ruby
require 'asciidoctor'
require 'asciidoctor/extensions'
require 'asciidoctor/cli'
require 'tmpdir'
require 'cgi'
require 'digest'
module MathRenderer
FONT_SIZE_PX = 16 # main html font size
PX_IN_EX = 8.5
DPI_PER_PX = 14.454 # selected so that height of simple single-line formula matches height of main html font
SCALING_FACTOR = 4.0 # dvipng produces preciser results with higher DPI; must be at least 2.0 because of HI-DPI monitors
def gentex formula
tex = ''
tex += "\\documentclass[10pt]{article}\n"
tex += "\\usepackage[utf8]{inputenc}\n"
tex += "\\usepackage{amsmath}\n"
tex += "\\usepackage{amssymb}\n"
# used by dvipng to extract baseline (--depth) from *.dvi
tex += "\\usepackage[active,textmath]{preview}\n"
tex += "\\usepackage{xcolor}\n"
# Fonts: https://en.wikibooks.org/wiki/LaTeX/Fonts
#tex += "\\renewcommand{\\sfdefault}{phv}\n"
#tex += "\\renewcommand{\\familydefault}{\\sfdefault}\n"
#tex += "\\SetSymbolFont{letters}{normal}{OML}{zplm}{m}{it}\n"
tex += "\\SetSymbolFont{operators}{normal}{OT1}{ptm}{m}{n}\n"
tex += "\\newcommand{\\less}{<}\n"
tex += "\\newcommand{\\gtr}{>}\n"
tex += "\\pagestyle{empty}\n"
tex += "\\begin{document}\n"
tex += %($\\color{black!85}\\displaystyle{#{formula}}$\n)
tex += "\\end{document}\n"
return tex
end
def convert1 formula, dir
puts dir
texfile = File.join(dir, 'formula.tex')
dvifile = File.join(dir, 'formula.dvi')
pngfile = File.join(dir, 'formula.png')
svgfile = File.join(dir, 'formula.svg')
# tex -> dvi
texdata = gentex(formula)
puts texdata
IO.binwrite(texfile, texdata)
if !system(%(cd #{dir} && latex -halt-on-error -interaction=batchmode #{texfile}))
raise 'latex execution failed'
end
# dvi -> svg
# without --cache=none dvisvgm may produce different XML on each invocation
# which is not convenient for storing result in VCS
# (difference is in order of tags -- not in perceptual content)
if !system(%(cd #{dir} && dvisvgm --cache=none -n -b min -e #{dvifile}))
raise 'dvisvgm execution failed'
end
svgdata = IO.binread(svgfile)
# dvi -> png (to get dimensions and baseline)
dpi = (1.0*FONT_SIZE_PX*DPI_PER_PX*SCALING_FACTOR).round
output=`cd #{dir} && dvipng -T tight -D #{dpi} --depth -o #{pngfile} #{dvifile}`
if !$?.success?
raise 'dvipng execution failed'
end
if !output.match(/depth=(\d+)/)
raise 'dvipng execution failed'
end
pngdepth = $~[1].to_i
pngwidth, pngheight = IO.read(pngfile)[0x10..0x18].unpack('NN')
return {
'svgdata' => svgdata,
'exheight' => 1.0*pngheight/FONT_SIZE_PX/SCALING_FACTOR,
'exvalign' => -1.0*pngdepth/FONT_SIZE_PX/SCALING_FACTOR,
'pxheight' => 1.0*pngheight/FONT_SIZE_PX/SCALING_FACTOR*PX_IN_EX,
'formula' => formula
}
end
def convert formula
Dir.mktmpdir do |dir|
return convert1(formula, dir)
end
end
def store img
dir = ENV['MATH_OUTPUT']
if !dir
raise 'No MATH_OUTPUT env var set'
end
name = %(math-#{Digest::SHA256.hexdigest(img['formula'])[0,16]}.svg)
path = File.join(dir, name)
IO.binwrite(File.join(dir, name), img['svgdata'])
return name
end
end
class MathInlineMacroProcessor < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :math
using_format :short
include MathRenderer
def process parent, target, attrs
formula = target
if !formula or formula == ''
return ''
end
img = convert(formula)
path = store(img)
html = ''
html += %(<img)
html += %( src="#{CGI.escapeHTML(path)}")
html += %( class="inlinemath")
html += %( style=")
html += %(height:#{CGI.escapeHTML(img['exheight'].round(3).to_s())}ex;)
html += %(vertical-align:#{CGI.escapeHTML(img['exvalign'].round(3).to_s())}ex;)
html += %(")
html += %( alt="#{CGI.escapeHTML(img['formula'])}")
html += %(/>)
return html
end
# This allows multiline https://github.com/asciidoctor/asciidoctor/issues/3263
def resolve_regexp name, format
if format == :short
%r(\\?#{name}:\[((?:\\\]|[^\]])*?)\])
else
%r(\\?#{name}:(\S+?)\[((?:\\\]|[^\]])*?)\])
end
end
end
class MathBlockProcessor < Asciidoctor::Extensions::BlockProcessor
use_dsl
named :math
on_context :listing
include MathRenderer
def process parent, reader, attrs
formula = reader.lines.join("\n")
if !formula or formula == ''
return nil
end
img = convert(formula)
path = store(img)
attrs['target'] = path
attrs['alt'] = formula
if !attrs['width'] and !attrs['height']
attrs['height'] = img['pxheight'].round()
end
if !attrs['align'] and !attrs['role'] and !attrs['float']
attrs['role'] = 'text-indent'
end
return create_image_block parent, attrs
end
end
class DisqusBlockMacroProcessor < Asciidoctor::Extensions::BlockMacroProcessor
use_dsl
named :disqus
def process parent, target, attrs
site_id = 'gudok'
page_id = target
page_url = 'http://gudok.xyz/' + CGI.escape(page_id) + '/'
# todo: escape javascript
html = %(
<div id='disqus_thread'></div>
<script>
var disqus_config = function () {
this.page.url = '#{page_url}';
this.page.identifier = '#{page_id}';
};
(function() {
var d = document, s = d.createElement('script');
s.src = '//#{site_id}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href='https://disqus.com/?ref_noscript'>comments powered by Disqus.</a></noscript>
)
create_pass_block parent, html, attrs, subs: nil
end
end
Asciidoctor::Extensions.register do
inline_macro MathInlineMacroProcessor
block MathBlockProcessor
block_macro DisqusBlockMacroProcessor
end
invoker = Asciidoctor::Cli::Invoker.new ARGV
GC.start
invoker.invoke!
exit invoker.code