annotate src/lib/Bbcode.luan @ 27:6871eec2cf4c

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