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()
|
|
5 local stringify = Luan.stringify or error()
|
13
|
6 local Io = require "luan:Io.luan"
|
|
7 local output_of = Io.output_of or error()
|
12
|
8 local Parsers = require "luan:Parsers.luan"
|
|
9 local bbcode_parse = Parsers.bbcode_parse or error()
|
|
10 local Html = require "luan:Html.luan"
|
|
11 local html_encode = Html.encode or error()
|
21
|
12 local html_parse = Html.parse or error()
|
12
|
13 local Table = require "luan:Table.luan"
|
|
14 local is_list = Table.is_list or error()
|
21
|
15 local concat = Table.concat or error()
|
13
|
16 local String = require "luan:String.luan"
|
|
17 local gsub = String.gsub or error()
|
21
|
18 local matches = String.matches or error()
|
12
|
19 local User = require "site:/lib/User.luan"
|
13
|
20 local Shared = require "site:/lib/Shared.luan"
|
|
21 local list_to_set = Shared.list_to_set or error()
|
20
|
22 local to_list = Shared.to_list or error()
|
12
|
23 local Logging = require "luan:logging/Logging.luan"
|
|
24 local logger = Logging.logger "Bbcode"
|
|
25
|
|
26
|
|
27 local Bbcode = {}
|
|
28
|
|
29 local to_html
|
|
30 local html = {}
|
|
31
|
20
|
32 function html.b(bbcode,options)
|
|
33 %><b><% to_html(bbcode.contents,options) %></b><%
|
|
34 end
|
|
35
|
|
36 function html.i(bbcode,options)
|
|
37 %><i><% to_html(bbcode.contents,options) %></i><%
|
|
38 end
|
|
39
|
|
40 function html.u(bbcode,options)
|
|
41 %><u><% to_html(bbcode.contents,options) %></u><%
|
12
|
42 end
|
|
43
|
20
|
44 function html.s(bbcode,options)
|
|
45 %><s><% to_html(bbcode.contents,options) %></s><%
|
12
|
46 end
|
|
47
|
20
|
48 function html.sup(bbcode,options)
|
|
49 %><sup><% to_html(bbcode.contents,options) %></sup><%
|
12
|
50 end
|
|
51
|
20
|
52 function html.brackets(bbcode,options)
|
|
53 %>[<% to_html(bbcode.contents,options) %>]<%
|
|
54 end
|
|
55
|
|
56 function html.url(bbcode,options)
|
12
|
57 local url = bbcode.param
|
|
58 if url == nil then
|
|
59 url = html_encode(bbcode.contents)
|
|
60 %><a href="<%=url%>"><%=url%></a><%
|
|
61 else
|
|
62 url = html_encode(url)
|
20
|
63 %><a href="<%=url%>"><% to_html(bbcode.contents,options) %></a><%
|
12
|
64 end
|
|
65 end
|
|
66
|
20
|
67 function html.code(bbcode,options)
|
|
68 local s = gsub(bbcode.contents,[[^\n]],"")
|
|
69 %><code><%= html_encode(s) %></code><%
|
|
70 options.strip_newline = true
|
12
|
71 end
|
|
72
|
20
|
73 function html.img(bbcode,options)
|
12
|
74 %><img src="<%= html_encode(bbcode.contents) %>"><%
|
|
75 end
|
|
76
|
20
|
77 function html.color(bbcode,options)
|
|
78 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><%
|
12
|
79 end
|
|
80
|
20
|
81 function html.size(bbcode,options)
|
|
82 %><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents,options) %></span><%
|
12
|
83 end
|
|
84
|
20
|
85 function html.quote(bbcode,options)
|
12
|
86 %><blockquote><%
|
|
87 local user_name = bbcode.param
|
|
88 if user_name ~= nil then
|
|
89 local user = User.get_by_name(user_name)
|
|
90 if user == nil then
|
|
91 %><%= user_name %> wrote:<%
|
|
92 else
|
|
93 %><a href="/user_something"><%= user_name %></a> wrote:<%
|
|
94 end
|
20
|
95 else
|
|
96 options.strip_newline = true
|
12
|
97 end
|
20
|
98 to_html(bbcode.contents,options)
|
12
|
99 %></blockquote><%
|
20
|
100 options.strip_newline = true
|
12
|
101 end
|
|
102
|
20
|
103 function html.video(bbcode,options)
|
12
|
104 local url = html_encode(bbcode.contents)
|
|
105 local site = bbcode.site
|
|
106 if site == "youtube" then
|
|
107 %><iframe width="420" height="315" src="https://www.youtube.com/embed/<%=bbcode.id%><%
|
|
108 local start = bbcode.start
|
|
109 if start ~= nil then
|
|
110 %>?start=<%=start%><%
|
|
111 end
|
|
112 %>" frameborder="0" allowfullscreen></iframe><%
|
|
113 elseif site == "bitchute" then
|
|
114 %><iframe width="420" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/<%=bbcode.id%>/"></iframe><%
|
|
115 else
|
|
116 %><a href="<%=url%>"><%=url%></a><%
|
|
117 end
|
|
118 end
|
|
119
|
20
|
120 local function list_to_html(bbcode,options)
|
|
121 local list = to_list(bbcode.contents)
|
|
122 for _, item in ipairs(list) do
|
|
123 if type(item) == "table" and item.name == "li" then
|
|
124 %><li><% to_html(item.contents,options) %></li><%
|
|
125 end
|
|
126 end
|
|
127 options.strip_newline = true
|
|
128 end
|
|
129
|
|
130 function html.ul(bbcode,options)
|
|
131 %><ul><%
|
|
132 list_to_html(bbcode,options)
|
|
133 %></ul><%
|
|
134 end
|
|
135
|
|
136 function html.ol(bbcode,options)
|
|
137 %><ol><%
|
|
138 list_to_html(bbcode,options)
|
|
139 %></ol><%
|
|
140 end
|
|
141
|
|
142 function to_html(bbcode,options)
|
|
143 if options.strip_newline then
|
|
144 if type(bbcode) == "string" then
|
|
145 bbcode = gsub(bbcode,[[^\n]],"")
|
|
146 end
|
|
147 options.strip_newline = false
|
|
148 end
|
12
|
149 if type(bbcode) == "string" then
|
|
150 %><%= html_encode(bbcode) %><%
|
|
151 else
|
|
152 type(bbcode) == "table" or error()
|
|
153 if is_list(bbcode) then
|
|
154 for _, v in ipairs(bbcode) do
|
20
|
155 to_html(v,options)
|
12
|
156 end
|
|
157 else
|
|
158 local fn = html[bbcode.name] or error(bbcode.name.." not handled")
|
20
|
159 fn(bbcode,options)
|
12
|
160 end
|
|
161 end
|
|
162 end
|
|
163
|
|
164 function Bbcode.to_html(bbcode)
|
|
165 bbcode = bbcode_parse(bbcode)
|
20
|
166 %><div message><%
|
|
167 to_html(bbcode,{strip_newline=false})
|
|
168 %></div><%
|
12
|
169 end
|
|
170
|
13
|
171
|
|
172 local doesnt_nest = list_to_set{
|
|
173 "url"
|
|
174 "code"
|
|
175 "img"
|
|
176 "video"
|
|
177 }
|
|
178
|
|
179 local function preprocess(bbcode)
|
|
180 if type(bbcode) == "string" then
|
|
181 bbcode = gsub( bbcode, [[(^|\s)(https?://\S+)]], "$1[url]$2[/url]" )
|
|
182 %><%= bbcode %><%
|
|
183 else
|
|
184 type(bbcode) == "table" or error()
|
|
185 if is_list(bbcode) then
|
|
186 for _, v in ipairs(bbcode) do
|
|
187 preprocess(v)
|
|
188 end
|
|
189 else
|
|
190 local name = bbcode.name
|
|
191 local param = bbcode.param
|
|
192 %>[<%=name%><%
|
|
193 if param ~= nil then
|
|
194 %>=<%=param%><%
|
|
195 end
|
|
196 %>]<%
|
|
197 if doesnt_nest[name] then
|
|
198 %><%=bbcode.contents%><%
|
|
199 else
|
|
200 preprocess(bbcode.contents)
|
|
201 end
|
20
|
202 if name == "code" and param ~= nil then
|
|
203 %>[/<%=name%>=<%=param%>]<%
|
|
204 else
|
|
205 %>[/<%=name%>]<%
|
|
206 end
|
13
|
207 end
|
|
208 end
|
|
209 end
|
|
210
|
|
211 function Bbcode.preprocess(bbcode)
|
|
212 bbcode = bbcode_parse(bbcode)
|
|
213 return output_of(function()
|
|
214 preprocess(bbcode)
|
|
215 end)
|
|
216 end
|
|
217
|
21
|
218 function Bbcode.remove_html(text)
|
|
219 local ends_with_newline = matches(text,[[\n$]])
|
|
220 local t = {}
|
|
221 local html = html_parse(text)
|
|
222 local is_first = true
|
|
223 for _, v in ipairs(html) do
|
|
224 if type(v) == "string" then
|
|
225 t[#t+1] = v
|
|
226 else
|
|
227 local name = v.name
|
|
228 if name == "div" then
|
|
229 if not is_first then
|
|
230 t[#t+1] = "\n"
|
|
231 end
|
|
232 elseif name == "/div" or name == "br" then
|
|
233 -- ignore
|
|
234 else
|
|
235 error("unexpected tag: "..name)
|
|
236 end
|
|
237 end
|
|
238 is_first = false
|
|
239 end
|
|
240 if not ends_with_newline then
|
|
241 t[#t+1] = "\n"
|
|
242 end
|
|
243 return concat(t)
|
|
244 end
|
|
245
|
|
246
|
12
|
247 return Bbcode
|