comparison src/lib/Bbcode.luan @ 13:24668255cede

preprocess urls
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Jul 2022 23:59:41 -0600
parents ad1604c72156
children 0edde02b908c
comparison
equal deleted inserted replaced
12:ad1604c72156 13:24668255cede
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local type = Luan.type or error() 3 local type = Luan.type or error()
4 local ipairs = Luan.ipairs or error() 4 local ipairs = Luan.ipairs or error()
5 local stringify = Luan.stringify or error() 5 local stringify = Luan.stringify or error()
6 local Io = require "luan:Io.luan"
7 local output_of = Io.output_of or error()
6 local Parsers = require "luan:Parsers.luan" 8 local Parsers = require "luan:Parsers.luan"
7 local bbcode_parse = Parsers.bbcode_parse or error() 9 local bbcode_parse = Parsers.bbcode_parse or error()
8 local Html = require "luan:Html.luan" 10 local Html = require "luan:Html.luan"
9 local html_encode = Html.encode or error() 11 local html_encode = Html.encode or error()
10 local Table = require "luan:Table.luan" 12 local Table = require "luan:Table.luan"
11 local is_list = Table.is_list or error() 13 local is_list = Table.is_list or error()
14 local String = require "luan:String.luan"
15 local gsub = String.gsub or error()
12 local User = require "site:/lib/User.luan" 16 local User = require "site:/lib/User.luan"
17 local Shared = require "site:/lib/Shared.luan"
18 local list_to_set = Shared.list_to_set or error()
13 local Logging = require "luan:logging/Logging.luan" 19 local Logging = require "luan:logging/Logging.luan"
14 local logger = Logging.logger "Bbcode" 20 local logger = Logging.logger "Bbcode"
15 21
16 22
17 local Bbcode = {} 23 local Bbcode = {}
109 function Bbcode.to_html(bbcode) 115 function Bbcode.to_html(bbcode)
110 bbcode = bbcode_parse(bbcode) 116 bbcode = bbcode_parse(bbcode)
111 %><div post><% to_html(bbcode) %></div><% 117 %><div post><% to_html(bbcode) %></div><%
112 end 118 end
113 119
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
114 return Bbcode 163 return Bbcode