comparison src/lib/Bbcode.luan @ 12:ad1604c72156

fix bbcode
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Jul 2022 22:01:23 -0600
parents
children 24668255cede
comparison
equal deleted inserted replaced
11:3ed1e3f3a53a 12:ad1604c72156
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()
6 local Parsers = require "luan:Parsers.luan"
7 local bbcode_parse = Parsers.bbcode_parse or error()
8 local Html = require "luan:Html.luan"
9 local html_encode = Html.encode or error()
10 local Table = require "luan:Table.luan"
11 local is_list = Table.is_list or error()
12 local User = require "site:/lib/User.luan"
13 local Logging = require "luan:logging/Logging.luan"
14 local logger = Logging.logger "Bbcode"
15
16
17 local Bbcode = {}
18
19 local to_html
20 local html = {}
21
22 function html.b(bbcode)
23 %><b><% to_html(bbcode.contents) %></b><%
24 end
25
26 function html.i(bbcode)
27 %><i><% to_html(bbcode.contents) %></i><%
28 end
29
30 function html.u(bbcode)
31 %><u><% to_html(bbcode.contents) %></u><%
32 end
33
34 function html.url(bbcode)
35 local url = bbcode.param
36 if url == nil then
37 url = html_encode(bbcode.contents)
38 %><a href="<%=url%>"><%=url%></a><%
39 else
40 url = html_encode(url)
41 %><a href="<%=url%>"><% to_html(bbcode.contents) %></a><%
42 end
43 end
44
45 function html.code(bbcode)
46 %><code><%= html_encode(bbcode.contents) %></code><%
47 end
48
49 function html.img(bbcode)
50 %><img src="<%= html_encode(bbcode.contents) %>"><%
51 end
52
53 function html.color(bbcode)
54 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents) %></span><%
55 end
56
57 function html.size(bbcode)
58 %><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents) %></span><%
59 end
60
61 function html.quote(bbcode)
62 %><blockquote><%
63 local user_name = bbcode.param
64 if user_name ~= nil then
65 local user = User.get_by_name(user_name)
66 if user == nil then
67 %><%= user_name %> wrote:<%
68 else
69 %><a href="/user_something"><%= user_name %></a> wrote:<%
70 end
71 end
72 to_html(bbcode.contents)
73 %></blockquote><%
74 end
75
76 function html.video(bbcode)
77 local url = html_encode(bbcode.contents)
78 local site = bbcode.site
79 if site == "youtube" then
80 %><iframe width="420" height="315" src="https://www.youtube.com/embed/<%=bbcode.id%><%
81 local start = bbcode.start
82 if start ~= nil then
83 %>?start=<%=start%><%
84 end
85 %>" frameborder="0" allowfullscreen></iframe><%
86 elseif site == "bitchute" then
87 %><iframe width="420" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/<%=bbcode.id%>/"></iframe><%
88 else
89 %><a href="<%=url%>"><%=url%></a><%
90 end
91 end
92
93 function to_html(bbcode)
94 if type(bbcode) == "string" then
95 %><%= html_encode(bbcode) %><%
96 else
97 type(bbcode) == "table" or error()
98 if is_list(bbcode) then
99 for _, v in ipairs(bbcode) do
100 to_html(v)
101 end
102 else
103 local fn = html[bbcode.name] or error(bbcode.name.." not handled")
104 fn(bbcode)
105 end
106 end
107 end
108
109 function Bbcode.to_html(bbcode)
110 bbcode = bbcode_parse(bbcode)
111 %><div post><% to_html(bbcode) %></div><%
112 end
113
114 return Bbcode