changeset 53:cac477dd1f82

convert image and video urls
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 24 Nov 2022 22:54:43 -0700
parents 9f8ebc757814
children 260abd8f8565
files src/bbcode/Bbcode.luan src/bbcode/bbcode.css src/bbcode/bbcode.js
diffstat 3 files changed, 54 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/bbcode/Bbcode.luan	Wed Nov 23 23:29:16 2022 -0700
+++ b/src/bbcode/Bbcode.luan	Thu Nov 24 22:54:43 2022 -0700
@@ -82,7 +82,11 @@
 end
 
 function html.img(bbcode,options)
-	%><img src="<%= html_encode(bbcode.contents) %>"><%
+	%><img src="<%= html_encode(bbcode.contents) %>"<%
+	if bbcode.param ~= nil then
+		%> width="<%=bbcode.param%>"<%
+	end
+	%>><%
 end
 
 function html.color(bbcode,options)
@@ -115,10 +119,17 @@
 	%><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><%
 end
 
+local video_urls = {
+	[[\.mp4$]]
+}
 local video_handlers = {}
+local function regex_video(s)
+	video_urls[#video_urls+1] = s
+	return regex(s)
+end
 do
-	local ptn1 = regex[[^\Qhttps://youtu.be/\E([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
-	local ptn2 = regex[[^\Qhttps://www.youtube.com/watch?v=\E([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]]
+	local ptn1 = regex_video[[^\Qhttps://youtu.be/\E([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
+	local ptn2 = regex_video[[^\Qhttps://www.youtube.com/watch?v=\E([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]]
 	function video_handlers.youtube(url)
 		local id, start = ptn1.match(url)
 		if id == nil then
@@ -136,7 +147,7 @@
 	end
 end
 do
-	local ptn = regex[[^\Qhttps://rumble.com/embed/\E[a-z0-9]+/\?pub=[a-z0-9]+]]
+	local ptn = regex_video[[^\Qhttps://rumble.com/embed/\E[a-z0-9]+/\?pub=[a-z0-9]+]]
 	function video_handlers.rumble(url)
 		if not ptn.matches(url) then
 			return false
@@ -146,7 +157,7 @@
 	end
 end
 do
-	local ptn = regex[[^\Qhttps://www.bitchute.com/video/\E([a-zA-Z0-9]+)/]]
+	local ptn = regex_video[[^\Qhttps://www.bitchute.com/video/\E([a-zA-Z0-9]+)/]]
 	function video_handlers.bitchute(url)
 		local id = ptn.match(url)
 		if id == nil then
@@ -158,7 +169,7 @@
 	end
 end
 do
-	local ptn = regex[[^\Qhttps://vimeo.com/\E([0-9]+)]]
+	local ptn = regex_video[[^\Qhttps://vimeo.com/\E([0-9]+)]]
 	function video_handlers.vimeo(url)
 		local id = ptn.match(url)
 		if id == nil then
@@ -170,7 +181,7 @@
 	end
 end
 do
-	local ptn = regex[[^\Qhttps://dai.ly/\E([a-z0-9]+)]]
+	local ptn = regex_video[[^\Qhttps://dai.ly/\E([a-z0-9]+)]]
 	function video_handlers.dailymotion(url)
 		local id = ptn.match(url)
 		if id == nil then
@@ -182,14 +193,13 @@
 	end
 end
 do
-	local ptn = regex[[^\Qhttps://www.tiktok.com/\E[^/]+/video/([0-9]+)]]
+	local ptn = regex_video[[^\Qhttps://www.tiktok.com/\E[^/]+/video/([0-9]+)]]
 	function video_handlers.tiktok(url)
 		local id = ptn.match(url)
 		if id == nil then
 			return false
 		end
-		%><blockquote class="tiktok-embed" data-video-id="<%=id%>" style="max-width: 560px; margin-left: 0;"><section></section></blockquote><%
-		%><script async src="https://www.tiktok.com/embed.js"></script><%
+		%><iframe allowfullscreen scrolling="no" width="325" height="720" src="https://www.tiktok.com/embed/<%=id%>"></iframe><%
 		return true
 	end
 end
@@ -204,6 +214,8 @@
 	end
 end
 
+local video_regex = regex(concat(video_urls,"|"))
+
 function html.video(bbcode,options)
 	local url = bbcode.contents
 	for _, handle in pairs(video_handlers) do
@@ -268,10 +280,33 @@
 }
 
 local url_regex = regex[[(^|\s)(https?://\S+)]]
+local img_regex
+do
+	local endings = {
+		"jpg"
+		"jpeg"
+		"png"
+		"gif"
+		"svg"
+	}
+	local t = {}
+	for _, ending in ipairs(endings) do
+		t[#t+1] = [[\.]]..ending..[[$]]
+	end
+	img_regex = regex(concat(t,"|"))
+end
 
 local function preprocess(bbcode)
 	if type(bbcode) == "string" then
-		bbcode = url_regex.gsub( bbcode, "$1[url]$2[/url]" )
+		bbcode = url_regex.gsub( bbcode, function(prefix,url)
+			if video_regex.matches(url) then
+				return prefix.."[video]"..url.."[/video]"
+			elseif img_regex.matches(url) then
+				return prefix.."[img]"..url.."[/img]"
+			else
+				return prefix.."[url]"..url.."[/url]"
+			end
+		end )
 		%><%= bbcode %><%
 	else
 		type(bbcode) == "table" or error()
--- a/src/bbcode/bbcode.css	Wed Nov 23 23:29:16 2022 -0700
+++ b/src/bbcode/bbcode.css	Thu Nov 24 22:54:43 2022 -0700
@@ -61,3 +61,9 @@
 	display: initial;
 	padding: 1px;
 }
+div[from_bbcode] img {
+	max-width: 100%;
+}
+div[from_bbcode] iframe {
+	max-width: 100%;
+}
--- a/src/bbcode/bbcode.js	Wed Nov 23 23:29:16 2022 -0700
+++ b/src/bbcode/bbcode.js	Thu Nov 24 22:54:43 2022 -0700
@@ -169,8 +169,8 @@
 			}
 			textarea.style.display = 'none';
 			divPreview.style.display = 'block';
-			let url = '/bbcode/preview.js?text=' + encodeURIComponent(textarea.value) + '&convert_urls=' + convertUrls;
-			ajax( url, null, {preview: contextPreview} );
+			let postData = 'text=' + encodeURIComponent(textarea.value) + '&convert_urls=' + convertUrls;
+			ajax( '/bbcode/preview.js', postData, {preview: contextPreview} );
 		} else {
 			for( let b of buttons ) {
 					b.disabled = false;