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