forked from tajmone/2bbcode
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbcode_phpbb.lua
More file actions
Latest commit
334 lines (281 loc) · 10.3 KB
/
Copy pathbbcode_phpbb.lua
File metadata and controls
334 lines (281 loc) · 10.3 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
--[[ ******************************************************************************
* *
* Pandoc 2 BBCode for phpBB *
* *
******************************************************************************
"bbcode_phpbb.lua" v1.1 (2016-12-20)
adapted by Tristano Ajmone (@tajmone):
-- https://github.com/tajmone/2bbcode
------------------------------------------------------------------------------
This code was forked by Tristano Ajmone from @lilydjwg's `2bbcode.lua`:
-- https://github.com/lilydjwg/2bbcode
Copyright (c) 2016, @lilydjwg (依云), all rights reserved.
Released under BSD 3-Clause License:
-- https://github.com/lilydjwg/2bbcode/blob/master/LICENSE
==============================================================================
BSD 3-Clause License
==============================================================================
Copyright (c) 2016, 依云
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
==============================================================================]]
-- Invoke with: pandoc -t bbcode_phpbb.lua
-- Blocksep is used to separate block elements.
functionBlocksep()
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.
functionDoc(body, title, authors, date, variables)
returnbody..'\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.
functionStr(s)
returns
end
functionSpace()
return""
end
functionLineBreak()
return"\n"
end
-- CHANGES BY @tajmone: Emphasis
-- * Convert to Italic [i] instead of Emphasis [em]
functionEmph(s)
return"[i]" ..s.."[/i]"
end
functionStrong(s)
return"[b]" ..s.."[/b]"
end
functionSubscript(s)
error("Subscript isn't supported")
end
functionSuperscript(s)
returns
end
functionSmallCaps(s)
error("SmallCaps isn't supported")
end
-- CHANGES BY @tajmone: Strikeout
-- * Convert to Underline [u] instead of Strikeout/Strikethrough [del]
functionStrikeout(s)
return'[u]' ..s..'[/u]'
end
functionLink(s, src, tit)
localret='[url'
ifsthen
ret=ret..'=' ..src
else
s=src
end
ret=ret.."]" ..s.."[/url]"
returnret
end
-- CHANGES BY @tajmone: CaptionedImage
-- * Added CaptionedImage function (missing in original)
-- Caused error for GFM images with Alt text (even if Alt was empty)
functionCaptionedImage(src, tit, caption, attr)
return"[img]" ..src.."[/img]"
end
functionImage(s, src, tit)
return"[img=" ..tit.."]" ..src.."[/img]"
end
-- CHANGES BY @tajmone: Inline Code
-- * Convert to bold and red.
functionCode(s, attr)
return"[b][color=#BF2700]" ..s.."[/color][/b]"
end
functionInlineMath(s)
error("InlineMath isn't supported")
end
functionDisplayMath(s)
error("DisplayMath isn't supported")
end
functionNote(s)
error("Note isn't supported")
end
functionPlain(s)
returns
end
functionPara(s)
returns
end
-- CHANGES BY @tajmone: Headers 1-6
-- * Don't use [h] (unsupported in phpBB)
-- * Convert to different sizes and colors, always bold.
-- * Higher order headers have bigger font size and more contrasting color.
-- lev is an integer, the header level.
functionHeader(lev, s, attr)
iflev==1then
return"[size=200][color=#BF2700][b]" ..s.."[/b][/color][/size]"
elseiflev==2then
return"[size=180][color=#002D97][b]" ..s.."[/b][/color][/size]"
elseiflev==3then
return"[size=160][color=#006600][b]" ..s.."[/b][/color][/size]"
elseiflev==4then
return"[size=150][color=#0C71A3][b]" ..s.."[/b][/color][/size]"
elseiflev==5then
return"[size=140][color=#A9B8C2][b]" ..s.."[/b][/color][/size]"
else
return"[size=130][b]" ..s.."[/b][/size]"
end
end
functionBlockQuote(s)
return"[quote]\n" ..s.."\n[/quote]"
end
functionHorizontalRule()
return"--------------------------------------------------------------------------------"
end
functionLineBlock(ls)
returntable.concat(ls, '\n')
end
functionCodeBlock(s, attr)
return"[code]\n" ..s..'\n[/code]'
end
functionBulletList(items)
localbuffer= {}
for_, iteminipairs(items) do
table.insert(buffer, "[*]" ..item)
end
return"[list]\n" ..table.concat(buffer, "\n") .."\n[/list]"
end
functionOrderedList(items)
localbuffer= {}
for_, iteminipairs(items) do
table.insert(buffer, "[*]" ..item)
end
return"[list=1]\n" ..table.concat(buffer, "\n") .."\n[/list]"
end
-- Revisit association list STackValue instance.
functionDefinitionList(items)
localbuffer= {}
for_, iteminpairs(items) do
fork, vinpairs(item) do
table.insert(buffer, "[b]" ..k.."[/b]:\n" ..
table.concat(v, "\n"))
end
end
returntable.concat(buffer, "\n")
end
-- Convert pandoc alignment to something HTML can use.
-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
functionhtml_align(align)
ifalign=='AlignLeft' then
return'left'
elseifalign=='AlignRight' then
return'right'
elseifalign=='AlignCenter' then
return'center'
else
return'left'
end
end
-- CHANGES BY @tajmone: Table
-- * Now presence of a Table in input doesn't throw an error and abort,
-- it just returns empty string, suppressing the table in converted output!
-- * Warning is printed to STDERR showing the omitted Table's headers
-- (so user can understand what was left out).
-- 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.
functionTable(caption, aligns, widths, headers, rows)
localtmpstr='| '
fori, hinpairs(headers) do
tmpstr=tmpstr..h..' | '
end
PrintWarning('Table omitted: ' ..tmpstr)
return''
-- error("Table isn't supported")
end
-- CHANGES BY @tajmone: RawBlock
-- * Suppress from output if it's raw HTML
-- (Even html comments were converted to [code], and Markdown TOC-tags would show up in final BBCode)
-- * Warning is printed to STDERR with suppresed HTML
functionRawBlock(format, str)
ifformat=="html" then
PrintWarning('Raw HTML omitted: ' ..str)
return''
else
return'[code]\n' ..str..'\n[/code]\n'
end
end
functionSpan(s, attr)
returns
end
functionDiv(s, attr)
returns..'\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.
localmeta= {}
meta.__index=
function(_, key)
io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
returnfunction() return"" end
end
setmetatable(_G, meta)
-- CHANGES BY @tajmone: DoubleQuoted
-- * Added DoubleQuoted function (missing in original)
-- Caused error for text within double quotes
-- * Returns text in nice UTF-8 curly double quotes (always)
functionDoubleQuoted(s)
return"“" ..s..'”'
end
-- CHANGES BY @tajmone: SingleQuoted
-- * Added SingleQuoted function (missing in original)
-- Caused error for text within single quotes
-- * Returns text in nice UTF-8 curly single quotes (always)
functionSingleQuoted(s)
return"‘" ..s..'’'
end
-- CHANGES BY @tajmone: SoftBreak
-- * Added SoftBreak function (missing in original)
-- This element was added in pandoc 1.16 as a matter of editing convenience
-- to preserve line breaks (as opposed to paragraph breaks) from input source to output.
-- * Returns text followed by \n
functionSoftBreak()
return"\n"
end
-- CHANGES BY @tajmone: PrintWarning function
-- * I've added this function to warn the user (on STDERR) when
-- some BBCode-unsupported input is suppressed from output.
functionPrintWarning(s)
io.stderr:write("WARNING! " ..s..'\n')
end
--[[
==============================================================================
FILE HISTORY
==============================================================================
v1.1 - 2016/12/20
- Bug Fix: Added missing fucntions:
- DoubleQuoted()
- SingleQuoted()
- SoftBreak()
v1.0 - 2016/12/10
- Forked from `2bbcode.lua` and created 1st release.
]]