annotate src/lib/Bbcode.luan @ 19:da006d1c1eba

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