-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki-addon.lua
205 lines (180 loc) · 4.84 KB
/
anki-addon.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
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
Writer = pandoc.scaffolding.Writer
-- Anki addon respected tags: img, a, b, i, code, ul, ol, li.
Writer.Inline.Image = function (nd)
local attr = ""
if nd.caption ~= nil then
attr = attr .. '" alt="' .. escape(pandoc.utils.stringify(nd.caption), true) .. '"'
end
if nd.attributes ~= nil then
for k, v in pairs(nd.attributes) do
attr = attr .. ' ' .. k .. '="' .. v .. '"'
end
end
return "<img src=\"" .. escape(nd.src, true) .. attr .. "/>"
end
Writer.Inline.Link = function (nd)
return "<a href=\"" .. escape(nd.target, true) .. "\">"
.. Writer.Inlines(nd.content) .. "</a>"
end
Writer.Inline.Strong = function (nd)
return "<b>" .. Writer.Inlines(nd.content) .. "</b>"
end
Writer.Inline.Emph = function (nd)
return "<i>" .. Writer.Inlines(nd.content) .. "</i>"
end
Writer.Inline.Cite = function (nd)
return '"' .. Writer.Inlines(nd.content) .. '"'
end
Writer.Inline.Code = function (nd)
return "<code>" .. escape(nd.text, false) .. "</code>"
end
Writer.Block.CodeBlock = function (nd)
-- local str = string.sub(nd.text, string.find(nd.text, "\n") + 1)
return "\n<code>" .. escape(str, false) .. "</code>\n"
-- return "<code>" .. escape(nd.text, false) .. "</code>"
end
Writer.Block.BulletList = function (nd)
local str = "<ul>"
for i, itm in pairs(nd.content) do
str = str .. "<li>" .. Writer.Blocks(itm) .. "</li>"
end
return str .. "</ul>"
end
Writer.Block.OrderedList = function (nd)
local str = "<ol>"
for i, itm in pairs(nd.content) do
str = str .. "<li>" .. Writer.Blocks(itm) .. "</li>"
end
return str .. "</ol>"
end
-- Override block spacing for HTML as input keeps `\n`
-- Block types:
-- BlockQuote: no space
-- BulletList: no space
-- CodeBlock: no space
-- OrderedList: no space
-- Plain: no space
-- Para: space
-- RawBlock: space
-- DefinitionList: space
-- Div: space
-- Figure: space
-- Header: space
-- HorizontalRule: space
-- LineBlock: space
-- Table: space
Writer.Blocks = function (nd)
local function spacing(cur, nxt)
local function lookup (nd_)
local space = {
['CodeBlock'] = 2,
['BlockQuote'] = 0,
['BulletList'] = 0,
['OrderedList'] = 0,
['Plain'] = 0,
['Header'] = 1,
['Para'] = 2,
['RawBlock'] = 2,
['DefinitionList'] = 2,
['Div'] = 2,
['Figure'] = 2,
['HorizontalRule'] = 2,
['LineBlock'] = 2,
['Table'] = 2
}
if nd_ == nil then
return 0
end
for key, val in pairs(space) do
if key == nd_ then
return val
end
end
return 2
end
local curs = lookup(cur)
local nxts = lookup(nxt)
if curs == 0 or nxts == 0 then
return ''
elseif curs == 1 then
return '\n'
else
return '\n\n'
end
end
local str = ""
local prev = nil
for _, itm in pairs(nd) do
str = str .. spacing(prev, itm.tag) .. Writer.Block(itm)
prev = itm.tag
end
return str
end
-- Reasonable rendering of markdown input
Writer.Inline.Str = function (nd)
return escape(nd.text, false)
end
Writer.Inline.Space = function (nd)
return " "
end
Writer.Block.Plain = function (nd)
return Writer.Inlines(nd.content)
end
Writer.Block.Para = function (nd)
return Writer.Inlines(nd.content)
end
Writer.Inline.Quoted = function (nd)
local q = '"'
if nd.quotetype == 'SingleQuote' then
q = "'"
end
return q .. Writer.Inlines(nd.content) .. q
end
Writer.Block.BlockQuote = function (nd)
return "<code>" .. Writer.Inlines(nd.content) .. "</code>"
end
Writer.Block.Header = function (nd)
local str = escape(pandoc.utils.stringify(nd.content), false)
if nd.level == 1 then
return "<b>" .. pandoc.text.upper(str) .. "</b>"
elseif nd.level == 2 then
return "<b>" .. pandoc.text.upper(str) .. "</b>"
elseif nd.level == 3 then
return "<b>" .. str .. "</b>"
elseif nd.level == 4 then
return "<b><i>" .. str .. "</i></b>"
elseif nd.level == 5 then
return "<i>" .. str .. "</i>"
else
return str
end
end
Writer.Block.Figure = function (nd)
local str = ""
for _, itm in pairs(nd.content) do
str = str .. Writer.Block(itm)
end
return str
end
Writer.Inline.SoftBreak = function (nd)
return " "
end
-- Character escaping
function escape(s, in_attribute)
return s:gsub('[<>&"\']',
function(x)
if x == '<' then
return '<'
elseif x == '>' then
return '>'
elseif x == '&' then
return '&'
elseif in_attribute and x == '"' then
return '"'
elseif in_attribute and x == "'" then
return '''
else
return x
end
end)
end