-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspoiler.lua
83 lines (71 loc) · 1.89 KB
/
spoiler.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
local function contains(needle, haystack)
for _, item in ipairs(haystack) do
if item == needle then
return true
end
end
return false
end
local function dump(o)
local function write_indent(indent)
for i = 0, indent do
io.stderr:write(' ')
end
end
local function impl(o, indent)
if o == nil then
return
end
for k, v in pairs(o) do
write_indent(indent)
io.stderr:write(tostring(k) .. ' = ')
if type(v) == 'table' then
io.stderr:write('{\n')
impl(v, indent + 2)
write_indent(indent)
io.stderr:write('}\n')
else
io.stderr:write(tostring(v) .. '\n')
end
end
end
impl(o, 0)
end
local function prepend(list, with)
table.insert(list, 1, with)
end
local function append(list, with)
table.insert(list, with)
end
local function raw(content, format)
format = format or FORMAT
return pandoc.RawBlock(format, content, 'RawBlock')
end
local MARKDOWN_FORMATS = {
'markdown',
'markdown_github',
'markdown_mmd',
'markdown_phpextra',
'markdown_strict',
}
local HTML_FORMATS = {
'html',
'html4',
'html5',
}
function Div(el)
if not el.attr or not contains('spoiler', el.attr.classes) then
io.stderr:write('not spoiler')
return el
end
local title = el.attr.attributes['title'] or 'Спойлер'
local content = el.content
if contains(FORMAT, MARKDOWN_FORMATS) then
prepend(content, raw('<spoiler title="' .. title .. '">', 'html'))
append (content, raw('</spoiler>', 'html'))
elseif contains(FORMAT, HTML_FORMATS) then
prepend(content, raw('<details><summary>' .. title .. '</summary>'))
append (content, raw('</details>'))
end
return content
end