-
Notifications
You must be signed in to change notification settings - Fork 2
/
gemini.lua
137 lines (130 loc) · 3.24 KB
/
gemini.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
-- final output:
local blocks = {}
function inner_text(el)
local fullText = ""
if (el.content) then
for i, v in ipairs(el.content) do
if (v.content) then
fullText = fullText .. inner_text(v)
end
if (v.text == nil) then
fullText = fullText .. " "
else
fullText = fullText .. v.text
end
end
else
if (el.text) then
fullText = fullText .. el.text
end
end
return fullText
end
-- add a block to the 'blocks' output:
function add_block(el)
table.insert(blocks, el)
end
function debug(el, level)
-- print(type(el))
level = level or 1
local prefix = ""
for i = 1, level do
prefix = prefix .. " "
end
if (el.tag) then
print(prefix .. el.tag .. ": " .. inner_text(el))
end
if (el.content) then
level = level + 1
debug(el.content, level)
end
end
function link_filter()
return {
Link = function(el)
-- print(el.tag .. " " .. el.target)
return {
pandoc.LineBreak(),
pandoc.Str("=> " .. el.target .. " " .. inner_text(el) ),
pandoc.LineBreak(),
}
end,
}
end
function handle_block (el)
-- print(el.tag)
if (el.tag == "Header") then
if (el.level > 3) then
el.level = 3
end
local prefix = ""
for i = 1, el.level do
prefix = prefix .. "#"
end
if (el.level <= 3) then
add_block(pandoc.Plain(prefix .. " " .. inner_text(el)))
end
return
end
if (el.tag == "HorizontalRule") then
add_block(pandoc.Para("--------------------------------------------------------------------------------"))
return
end
if (el.tag == "Link") then
add_block(pandoc.Para(pandoc.Str("=> " .. el.target .. " " .. inner_text(el) )))
return
end
if (el.tag == "Str") then
add_block(pandoc.Para(el))
return
end
if (el.tag == "OrderedList") then
for i,v in ipairs(el.content) do
local listItem = {}
table.insert(listItem, pandoc.Str("* "))
for j, item in ipairs(v) do
local block_with_links = pandoc.walk_block(item, link_filter())
local inlines = pandoc.utils.blocks_to_inlines({ block_with_links })
for index,inlineElement in ipairs(inlines) do
table.insert(listItem, inlineElement)
end
end
add_block(pandoc.Para(listItem))
end
return
end
if(el.tag == "Para") then
local block_with_links = pandoc.walk_block(el, link_filter())
add_block(block_with_links)
return
end
if (el.tag == "BulletList") then
for i,v in ipairs(el.content) do
local listItem = {}
table.insert(listItem, pandoc.Str("* "))
for j, item in ipairs(v) do
local block_with_links = pandoc.walk_block(item, link_filter())
local inlines = pandoc.utils.blocks_to_inlines({ block_with_links })
for index,inlineElement in ipairs(inlines) do
table.insert(listItem, inlineElement)
end
end
add_block(pandoc.Para(listItem))
end
return
end
if (el.content and el.tag ~= "Link") then
for i, child in ipairs(el.content) do
handle_block(child)
end
end
end
function flatten_document (doc)
for i,el in ipairs(doc.blocks) do
handle_block(el)
end
return pandoc.Pandoc(blocks, doc.meta)
end
return {
{ Pandoc = flatten_document },
}