annotate src/lib/Bbcode.luan @ 20:3ea49246d6a7

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