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