12
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local type = Luan.type or error()
|
|
4 local ipairs = Luan.ipairs or error()
|
28
|
5 local pairs = Luan.pairs or error()
|
12
|
6 local stringify = Luan.stringify or error()
|
13
|
7 local Io = require "luan:Io.luan"
|
|
8 local output_of = Io.output_of or error()
|
12
|
9 local Parsers = require "luan:Parsers.luan"
|
|
10 local bbcode_parse = Parsers.bbcode_parse or error()
|
|
11 local Html = require "luan:Html.luan"
|
|
12 local html_encode = Html.encode or error()
|
21
|
13 local html_parse = Html.parse or error()
|
12
|
14 local Table = require "luan:Table.luan"
|
|
15 local is_list = Table.is_list or error()
|
21
|
16 local concat = Table.concat or error()
|
13
|
17 local String = require "luan:String.luan"
|
29
|
18 local regex = String.regex or error()
|
|
19 local ends_with = String.ends_with or error()
|
12
|
20 local User = require "site:/lib/User.luan"
|
13
|
21 local Shared = require "site:/lib/Shared.luan"
|
|
22 local list_to_set = Shared.list_to_set or error()
|
20
|
23 local to_list = Shared.to_list or error()
|
12
|
24 local Logging = require "luan:logging/Logging.luan"
|
|
25 local logger = Logging.logger "Bbcode"
|
|
26
|
|
27
|
|
28 local Bbcode = {}
|
|
29
|
29
|
30 local starting_cr_regex = regex[[^\n]]
|
|
31
|
12
|
32 local to_html
|
|
33 local html = {}
|
|
34
|
20
|
35 function html.b(bbcode,options)
|
|
36 %><b><% to_html(bbcode.contents,options) %></b><%
|
|
37 end
|
|
38
|
|
39 function html.i(bbcode,options)
|
|
40 %><i><% to_html(bbcode.contents,options) %></i><%
|
|
41 end
|
|
42
|
|
43 function html.u(bbcode,options)
|
|
44 %><u><% to_html(bbcode.contents,options) %></u><%
|
12
|
45 end
|
|
46
|
20
|
47 function html.s(bbcode,options)
|
|
48 %><s><% to_html(bbcode.contents,options) %></s><%
|
12
|
49 end
|
|
50
|
20
|
51 function html.sup(bbcode,options)
|
|
52 %><sup><% to_html(bbcode.contents,options) %></sup><%
|
12
|
53 end
|
|
54
|
20
|
55 function html.brackets(bbcode,options)
|
|
56 %>[<% to_html(bbcode.contents,options) %>]<%
|
|
57 end
|
|
58
|
|
59 function html.url(bbcode,options)
|
12
|
60 local url = bbcode.param
|
|
61 if url == nil then
|
|
62 url = html_encode(bbcode.contents)
|
|
63 %><a href="<%=url%>"><%=url%></a><%
|
|
64 else
|
|
65 url = html_encode(url)
|
20
|
66 %><a href="<%=url%>"><% to_html(bbcode.contents,options) %></a><%
|
12
|
67 end
|
|
68 end
|
|
69
|
20
|
70 function html.code(bbcode,options)
|
29
|
71 local s = starting_cr_regex.gsub(bbcode.contents,"")
|
20
|
72 %><code><%= html_encode(s) %></code><%
|
|
73 options.strip_newline = true
|
12
|
74 end
|
|
75
|
20
|
76 function html.img(bbcode,options)
|
12
|
77 %><img src="<%= html_encode(bbcode.contents) %>"><%
|
|
78 end
|
|
79
|
20
|
80 function html.color(bbcode,options)
|
|
81 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><%
|
12
|
82 end
|
|
83
|
20
|
84 function html.size(bbcode,options)
|
23
|
85 %><span style="font-size:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><%
|
12
|
86 end
|
|
87
|
20
|
88 function html.quote(bbcode,options)
|
12
|
89 %><blockquote><%
|
|
90 local user_name = bbcode.param
|
|
91 if user_name ~= nil then
|
|
92 local user = User.get_by_name(user_name)
|
|
93 if user == nil then
|
|
94 %><%= user_name %> wrote:<%
|
|
95 else
|
|
96 %><a href="/user_something"><%= user_name %></a> wrote:<%
|
|
97 end
|
20
|
98 else
|
|
99 options.strip_newline = true
|
12
|
100 end
|
20
|
101 to_html(bbcode.contents,options)
|
12
|
102 %></blockquote><%
|
20
|
103 options.strip_newline = true
|
12
|
104 end
|
|
105
|
28
|
106 local function video_iframe(url)
|
|
107 %><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><%
|
|
108 end
|
|
109
|
|
110 local video_handlers = {}
|
|
111 do
|
30
|
112 local ptn1 = regex[[^\Qhttps://youtu.be/\E([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
|
|
113 local ptn2 = regex[[^\Qhttps://www.youtube.com/watch?v=\E([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]]
|
28
|
114 function video_handlers.youtube(url)
|
29
|
115 local id, start = ptn1.match(url)
|
28
|
116 if id == nil then
|
29
|
117 id, start = ptn2.match(url)
|
28
|
118 end
|
|
119 if id == nil then
|
|
120 return false
|
|
121 end
|
|
122 url = "https://www.youtube.com/embed/"..id
|
|
123 if start ~= nil then
|
|
124 url = url.."?start="..start
|
|
125 end
|
|
126 video_iframe(url)
|
|
127 return true
|
|
128 end
|
|
129 end
|
|
130 do
|
30
|
131 local ptn = regex[[^\Qhttps://rumble.com/embed/\E[a-z0-9]+/\?pub=[a-z0-9]+]]
|
28
|
132 function video_handlers.rumble(url)
|
29
|
133 if not ptn.matches(url) then
|
28
|
134 return false
|
|
135 end
|
|
136 video_iframe(url)
|
|
137 return true
|
|
138 end
|
|
139 end
|
|
140 do
|
30
|
141 local ptn = regex[[^\Qhttps://www.bitchute.com/video/\E([a-zA-Z0-9]+)/]]
|
28
|
142 function video_handlers.bitchute(url)
|
29
|
143 local id = ptn.match(url)
|
28
|
144 if id == nil then
|
|
145 return false
|
|
146 end
|
|
147 url = "https://www.bitchute.com/embed/"..id.."/"
|
|
148 video_iframe(url)
|
|
149 return true
|
|
150 end
|
|
151 end
|
|
152 do
|
30
|
153 local ptn = regex[[^\Qhttps://vimeo.com/\E([0-9]+)]]
|
28
|
154 function video_handlers.vimeo(url)
|
29
|
155 local id = ptn.match(url)
|
28
|
156 if id == nil then
|
|
157 return false
|
|
158 end
|
|
159 url = "https://player.vimeo.com/video/"..id
|
|
160 video_iframe(url)
|
|
161 return true
|
|
162 end
|
|
163 end
|
|
164 do
|
30
|
165 local ptn = regex[[^\Qhttps://dai.ly/\E([a-z0-9]+)]]
|
28
|
166 function video_handlers.dailymotion(url)
|
29
|
167 local id = ptn.match(url)
|
28
|
168 if id == nil then
|
|
169 return false
|
|
170 end
|
|
171 url = "https://www.dailymotion.com/embed/video/"..id
|
|
172 video_iframe(url)
|
|
173 return true
|
|
174 end
|
|
175 end
|
|
176 do
|
30
|
177 local ptn = regex[[^\Qhttps://www.tiktok.com/\E[^/]+/video/([0-9]+)]]
|
28
|
178 function video_handlers.tiktok(url)
|
29
|
179 local id = ptn.match(url)
|
28
|
180 if id == nil then
|
|
181 return false
|
|
182 end
|
|
183 %><blockquote class="tiktok-embed" data-video-id="<%=id%>" style="max-width: 560px; margin-left: 0;"><section></section></blockquote><%
|
|
184 %><script async src="https://www.tiktok.com/embed.js"></script><%
|
|
185 return true
|
|
186 end
|
|
187 end
|
|
188 do
|
29
|
189 local ptn = regex[[\.[a-zA-Z0-9]+$]]
|
28
|
190 function video_handlers.file(url)
|
29
|
191 if not ptn.matches(url) then
|
28
|
192 return false
|
|
193 end
|
|
194 %><video controls width="560"><source src="<%=html_encode(url)%>"></video><%
|
|
195 return true
|
|
196 end
|
|
197 end
|
23
|
198
|
20
|
199 function html.video(bbcode,options)
|
23
|
200 local url = bbcode.contents
|
28
|
201 for _, handle in pairs(video_handlers) do
|
|
202 if handle(url) then return end
|
27
|
203 end
|
23
|
204 url = html_encode(url)
|
|
205 %><a href="<%=url%>"><%=url%></a><%
|
12
|
206 end
|
|
207
|
20
|
208 local function list_to_html(bbcode,options)
|
|
209 local list = to_list(bbcode.contents)
|
|
210 for _, item in ipairs(list) do
|
|
211 if type(item) == "table" and item.name == "li" then
|
|
212 %><li><% to_html(item.contents,options) %></li><%
|
|
213 end
|
|
214 end
|
|
215 options.strip_newline = true
|
|
216 end
|
|
217
|
|
218 function html.ul(bbcode,options)
|
|
219 %><ul><%
|
|
220 list_to_html(bbcode,options)
|
|
221 %></ul><%
|
|
222 end
|
|
223
|
|
224 function html.ol(bbcode,options)
|
|
225 %><ol><%
|
|
226 list_to_html(bbcode,options)
|
|
227 %></ol><%
|
|
228 end
|
|
229
|
|
230 function to_html(bbcode,options)
|
|
231 if options.strip_newline then
|
|
232 if type(bbcode) == "string" then
|
29
|
233 bbcode = starting_cr_regex.gsub(bbcode,"")
|
20
|
234 end
|
|
235 options.strip_newline = false
|
|
236 end
|
12
|
237 if type(bbcode) == "string" then
|
|
238 %><%= html_encode(bbcode) %><%
|
|
239 else
|
|
240 type(bbcode) == "table" or error()
|
|
241 if is_list(bbcode) then
|
|
242 for _, v in ipairs(bbcode) do
|
20
|
243 to_html(v,options)
|
12
|
244 end
|
|
245 else
|
|
246 local fn = html[bbcode.name] or error(bbcode.name.." not handled")
|
20
|
247 fn(bbcode,options)
|
12
|
248 end
|
|
249 end
|
|
250 end
|
|
251
|
|
252 function Bbcode.to_html(bbcode)
|
|
253 bbcode = bbcode_parse(bbcode)
|
20
|
254 %><div message><%
|
|
255 to_html(bbcode,{strip_newline=false})
|
|
256 %></div><%
|
12
|
257 end
|
|
258
|
13
|
259
|
|
260 local doesnt_nest = list_to_set{
|
|
261 "url"
|
|
262 "code"
|
|
263 "img"
|
|
264 "video"
|
|
265 }
|
|
266
|
29
|
267 local url_regex = regex[[(^|\s)(https?://\S+)]]
|
|
268
|
13
|
269 local function preprocess(bbcode)
|
|
270 if type(bbcode) == "string" then
|
29
|
271 bbcode = url_regex.gsub( bbcode, "$1[url]$2[/url]" )
|
13
|
272 %><%= bbcode %><%
|
|
273 else
|
|
274 type(bbcode) == "table" or error()
|
|
275 if is_list(bbcode) then
|
|
276 for _, v in ipairs(bbcode) do
|
|
277 preprocess(v)
|
|
278 end
|
|
279 else
|
|
280 local name = bbcode.name
|
|
281 local param = bbcode.param
|
|
282 %>[<%=name%><%
|
|
283 if param ~= nil then
|
|
284 %>=<%=param%><%
|
|
285 end
|
|
286 %>]<%
|
|
287 if doesnt_nest[name] then
|
|
288 %><%=bbcode.contents%><%
|
|
289 else
|
|
290 preprocess(bbcode.contents)
|
|
291 end
|
20
|
292 if name == "code" and param ~= nil then
|
|
293 %>[/<%=name%>=<%=param%>]<%
|
|
294 else
|
|
295 %>[/<%=name%>]<%
|
|
296 end
|
13
|
297 end
|
|
298 end
|
|
299 end
|
|
300
|
43
|
301 -- url handling
|
13
|
302 function Bbcode.preprocess(bbcode)
|
|
303 bbcode = bbcode_parse(bbcode)
|
|
304 return output_of(function()
|
|
305 preprocess(bbcode)
|
|
306 end)
|
|
307 end
|
|
308
|
21
|
309 function Bbcode.remove_html(text)
|
29
|
310 local ends_with_newline = ends_with(text,"\n")
|
21
|
311 local t = {}
|
|
312 local html = html_parse(text)
|
|
313 local is_first = true
|
|
314 for _, v in ipairs(html) do
|
|
315 if type(v) == "string" then
|
|
316 t[#t+1] = v
|
|
317 else
|
|
318 local name = v.name
|
|
319 if name == "div" then
|
|
320 if not is_first then
|
|
321 t[#t+1] = "\n"
|
|
322 end
|
|
323 elseif name == "/div" or name == "br" then
|
|
324 -- ignore
|
|
325 else
|
|
326 error("unexpected tag: "..name)
|
|
327 end
|
|
328 end
|
|
329 is_first = false
|
|
330 end
|
|
331 if not ends_with_newline then
|
|
332 t[#t+1] = "\n"
|
|
333 end
|
|
334 return concat(t)
|
|
335 end
|
|
336
|
|
337
|
12
|
338 return Bbcode
|