-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-html.lua
More file actions
463 lines (382 loc) · 14.1 KB
/
parse-html.lua
File metadata and controls
463 lines (382 loc) · 14.1 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
function RawBlock(raw)
-- return raw.format:match 'html'
-- and pandoc.read(raw.text, 'html').blocks
-- or raw
-- print(raw.text)
-- print("\n")
-- if raw.text == "<p>" then
-- out = raw.text
-- end
-- return out
-- return raw.text
-- print("Hello \n world")
if raw.text == "\n" then
return ""
else
text = string.gsub(raw.text, "\n\r", "\n\r")
return pandoc.RawInline('markdown', text)
end
end
-- function RawBlock(el)
-- if FORMAT:match 'markdown' and el.format:match 'latex' then
-- print(el)
-- return pandoc.RawInline('markdown', "")
-- else
-- return el
-- end
-- end
function RawInline(raw)
if raw.text == "\n" then
return ""
else
text = string.gsub(raw.text, "\n\r", "\n\r")
return pandoc.RawInline('markdown', text)
end
end
function figure_image(elem)
local image = elem.content and elem.content[1]
return (image.t == "Image" and image.title == 'fig:')
and image
or nil
end
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k, v in pairs(o) do
if type(k) ~= 'number' then k = '"' .. k .. '"' end
s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
-- if it's a figure crossref, change it to the markdown format.
-- I need to figure out how to have figures referenced by numbers...
-- That's normally what pandoc-crossref does.
-- function Cite(citation)
-- cite = citation.content[1].text
-- if cite:sub(0,4) == "@Fig" then
-- string = string.format("[f%s](./#f%s)", cite:sub(3), cite:sub(3))
-- return pandoc.RawInline('markdown', string)
-- end
-- end
-- I need to remove the escape shlash in front of percent signs inside of latex.
-- This function takes the src/ style of figures and converts it into a mkdocs format that supports
-- light and dark mode, and figure captions.
-- function Para(el)
-- print(el.identifier)
-- end
function Para(para)
if para.content[1].identifier and string.sub(para.content[1].identifier, 1, 2) == "eq" then
-- fix equations
-- <a name='table:branches'></a>
-- string = string.format("<a name='%s'></a>", para.content[1].identifier)
-- math = para.content[1].content[1]
local link = string.format("<a name='%s'></a>\n", para.content[1].identifier)
local linkBlock = pandoc.RawBlock("markdown", link)
-- table.insert(para.content.content, 1, linkBlock)
-- local math = para.content[2].content[1]
-- print
-- print(para.content[1].content[1])
-- table.insert(para.content, 1, pandoc.RawInline('markdown', string))
-- table.insert(para.content, pandoc.RawInline('markdown', ''))
-- print(para.content[1])
-- this works
-- print(para.content[1].content[1])
table.insert(para.content[1].content, 1, linkBlock)
table.insert(para.content[1].content, pandoc.RawInline('markdown', '\n'))
return para.content[1].content
end
local img = figure_image(para)
-- if img.attributes['style'] then
-- print(img.attributes['style'])
-- end
if not img or not img.caption or not img.attributes['short-caption'] then
-- print("here?")
return nil
-- if not img.attributes["style"] then
-- return nil
-- else:
-- print(img.attributes["style"])
-- end
end
-- print(img.attributes['style'])
local short_caption = pandoc.Span(
pandoc.read(img.attributes['short-caption']).blocks[1].c
)
-- print(img.attributes['width'])
local hypertarget = "{%%\n"
local label = "\n"
if img.identifier ~= img.title then
hypertarget = string.format("\\hypertarget{%s}{%%\n", img.identifier)
label = string.format("\n\\label{%s}", img.identifier)
end
src_begin = img.src:sub(0, -10)
file_type = img.src:sub(-4) -- could be png or jpg or svg. would break with jpeg
check_light = img.src:sub(-9, -5)
-- print(type(img.caption))
-- caption = pandoc.utils.stringify(Maths2Markdown(pandoc.Span(img.caption)))
caption = pandoc.Span(img.caption)
short_caption = pandoc.utils.stringify(short_caption)
-- inject markdown that works inside the html environment
-- see: https://squidfunk.github.io/mkdocs-material/reference/images/#light-and-dark-mode
-- light_src = string.format("", img.identifier, src_begin)
-- dark_src = string.format("", img.identifier, src_begin)
-- switch to injecting raw html
html_style = string.format("style=\"width: %s; margin: auto;\"", img.attributes['width'])
light_src = string.format("<img alt=\"%s\" %s src=\".%slight%s#only-light\" >", img.identifier, html_style, src_begin, file_type)
dark_src = string.format("<img alt=\"%s\" %s src=\".%sdark%s#only-dark\" >", img.identifier, html_style, src_begin, file_type)
if check_light == "light" then
full_src = string.format("%s\n %s", light_src, dark_src)
else
-- if light/dark options don't seem present, just use the src as is.
full_src = string.format("", img.identifier, img.src)
end
-- print(caption)
-- print(type(caption))
-- string = string.format("<figure markdown> \
-- <a name='%s'></a> \
-- %s \
-- <figcaption markdown>%s</figcaption> \
-- </figure>", img.identifier, full_src, caption)
string = string.format("<figure markdown> \
<a name='%s'></a> \
%s \
<figcaption markdown> ", img.identifier, full_src)
return pandoc.Para { pandoc.RawInline('markdown', string), caption, pandoc.RawInline('markdown', "</figcaption>\
</figure>") }
end
function Maths2Markdown(sp)
-- print(type(sp))
-- for i, thing in ipairs(sp) do
-- print(thing)
-- end
-- print(sp.content[15])
-- print(#sp.content)
for i, thing in ipairs(sp.content) do
-- print(type(thing))
-- print(thing)
-- print(thing.t)
-- print(thing)
if thing.t == "Math" then
print(thing.text)
thing.t = "Str"
thing.text = string.format("$%s$", thing.text)
print(thing.text)
end
end
return sp
end
-- math-printer.lua
-- function Math(el)
-- -- el.t can be 'Math', 'InlineMath', or 'DisplayMath'
-- -- el.text is the LaTeX code
-- print(el.text)
-- -- Return the element unchanged
-- return el
-- end
-- math-label-capture.lua
-- function Div(el)
-- -- Check if the Div contains a math block
-- if #el.content > 0 and el.content[1].t == "Math" then
-- -- Print the label of the math block
-- print(el.identifier)
-- end
-- -- Return the element unchanged
-- return el
-- end
-- This takes the native pandoc representation of Divs and outputs an html div with the markdown data attribute
-- For some reason, pandoc was stripping the markdown data attribute when deactivating the native_divs extension
function Div(el)
-- this is useful for parsing divs: https://github.com/pandoc/lua-filters/blob/master/list-table/list-table.lua
-- print(el.classes[1])
-- print(el.content[1].t)
-- print(el.content[1].content[1].t)
-- color = el.attributes['color']
-- -- if no color attribute, return unchanged
-- if color ~= nil then
-- print(color)
-- end
-- print("")
-- success, color = pcall(function() return el.attributes['color'] end)
-- print(el.content[1])
-- if el.content[1].t == "Para" then
-- if success then
-- string = string.format("<div class=%s markdown>\n\n", color)
-- table.insert(el.content, 1, pandoc.RawInline('markdown', string))
-- table.insert(el.content, pandoc.RawInline('markdown', '</div>'))
-- return el.content
-- end
-- if
if el.classes[1] == "latex" then
return pandoc.RawInline('markdown', "")
end
if el.attributes.style then
local style = el.attributes.style
local color = style:match("color:%s*([^;]+)")
if color then
string = string.format("<div class=%s markdown>\n\n", color)
table.insert(el.content, 1, pandoc.RawInline('markdown', string))
table.insert(el.content, pandoc.RawInline('markdown', '</div>'))
return el.content
end
end
if el.content[1].t == "Para" then
if el.content[1].content[1].t == "Math" then
string = string.format("<div class=%s markdown>\n\n$$%s$$\n\n</div>", el.classes[1],
el.content[1].content[1].text)
-- print(el.content[1].content[1].text)
return pandoc.RawInline('markdown', string)
end
end
return el
end
function Span(el)
-- <span class="bokeh" id="../code/test_1"></span>
if el.classes:includes("bokeh") then
local path = el.attributes.path
string = string.format("<span class='bokeh' id='%s'></span>", path)
return pandoc.RawInline('markdown', string)
end
-- <span class="html">This is some html</span>
-- pass through unmodified spans marked "html"
if el.classes[1] == "html" then
-- print(el.content[1].text)
return el.content
end
if el.classes[1] == "html_lineskip" then
-- print(el.content[1].text)
el.content[1].text = "\n" .. el.content[1].text
return el.content
end
-- ignore stuff like <span class="latex"></span> that's only for latex output
if el.classes[1] == "latex" then
-- print(el)
-- print("NOT including: ", el.content)
return pandoc.RawInline('markdown', "")
end
-- switch style tags for colors to classes (for light/dark support)
if el.attributes.style then
local style = el.attributes.style
-- print(style)
local color = style:match("color:%s*([^;]+)")
-- print(color)
if color then
string = string.format("<span class=%s markdown>", color)
table.insert(el.content, 1, pandoc.RawInline('markdown', string))
table.insert(el.content, pandoc.RawInline('markdown', '</span>'))
return el.content
end
end
-- success, color = pcall(function() return el.attributes['color'] end)
-- print(el.content[1])
-- if el.content[1].t == "Para" then
end
function Note (note)
--- this can capture footnotes
-- you might be able to customize this for turning footnotes into material mkdocs annotations
local text = pandoc.utils.stringify(note.content)
return note
end
-- function Span(el)
-- -- print("dsf")
-- color = el.attributes['color']
-- -- if no color attribute, return unchanged
-- if color ~= nil then
-- print(color)
-- string = string.format("<span class=%s markdown></div>", el.classes[1],
-- el.content[1].content[1].text)
-- return pandoc.RawInline('markdown', string)
-- end
-- end
-- -- converts things like <span color=red>This text is red </span>
-- Span = function(el)
-- color = el.attributes['color']
-- -- if no color attribute, return unchanged
-- if color == nil then return el end
-- -- transform to <span style="color: red;"></span>
-- if FORMAT:match 'html' then
-- -- remove color attributes
-- el.attributes['color'] = nil
-- -- use style attribute instead
-- el.attributes['style'] = 'class: ' .. color .. ';'
-- -- return full span element
-- return el
-- elseif FORMAT:match 'latex' then
-- -- remove color attributes
-- el.attributes['color'] = nil
-- -- encapsulate in latex code
-- table.insert(
-- el.content, 1,
-- pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
-- )
-- table.insert(
-- el.content,
-- pandoc.RawInline('latex', '}')
-- )
-- return el.content
-- else
-- -- for other format return unchanged
-- return el
-- end
-- end
-- Span = function(el)
-- color = el.attributes['color']
-- -- if no color attribute, return unchanged
-- if color == nil then return el end
-- -- transform to <span style="color: red;"></span>
-- if FORMAT:match 'html' then
-- -- remove color attributes
-- el.attributes['color'] = nil
-- -- use style attribute instead
-- el.attributes['style'] = 'color: ' .. color .. ';'
-- -- return full span element
-- return el
-- elseif FORMAT:match 'latex' then
-- -- remove color attributes
-- el.attributes['color'] = nil
-- -- encapsulate in latex code
-- table.insert(
-- el.content, 1,
-- pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
-- )
-- table.insert(
-- el.content,
-- pandoc.RawInline('latex', '}')
-- )
-- return el.content
-- else
-- -- for other format return unchanged
-- return el
-- end
-- end
-- img.identifier is cryostat_concept
-- <figure markdown>
-- <a name="histogram_1"></a>
-- 
-- 
-- <figcaption>Image caption</figcaption>
-- </figure>
-- function serializeTable(val, name, skipnewlines, depth)
-- skipnewlines = skipnewlines or false
-- depth = depth or 0
-- local tmp = string.rep(" ", depth)
-- if name then tmp = tmp .. name .. " = " end
-- if type(val) == "table" then
-- tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
-- for k, v in pairs(val) do
-- tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
-- end
-- tmp = tmp .. string.rep(" ", depth) .. "}"
-- elseif type(val) == "number" then
-- tmp = tmp .. tostring(val)
-- elseif type(val) == "string" then
-- tmp = tmp .. string.format("%q", val)
-- elseif type(val) == "boolean" then
-- tmp = tmp .. (val and "true" or "false")
-- else
-- tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
-- end
-- return tmp
-- end