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()
|
12
|
19 local Logging = require "luan:logging/Logging.luan"
|
|
20 local logger = Logging.logger "Bbcode"
|
|
21
|
|
22
|
|
23 local Bbcode = {}
|
|
24
|
|
25 local to_html
|
|
26 local html = {}
|
|
27
|
|
28 function html.b(bbcode)
|
|
29 %><b><% to_html(bbcode.contents) %></b><%
|
|
30 end
|
|
31
|
|
32 function html.i(bbcode)
|
|
33 %><i><% to_html(bbcode.contents) %></i><%
|
|
34 end
|
|
35
|
|
36 function html.u(bbcode)
|
|
37 %><u><% to_html(bbcode.contents) %></u><%
|
|
38 end
|
|
39
|
|
40 function html.url(bbcode)
|
|
41 local url = bbcode.param
|
|
42 if url == nil then
|
|
43 url = html_encode(bbcode.contents)
|
|
44 %><a href="<%=url%>"><%=url%></a><%
|
|
45 else
|
|
46 url = html_encode(url)
|
|
47 %><a href="<%=url%>"><% to_html(bbcode.contents) %></a><%
|
|
48 end
|
|
49 end
|
|
50
|
|
51 function html.code(bbcode)
|
|
52 %><code><%= html_encode(bbcode.contents) %></code><%
|
|
53 end
|
|
54
|
|
55 function html.img(bbcode)
|
|
56 %><img src="<%= html_encode(bbcode.contents) %>"><%
|
|
57 end
|
|
58
|
|
59 function html.color(bbcode)
|
|
60 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents) %></span><%
|
|
61 end
|
|
62
|
|
63 function html.size(bbcode)
|
|
64 %><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents) %></span><%
|
|
65 end
|
|
66
|
|
67 function html.quote(bbcode)
|
|
68 %><blockquote><%
|
|
69 local user_name = bbcode.param
|
|
70 if user_name ~= nil then
|
|
71 local user = User.get_by_name(user_name)
|
|
72 if user == nil then
|
|
73 %><%= user_name %> wrote:<%
|
|
74 else
|
|
75 %><a href="/user_something"><%= user_name %></a> wrote:<%
|
|
76 end
|
|
77 end
|
|
78 to_html(bbcode.contents)
|
|
79 %></blockquote><%
|
|
80 end
|
|
81
|
|
82 function html.video(bbcode)
|
|
83 local url = html_encode(bbcode.contents)
|
|
84 local site = bbcode.site
|
|
85 if site == "youtube" then
|
|
86 %><iframe width="420" height="315" src="https://www.youtube.com/embed/<%=bbcode.id%><%
|
|
87 local start = bbcode.start
|
|
88 if start ~= nil then
|
|
89 %>?start=<%=start%><%
|
|
90 end
|
|
91 %>" frameborder="0" allowfullscreen></iframe><%
|
|
92 elseif site == "bitchute" then
|
|
93 %><iframe width="420" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/<%=bbcode.id%>/"></iframe><%
|
|
94 else
|
|
95 %><a href="<%=url%>"><%=url%></a><%
|
|
96 end
|
|
97 end
|
|
98
|
|
99 function to_html(bbcode)
|
|
100 if type(bbcode) == "string" then
|
|
101 %><%= html_encode(bbcode) %><%
|
|
102 else
|
|
103 type(bbcode) == "table" or error()
|
|
104 if is_list(bbcode) then
|
|
105 for _, v in ipairs(bbcode) do
|
|
106 to_html(v)
|
|
107 end
|
|
108 else
|
|
109 local fn = html[bbcode.name] or error(bbcode.name.." not handled")
|
|
110 fn(bbcode)
|
|
111 end
|
|
112 end
|
|
113 end
|
|
114
|
|
115 function Bbcode.to_html(bbcode)
|
|
116 bbcode = bbcode_parse(bbcode)
|
15
|
117 %><div message><% to_html(bbcode) %></div><%
|
12
|
118 end
|
|
119
|
13
|
120
|
|
121 local doesnt_nest = list_to_set{
|
|
122 "url"
|
|
123 "code"
|
|
124 "img"
|
|
125 "video"
|
|
126 }
|
|
127
|
|
128 local function preprocess(bbcode)
|
|
129 if type(bbcode) == "string" then
|
|
130 bbcode = gsub( bbcode, [[(^|\s)(https?://\S+)]], "$1[url]$2[/url]" )
|
|
131 %><%= bbcode %><%
|
|
132 else
|
|
133 type(bbcode) == "table" or error()
|
|
134 if is_list(bbcode) then
|
|
135 for _, v in ipairs(bbcode) do
|
|
136 preprocess(v)
|
|
137 end
|
|
138 else
|
|
139 local name = bbcode.name
|
|
140 local param = bbcode.param
|
|
141 %>[<%=name%><%
|
|
142 if param ~= nil then
|
|
143 %>=<%=param%><%
|
|
144 end
|
|
145 %>]<%
|
|
146 if doesnt_nest[name] then
|
|
147 %><%=bbcode.contents%><%
|
|
148 else
|
|
149 preprocess(bbcode.contents)
|
|
150 end
|
|
151 %>[/<%=name%>]<%
|
|
152 end
|
|
153 end
|
|
154 end
|
|
155
|
|
156 function Bbcode.preprocess(bbcode)
|
|
157 bbcode = bbcode_parse(bbcode)
|
|
158 return output_of(function()
|
|
159 preprocess(bbcode)
|
|
160 end)
|
|
161 end
|
|
162
|
12
|
163 return Bbcode
|