1
+ -- VERSION 2021-01-19
2
+
3
+
1
4
-- DESCRIPTION
2
5
--
3
6
-- This Lua filter for Pandoc converts LaTeX math to MathJax generated
@@ -75,6 +78,17 @@ local extensions = ''
75
78
-- /usr/local/lib/node_modules/mathjax-node-cli/node_modules/mathjax/unpacked/\
76
79
-- extensions/
77
80
81
+ -- Speed up conversion by caching SVG.
82
+ local cache = true
83
+
84
+ local _cache = {}
85
+ _cache .DisplayMath = {}
86
+ _cache .InlineMath = {}
87
+
88
+ local tags = {}
89
+ tags .DisplayMath = {' <span class="math display">' , ' </span>' }
90
+ tags .InlineMath = {' <span class="math inline">' , ' </span>' }
91
+
78
92
79
93
function Meta (meta )
80
94
@@ -87,14 +101,15 @@ function Meta(meta)
87
101
ex = tostring (meta .math2svg_ex or ex )
88
102
width = tostring (meta .math2svg_width or width )
89
103
extensions = tostring (meta .math2svg_extensions or extensions )
104
+ cache = tostring (meta .math2svg_cache or cache )
90
105
91
106
end
92
107
93
108
94
109
function Math (elem )
95
110
96
111
local svg = nil
97
- local tags = nil
112
+
98
113
local argumentlist = {
99
114
' --speech' , speech ,
100
115
' --linebreaks' , linebreaks ,
@@ -117,26 +132,45 @@ function Math(elem)
117
132
-- extensions extra MathJax extensions [default: ""]
118
133
-- e.g. 'Safe,TeX/noUndefined'
119
134
120
- if elem .mathtype == ' DisplayMath' and display2svg then
121
- svg = pandoc .pipe (tex2svg , argumentlist , ' ' )
122
- tags = {' <span class="math display">' , ' </span>' }
135
+ if (elem .mathtype == ' DisplayMath' and display2svg )
136
+ or (elem .mathtype == ' InlineMath' and inline2svg ) then
137
+
138
+ if cache then
139
+ -- Attempt to retrieve cache.
140
+ svg = _cache [elem .mathtype ][elem .text ]
141
+ end
142
+
143
+ if not svg then
144
+
145
+ if elem .mathtype == ' InlineMath' then
146
+ -- Add the --inline argument to the argument list.
147
+ table.insert (argumentlist , 1 , ' --inline' )
148
+ end
149
+
150
+ -- Generate SVG.
151
+ svg = pandoc .pipe (tex2svg , argumentlist , ' ' )
152
+
153
+ if cache then
154
+ -- Add to cache.
155
+ _cache [elem .mathtype ][elem .text ] = svg
156
+ end
123
157
124
- elseif elem .mathtype == ' InlineMath' and inline2svg then
125
- table.insert (argumentlist , 1 , ' --inline' )
126
- svg = pandoc .pipe (tex2svg , argumentlist , ' ' )
127
- tags = {' <span class="math inline">' , ' </span>' }
158
+ end
128
159
129
160
end
130
161
162
+ -- Return
131
163
if svg then
132
164
133
165
if FORMAT :match ' ^html.?' then
134
- svg = tags [1 ] .. svg .. tags [2 ]
166
+ svg = tags [elem . mathtype ][ 1 ] .. svg .. tags [ elem . mathtype ] [2 ]
135
167
return pandoc .RawInline (FORMAT , svg )
168
+
136
169
else
137
170
local filename = pandoc .sha1 (svg ) .. ' .svg'
138
171
pandoc .mediabag .insert (filename , ' image/svg+xml' , svg )
139
172
return pandoc .Image (' ' , filename )
173
+
140
174
end
141
175
142
176
else
@@ -145,7 +179,7 @@ function Math(elem)
145
179
146
180
end
147
181
148
- end
182
+ end -- function
149
183
150
184
151
185
-- Redefining the execution order.
0 commit comments