forked from pandoc/lua-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath2svg.lua
189 lines (142 loc) · 5.67 KB
/
math2svg.lua
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
-- VERSION 2021-01-19
-- DESCRIPTION
--
-- This Lua filter for Pandoc converts LaTeX math to MathJax generated
-- scalable vector graphics (SVG) for insertion into the output document
-- in a standalone manner. SVG output is in any of the available MathJax
-- fonts. This is useful when a CSS paged media engine cannot process complex
-- JavaScript. No Internet connection is required when generating or viewing
-- SVG formulas, resulting in both absolute privacy and offline, standalone
-- robustness.
-- REQUIREMENTS, USAGE & PRIVACY
--
-- See: https://github.com/pandoc/lua-filters/tree/master/math2svg
-- LICENSE
--
-- Copyright (c) 2020-2021 Serge Y. Stroobandt
--
-- MIT License
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
-- CONTACT
--
-- $ echo c2VyZ2VAc3Ryb29iYW5kdC5jb20K |base64 -d
-- The full path to the tex2svg binary of the mathjax-node-cli package.
local tex2svg = 'tex2svg'
-- By default, DisplayMath is converted to SVG, whereas InlineMath is not.
local display2svg = true
local inline2svg = false
-- The fallback is MathML when pandoc is executed with the --mathml argument.
-- Textual annotation for speech
local speech = false
-- Automatic line breaking
local linebreaks = true
-- MathJax font
local font = 'TeX'
-- Supported MathJax fonts are:
-- https://docs.mathjax.org/en/latest/output/fonts.html
-- ex unit size in pixels
local ex = 6
-- Container width in ex for line breaking and tags
local width = 100
-- String of MathJax extensions for TeX and LaTeX to be loaded at run time
local extensions = ''
-- Available MathJax extensions are listed in:
-- /usr/local/lib/node_modules/mathjax-node-cli/node_modules/mathjax/unpacked/\
-- extensions/
-- Speed up conversion by caching SVG.
local cache = true
local _cache = {}
_cache.DisplayMath = {}
_cache.InlineMath = {}
local tags = {}
tags.DisplayMath = {'<span class="math display">', '</span>'}
tags.InlineMath = {'<span class="math inline">', '</span>'}
function Meta(meta)
tex2svg = tostring(meta.math2svg_tex2svg or tex2svg)
display2svg = meta.math2svg_display2svg or display2svg
inline2svg = meta.math2svg_inline2svg or inline2svg
speech = tostring(meta.math2svg_speech or speech)
linebreaks = tostring(meta.math2svg_linebreaks or linebreaks)
font = tostring(meta.math2svg_font or font)
ex = tostring(meta.math2svg_ex or ex)
width = tostring(meta.math2svg_width or width)
extensions = tostring(meta.math2svg_extensions or extensions)
cache = tostring(meta.math2svg_cache or cache)
end
function Math(elem)
local svg = nil
local argumentlist = {
'--speech', speech,
'--linebreaks', linebreaks,
'--font', font,
'--ex', ex,
'--width', width,
'--extensions', extensions,
elem.text
}
-- The available options for tex2svg are:
--help Show help [boolean]
--version Show version number [boolean]
--inline process as in-line TeX [boolean]
--speech include speech text [boolean] [default: true]
--linebreaks perform automatic line-breaking [boolean]
--font web font to use [default: "TeX"]
--ex ex-size in pixels [default: 6]
--width width of container in ex [default: 100]
--extensions extra MathJax extensions [default: ""]
-- e.g. 'Safe,TeX/noUndefined'
if (elem.mathtype == 'DisplayMath' and display2svg)
or (elem.mathtype == 'InlineMath' and inline2svg ) then
if cache then
-- Attempt to retrieve cache.
svg = _cache[elem.mathtype][elem.text]
end
if not svg then
if elem.mathtype == 'InlineMath' then
-- Add the --inline argument to the argument list.
table.insert(argumentlist, 1, '--inline')
end
-- Generate SVG.
svg = pandoc.pipe(tex2svg, argumentlist, '')
if cache then
-- Add to cache.
_cache[elem.mathtype][elem.text] = svg
end
end
end
-- Return
if svg then
if FORMAT:match '^html.?' then
svg = tags[elem.mathtype][1] .. svg .. tags[elem.mathtype][2]
return pandoc.RawInline(FORMAT, svg)
else
local filename = pandoc.sha1(svg) .. '.svg'
pandoc.mediabag.insert(filename, 'image/svg+xml', svg)
return pandoc.Image('', filename)
end
else
return elem
end
end -- function
-- Redefining the execution order.
return {
{Meta = Meta},
{Math = Math}
}