comparison src/bbcode/Bbcode.luan @ 53:cac477dd1f82

convert image and video urls
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 24 Nov 2022 22:54:43 -0700
parents f225e82b2bf8
children
comparison
equal deleted inserted replaced
52:9f8ebc757814 53:cac477dd1f82
80 options.strip_newline = true 80 options.strip_newline = true
81 end 81 end
82 end 82 end
83 83
84 function html.img(bbcode,options) 84 function html.img(bbcode,options)
85 %><img src="<%= html_encode(bbcode.contents) %>"><% 85 %><img src="<%= html_encode(bbcode.contents) %>"<%
86 if bbcode.param ~= nil then
87 %> width="<%=bbcode.param%>"<%
88 end
89 %>><%
86 end 90 end
87 91
88 function html.color(bbcode,options) 92 function html.color(bbcode,options)
89 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><% 93 %><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><%
90 end 94 end
113 117
114 local function video_iframe(url) 118 local function video_iframe(url)
115 %><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><% 119 %><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><%
116 end 120 end
117 121
122 local video_urls = {
123 [[\.mp4$]]
124 }
118 local video_handlers = {} 125 local video_handlers = {}
119 do 126 local function regex_video(s)
120 local ptn1 = regex[[^\Qhttps://youtu.be/\E([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]] 127 video_urls[#video_urls+1] = s
121 local ptn2 = regex[[^\Qhttps://www.youtube.com/watch?v=\E([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]] 128 return regex(s)
129 end
130 do
131 local ptn1 = regex_video[[^\Qhttps://youtu.be/\E([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
132 local ptn2 = regex_video[[^\Qhttps://www.youtube.com/watch?v=\E([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]]
122 function video_handlers.youtube(url) 133 function video_handlers.youtube(url)
123 local id, start = ptn1.match(url) 134 local id, start = ptn1.match(url)
124 if id == nil then 135 if id == nil then
125 id, start = ptn2.match(url) 136 id, start = ptn2.match(url)
126 end 137 end
134 video_iframe(url) 145 video_iframe(url)
135 return true 146 return true
136 end 147 end
137 end 148 end
138 do 149 do
139 local ptn = regex[[^\Qhttps://rumble.com/embed/\E[a-z0-9]+/\?pub=[a-z0-9]+]] 150 local ptn = regex_video[[^\Qhttps://rumble.com/embed/\E[a-z0-9]+/\?pub=[a-z0-9]+]]
140 function video_handlers.rumble(url) 151 function video_handlers.rumble(url)
141 if not ptn.matches(url) then 152 if not ptn.matches(url) then
142 return false 153 return false
143 end 154 end
144 video_iframe(url) 155 video_iframe(url)
145 return true 156 return true
146 end 157 end
147 end 158 end
148 do 159 do
149 local ptn = regex[[^\Qhttps://www.bitchute.com/video/\E([a-zA-Z0-9]+)/]] 160 local ptn = regex_video[[^\Qhttps://www.bitchute.com/video/\E([a-zA-Z0-9]+)/]]
150 function video_handlers.bitchute(url) 161 function video_handlers.bitchute(url)
151 local id = ptn.match(url) 162 local id = ptn.match(url)
152 if id == nil then 163 if id == nil then
153 return false 164 return false
154 end 165 end
156 video_iframe(url) 167 video_iframe(url)
157 return true 168 return true
158 end 169 end
159 end 170 end
160 do 171 do
161 local ptn = regex[[^\Qhttps://vimeo.com/\E([0-9]+)]] 172 local ptn = regex_video[[^\Qhttps://vimeo.com/\E([0-9]+)]]
162 function video_handlers.vimeo(url) 173 function video_handlers.vimeo(url)
163 local id = ptn.match(url) 174 local id = ptn.match(url)
164 if id == nil then 175 if id == nil then
165 return false 176 return false
166 end 177 end
168 video_iframe(url) 179 video_iframe(url)
169 return true 180 return true
170 end 181 end
171 end 182 end
172 do 183 do
173 local ptn = regex[[^\Qhttps://dai.ly/\E([a-z0-9]+)]] 184 local ptn = regex_video[[^\Qhttps://dai.ly/\E([a-z0-9]+)]]
174 function video_handlers.dailymotion(url) 185 function video_handlers.dailymotion(url)
175 local id = ptn.match(url) 186 local id = ptn.match(url)
176 if id == nil then 187 if id == nil then
177 return false 188 return false
178 end 189 end
180 video_iframe(url) 191 video_iframe(url)
181 return true 192 return true
182 end 193 end
183 end 194 end
184 do 195 do
185 local ptn = regex[[^\Qhttps://www.tiktok.com/\E[^/]+/video/([0-9]+)]] 196 local ptn = regex_video[[^\Qhttps://www.tiktok.com/\E[^/]+/video/([0-9]+)]]
186 function video_handlers.tiktok(url) 197 function video_handlers.tiktok(url)
187 local id = ptn.match(url) 198 local id = ptn.match(url)
188 if id == nil then 199 if id == nil then
189 return false 200 return false
190 end 201 end
191 %><blockquote class="tiktok-embed" data-video-id="<%=id%>" style="max-width: 560px; margin-left: 0;"><section></section></blockquote><% 202 %><iframe allowfullscreen scrolling="no" width="325" height="720" src="https://www.tiktok.com/embed/<%=id%>"></iframe><%
192 %><script async src="https://www.tiktok.com/embed.js"></script><%
193 return true 203 return true
194 end 204 end
195 end 205 end
196 do 206 do
197 local ptn = regex[[\.[a-zA-Z0-9]+$]] 207 local ptn = regex[[\.[a-zA-Z0-9]+$]]
201 end 211 end
202 %><video controls width="560"><source src="<%=html_encode(url)%>"></video><% 212 %><video controls width="560"><source src="<%=html_encode(url)%>"></video><%
203 return true 213 return true
204 end 214 end
205 end 215 end
216
217 local video_regex = regex(concat(video_urls,"|"))
206 218
207 function html.video(bbcode,options) 219 function html.video(bbcode,options)
208 local url = bbcode.contents 220 local url = bbcode.contents
209 for _, handle in pairs(video_handlers) do 221 for _, handle in pairs(video_handlers) do
210 if handle(url) then return end 222 if handle(url) then return end
266 "img" 278 "img"
267 "video" 279 "video"
268 } 280 }
269 281
270 local url_regex = regex[[(^|\s)(https?://\S+)]] 282 local url_regex = regex[[(^|\s)(https?://\S+)]]
283 local img_regex
284 do
285 local endings = {
286 "jpg"
287 "jpeg"
288 "png"
289 "gif"
290 "svg"
291 }
292 local t = {}
293 for _, ending in ipairs(endings) do
294 t[#t+1] = [[\.]]..ending..[[$]]
295 end
296 img_regex = regex(concat(t,"|"))
297 end
271 298
272 local function preprocess(bbcode) 299 local function preprocess(bbcode)
273 if type(bbcode) == "string" then 300 if type(bbcode) == "string" then
274 bbcode = url_regex.gsub( bbcode, "$1[url]$2[/url]" ) 301 bbcode = url_regex.gsub( bbcode, function(prefix,url)
302 if video_regex.matches(url) then
303 return prefix.."[video]"..url.."[/video]"
304 elseif img_regex.matches(url) then
305 return prefix.."[img]"..url.."[/img]"
306 else
307 return prefix.."[url]"..url.."[/url]"
308 end
309 end )
275 %><%= bbcode %><% 310 %><%= bbcode %><%
276 else 311 else
277 type(bbcode) == "table" or error() 312 type(bbcode) == "table" or error()
278 if is_list(bbcode) then 313 if is_list(bbcode) then
279 for _, v in ipairs(bbcode) do 314 for _, v in ipairs(bbcode) do