forked from lilydjwg/2bbcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbcode.lua
183 lines (150 loc) · 3.87 KB
/
bbcode.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
-- Invoke with: pandoc -t sample.lua
-- Blocksep is used to separate block elements.
function Blocksep()
return "\n\n"
end
-- This function is called once for the whole document. Parameters:
-- body, title, date are strings; authors is an array of strings;
-- variables is a table. One could use some kind of templating
-- system here; this just gives you a simple standalone HTML file.
function Doc(body, title, authors, date, variables)
return body .. '\n'
end
-- The functions that follow render corresponding pandoc elements.
-- s is always a string, attr is always a table of attributes, and
-- items is always an array of strings (the items in a list).
-- Comments indicate the types of other variables.
function Str(s)
return s
end
function Space()
return " "
end
function LineBreak()
return "\n"
end
function Emph(s)
return "[em]" .. s .. "[/em]"
end
function Strong(s)
return "[b]" .. s .. "[/b]"
end
function Subscript(s)
error("Subscript isn't supported")
end
function Superscript(s)
return s
end
function SmallCaps(s)
error("SmallCaps isn't supported")
end
function Strikeout(s)
return '[del]' .. s .. '[/del]'
end
function Link(s, src, tit)
local ret = '[url'
if s then
ret = ret .. '=' .. src
else
s = src
end
ret = ret .. "]" .. s .. "[/url]"
return ret
end
function Image(s, src, tit)
return "[img=" .. tit .. "]" .. src .. "[/img]"
end
function Code(s, attr)
return "[u]" .. s .. "[/u]"
end
function InlineMath(s)
error("InlineMath isn't supported")
end
function DisplayMath(s)
error("DisplayMath isn't supported")
end
function Note(s)
error("Note isn't supported")
end
function Plain(s)
return s
end
function Para(s)
return s
end
-- lev is an integer, the header level.
function Header(lev, s, attr)
return "[h]" .. s .. "[/h]"
end
function BlockQuote(s)
return "[quote]\n" .. s .. "\n[/quote]"
end
function HorizontalRule()
return "--------------------------------------------------------------------------------"
end
function CodeBlock(s, attr)
return "[code]\n" .. s .. '\n[/code]'
end
function BulletList(items)
local buffer = {}
for _, item in ipairs(items) do
table.insert(buffer, "[*]" .. item)
end
return "[list]\n" .. table.concat(buffer, "\n") .. "\n[/list]"
end
function OrderedList(items)
local buffer = {}
for _, item in ipairs(items) do
table.insert(buffer, "[*]" .. item)
end
return "[list=1]\n" .. table.concat(buffer, "\n") .. "\n[/list]"
end
-- Revisit association list STackValue instance.
function DefinitionList(items)
local buffer = {}
for _, item in pairs(items) do
for k, v in pairs(item) do
table.insert(buffer, "[b]" .. k .. "[/b]:\n" ..
table.concat(v, "\n"))
end
end
return table.concat(buffer, "\n")
end
-- Convert pandoc alignment to something HTML can use.
-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
function html_align(align)
if align == 'AlignLeft' then
return 'left'
elseif align == 'AlignRight' then
return 'right'
elseif align == 'AlignCenter' then
return 'center'
else
return 'left'
end
end
-- Caption is a string, aligns is an array of strings,
-- widths is an array of floats, headers is an array of
-- strings, rows is an array of arrays of strings.
function Table(caption, aligns, widths, headers, rows)
error("Table isn't supported")
end
function RawBlock(format, str)
return '[code]\n' .. str .. '\n[/code]\n'
end
function Span(s, attr)
return s
end
function Div(s, attr)
return s .. '\n'
end
-- The following code will produce runtime warnings when you haven't defined
-- all of the functions you need for the custom writer, so it's useful
-- to include when you're working on a writer.
local meta = {}
meta.__index =
function(_, key)
io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
return function() return "" end
end
setmetatable(_G, meta)