-
Notifications
You must be signed in to change notification settings - Fork 0
/
.latex_to_html5
executable file
·348 lines (291 loc) · 11.1 KB
/
.latex_to_html5
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env lua5.3
-- lua filter for converting (a subset of) LaTeX into HTML5 with mathjax
-- Basic idea: leave the math to matjax and search&replace simple commands
-- Note that this does not really parse the LaTeX source, it only performs
-- a line-based pattern matching for a well-behaved subset of LaTeX that
-- i am forced to use if i want to run this filter.
-- read whole input file into a string
whole_file = io.read("*all")
-- extract title and other data
metadata_title = whole_file:match("^\\title{(.-)}")
metadata_maths = false
-- table of global substitutions (could be implemented in sed)
global_substitutions = {
-- change a few character entities
{"&", "&"}, -- note: this affects LaTeX tables, see below
{"<", "<"},
{">", ">"},
{"~", " "}, -- real non-breaking space here, equivalent to
-- htmlize SCRIPT tags
{"\n%%(SCRIPT[^\n]+)", "\n<!--\n%%1\n-->"},
{"\n%-%->\n<!%-%-", ""},
-- do not remove comments, just escape % to %
{"%%", "%"},
-- change \emph{...} to <i>...</i> (and similar inline stuff)
{"\\emph{(.-)}", "<i>%1</i>"},
{"\\textbf{(.-)}", "<b>%1</b>"},
{"\\textit{(.-)}", "<i>%1</i>"},
{"\\texttt{(.-)}", "<code>%1</code>"},
{"{\\it%s(.-)}", "<i>%1</i>"},
{"{\\bf%s(.-)}", "<b>%1</b>"},
{"{\\tt%s(.-)}", "<code>%1</code>"},
-- change \includegraphics{...} to <img src="...">
{"\\includegraphics{(.-)}", '<img src="%1" alt="image">'},
-- change \href{link}{text} to <a href="link">text</a>
{"\\href{(.-)}{(.-)}", "<a href=\"%1\">%2</a>"},
{"\\url{(.-)}", "<a href=\"%1\">%1</a>"},
-- change \newline to <br>
{"\\newline", "<br>"},
{"\\bigskip", "<p>"},
{"\\medskip", "<p>"},
{"\\smallskip", ""},
-- remove latex-only stuff
{"\\clearpage", ""},
{"\\thispagestyle{(.-)}", ""},
{"\\setcounter{(.-)}{(.-)}", ""},
{"\\vfill", ""},
{"\\mbox{(.-)}", '<span>%1</span>'},
{"\\fbox{(.-)}", '<span style="border:1px solid">%1</span>'},
-- change \sections to <headers>
{"\\title{(.-)}", "<h1>%1</h1>\n"},
{"\\section{(.-)}", "<h2>%1</h2>\n"},
{"\\subsection{(.-)}", "<h3>%1</h3>\n"},
{"\\paragraph{(.-)}", "<p>\n<b>%1</b>"},
-- list environments (TODO: support for \item[] )
{"\\item ", "<li>" },
{"\\begin{enumerate}", "<ol>"},
{"\\end{enumerate}", "</ol>"},
{"\\begin{itemize}", "<ul>"},
{"\\end{itemize}", "</ul>"},
{"\\begin{verbatim}", "<pre>"},
{"\\end{verbatim}", "</pre>"},
{"\\item%[(.-)%] ", "<li style='list-style-type:none'>%1 "},
-- change \verb+...+ to <code>...</code>
{"\\verb%+([^+]*)%+", "<code>%1</code>"},
{"\\verb_([^_]*)_", "<code>%1</code>"},
-- quotation environements
{"\\begin{quotation}", "<blockquote>"},
{"\\end{quotation}", "</blockquote>"},
{"\\begin{quote}", "<blockquote>"},
{"\\end{quote}", "</blockquote>"},
-- gallery environement
{"\\begin{gallery}", "<div class=\"gallery\" style=\"height:400px\"><ul class=\"index\">\n"},
{"\\end{gallery}", "</ul></div>"},
{"\\galleryline{(.-)}", '<li><a href="%1">%1<span><img src="%1" alt=""></span></a>\n'},
-- theorem environments (TODO: different class for each type)
{"\\begin{proof}", "<p>\n<i>Proof.</i>"},
{"\\end{proof}", "<span style=\"float:right\">∎</span>"},
-- to avoid repetitive code, the other environments are added in a loop
-- below
-- abstract environement (wherever it appears)
{"\\begin{abstract}(.-)\\end{abstract}",
'<blockquote style="font-size:80%%"><b>Abstract.</b>%1</blockquote>'
},
-- tabular (TODO: recover vertical bars)
-- XXX: the order of these pattern replacements is important
{"%s*\\\\(%s*\\end{tabular})", "%1"},
{"\\begin{tabular}{.-}", "<table><tr><td>"},
{"\\end{tabular}", "</table>"},
-- surround \newcommands by math so that mathjax sees it
{"\n(\\newcommand[^\n]+)", "$%1$\n"},
-- hack to remove "i/" from displayed image and text filenames
{'([^"])i/([%a.-]-%.tif)', "%1%2"},
{'([^"])i/([%a.-]-%.png)', "%1%2"},
{'([^"])i/([%a.-]-%.jpg)', "%1%2"},
{'([^"])i/([%a.-]-%.txt)', "%1%2"},
{'([^"])o/([%a.-]-%.tif)', "%1%2"},
{'([^"])o/([%a.-]-%.png)', "%1%2"},
{'([^"])o/([%a.-]-%.jpg)', "%1%2"},
{'([^"])o/([%a.-]-%.txt)', "%1%2"},
}
-- add theorem-like environments to the list of substitutions
thm_envs = {"Theorem", "Definition", "Remark", "Lemma", "Proposition",
"Exercice", "Axiom"}
for _,X in pairs(thm_envs) do
x = X:lower()
p = {"\\begin{"..x.."}%[(.-)%]",
'<p>\n<div class="thm"><b>'..X..'</b> (%1). <em>'}
q = {"\\begin{"..x.."}", '<p>\n<div class="thm"><b>'..X.."</b>. <em>"}
r = {"\\end{"..x.."}", "</em></div>"}
table.insert(global_substitutions, p)
table.insert(global_substitutions, q)
table.insert(global_substitutions, r)
end
-- perform the global substitutions
for _,s in ipairs(global_substitutions) do
whole_file = whole_file:gsub(s[1], s[2])
end
-- process the galleries
list_of_galleries = {}
-- g traverses all gallery blocks
for g in whole_file:gmatch('(<div class="gallery" .-</ul></div>)') do
f = g:match('<img src="(.-)"') -- f=first image file in the gallery
h = io.popen("imprintf %h "..f):read("*all*") -- h=image height
_,n = g:gsub("<li>", "") -- n=total number of images in the gallery
table.insert(list_of_galleries, {f, h, math.ceil(2*n)})
end
-- adapt the size of each gallery
for i=1,#list_of_galleries do
g = list_of_galleries[i]
f,h,n = g[1],g[2],g[3] -- filename, height in pixels, height in em
f2 = f:gsub("%-", "--") -- escaped filename
p = 'gallery" style="height:400px">(.-<a href=")'..f2
q = 'gallery" style="min-height:'..h..'px;height:'..n..'em">%1'..f
whole_file = whole_file:gsub(p, q)
end
-- process \input{file} and \VerbatimInput{file}
putfile = function(l) return io.open(l, "r"):read("*all") end
putfile_verb = function(l) return "<pre>\n"..io.open(l, "r"):read("*all").."</pre>\n" end
whole_file = whole_file:gsub("\\input{(.-)}", putfile)
whole_file = whole_file:gsub("\\VerbatimInput{(.-)}", putfile_verb)
output_template = [[
<!doctype html>
<meta charset="utf-8" />
<title>TITLE</title>
CSSCODE
JAXCODE
LITCODE
]]
boilerplate_lit = [[
<!--
This file is a literate program.
The experiments are run by applying the following filter:
grep ^%%SCRIPT | sed 's/&/\&/g' | cut -c9- | sh
-->
]]
boilerplate_css = [[
body { max-width:90ex; }
pre { background:lightgray; width:80ch; }
table, td { border:1px solid black; border-collapse:collapse; }
table td { padding:7px; border-spacing:0px; }
.thm em i { font-style: normal;}]]
-- TODO: only add the styling if the concerned elements are present
css_gallery = [[
.gallery{position:relative;width:auto;height:400px}
.gallery .index{padding:0;margin:0;width:9em;list-style:none}
.gallery .index li{margin:0;padding:0}
.gallery .index a{display:block;background-color:#eee;border:1px solid #fff;text-decoration:none;width:11em;padding:5px}
.gallery .index a span{display:block;position:absolute;left:-9999px;top:0;padding-left:2em}
.gallery .index li:first-child a span{left:10em;z-index:99}
.gallery .index a:hover{ border: 1px solid #888888;}
.gallery .index a:hover span{left:10em;z-index:100}
.gallery .index a span img{ }
.gallery .index a span { white-space:nowrap; }
]]
boilerplate_mathjax = [[
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ['\\(','\\)'] ],
processEscapes: true
}
});
</script>
<script async src="JAXURL?JAXOPT">
</script>
]]
JAXURL = "https://cdn.rawgit.com/mathjax/MathJax/2.7.1/MathJax.js"
JAXOPT = "config=TeX-AMS_CHTML-full"
if whole_file:match("%$") then -- TODO: perform a more intelligent check here
metadata_maths = true
end
if not metadata_maths then boilerplate_mathjax = "" end
if not whole_file:match("\n%SCRIPT") then boilerplate_lit = "" end
if whole_file:match("<div class=\"gallery\"") then
boilerplate_css = boilerplate_css .. css_gallery
end
boilerplate_mathjax = boilerplate_mathjax:gsub("JAXURL", JAXURL)
boilerplate_mathjax = boilerplate_mathjax:gsub("JAXOPT", JAXOPT)
if not metadata_title then metadata_title = "" end
boilerplate_css = "<style>\n"..boilerplate_css.."\n</style>"
output_template = output_template:gsub("TITLE", metadata_title)
output_template = output_template:gsub("CSSCODE", boilerplate_css)
output_template = output_template:gsub("JAXCODE", boilerplate_mathjax)
output_template = output_template:gsub("LITCODE", boilerplate_lit)
-- start building output file
output_lines = { output_template }
table.insert(output_lines, "\n")
-- initialize counters
counter_sec = 0
counter_ssec = 0
counter_thm = 0
table_depth = 0
toggle_pre = false
o = "" -- the previous line
for l in string.gmatch(whole_file, "[^\n]-\n") do -- traverse line by line
u = l -- line to add to the output (the same, by default)
-- if a paragraph starts, put <p>
if o == "\n" and
not toggle_pre and
not u:match("^%$\\newcommand") and
(
u:match("^[%a][^\n]-\n") or
u:match("^<img[^\n]-\n") or
u:match("^<span[^\n]-\n") or
u:match("^<b>[^\n]-\n") or
u:match("^<i>[^\n]-\n") or
u:match("^<em>[^\n]-\n") or
u:match("^%$[^%$]")
)
then
u = "<p>\n"..u
end
-- build a table context
if o:match("<table" ) then table_depth = table_depth + 1 end
if u:match("</table>") then table_depth = table_depth - 1 end
-- if inside table, change & and \\ to <td> and <tr>, remove \hline
if table_depth > 0 then
u = u:gsub("&", "<td>")
u = u:gsub("\\\\", "<tr><td>")
u = u:gsub("\\hline", "")
end
-- section counters (TODO: add attr name="sec-n" for easier navigation)
if u:match("^<h2>.*$") then
counter_sec = counter_sec + 1
counter_ssec = 0
u = u:gsub("^<h2>", "<h2>"..counter_sec..". ")
end
-- subsection counters
if u:match("^<h3>.*$") then
counter_ssec = counter_ssec + 1
u = u:gsub("^<h3>","<h3>"..counter_sec.."."..counter_ssec..". ")
end
-- theorem counters
if u:match("^<div class=\"thm\">.*%</b>") then
counter_thm = counter_thm + 1
u = u:gsub("%</b>", " "..counter_thm.."</b>")
end
-- <pre> book-keeping
if u:match("^<pre") then toggle_pre = true end
if u:match("^ <pre") then toggle_pre = true end
if u:match("</pre>") then toggle_pre = false end
-- post-processing replacements
if toggle_pre then -- if inside <pre> :
u = u:gsub("%", "%%") -- un-escape html entity for %
end
if u:match("^%%SCRIPT ") then -- if inside valid ^%SCRIPT line :
u = u:gsub("<", "<") -- un-escape html entity for <
u = u:gsub(">", ">") -- un-escape html entity for >
u = u:gsub("%", "%%") -- un-escape html entity for %
elseif not toggle_pre then -- deal with comments
u = u:gsub("^%[^\n]*\n", "") -- remove whole line comment
u = u:gsub("([^\\])%[^\n]*\n", "%1\n") -- midline comment
u = u:gsub("\\%", "\\%%") -- escaped % symbols
end
if o ~= "\n" or u ~= "\n" then -- uniq-ify blank lines
table.insert(output_lines, u)
end
o = l
end
table.insert(output_lines, "\n<hr>\n\n")
--table.insert(output_lines, '\n<!-- yes i know, wanna fight about it ? -->\n')
--tracker = string.format('<img src="%s?a=%s" width="1" height="1" alt="">\n\n',
-- "http://boucantrin.ovh.hw.ipol.im:7743/white_pixel.png",
-- metadata_title:gsub("%s", "_")
-- )
--table.insert(output_lines, tracker)
table.insert(output_lines, "<!-- enric meinhardt-llopis, 2019 -->\n\n")
-- dump output lines
for _,l in ipairs(output_lines) do
io.stdout:write(l)
end