changeset 28:d9d7aa2a79db

more video types
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Jul 2022 23:44:49 -0600
parents 6871eec2cf4c
children a1db5223ced1
files src/lib/Bbcode.luan src/test/tiny.html
diffstat 2 files changed, 181 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/Bbcode.luan	Thu Jul 21 00:01:46 2022 -0600
+++ b/src/lib/Bbcode.luan	Thu Jul 21 23:44:49 2022 -0600
@@ -2,6 +2,7 @@
 local error = Luan.error
 local type = Luan.type or error()
 local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
 local stringify = Luan.stringify or error()
 local Io = require "luan:Io.luan"
 local output_of = Io.output_of or error()
@@ -101,33 +102,103 @@
 	options.strip_newline = true
 end
 
-local youtube_ptn1 = [[https://youtu.be/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
-local youtube_ptn2 = [[https://www.youtube.com/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]]
-local bitchute_ptn = [[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]]
-local rumble_ptn = [[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]]
+local function video_iframe(url)
+	%><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><%
+end
+
+local video_handlers = {}
+do
+	local ptn1 = [[https://youtu.be/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
+	local ptn2 = [[https://www.youtube.com/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]]
+	function video_handlers.youtube(url)
+		local id, start = match(url,ptn1)
+		if id == nil then
+			id, start = match(url,ptn2)
+		end
+		if id == nil then
+			return false
+		end
+		url = "https://www.youtube.com/embed/"..id
+		if start ~= nil then
+			url = url.."?start="..start
+		end
+		video_iframe(url)
+		return true
+	end
+end
+do
+	local ptn = [[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]]
+	function video_handlers.rumble(url)
+		if not matches(url,ptn) then
+			return false
+		end
+		video_iframe(url)
+		return true
+	end
+end
+do
+	local ptn = [[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]]
+	function video_handlers.bitchute(url)
+		local id = match(url,ptn)
+		if id == nil then
+			return false
+		end
+		url = "https://www.bitchute.com/embed/"..id.."/"
+		video_iframe(url)
+		return true
+	end
+end
+do
+	local ptn = [[https://vimeo.com/([0-9]+)]]
+	function video_handlers.vimeo(url)
+		local id = match(url,ptn)
+		if id == nil then
+			return false
+		end
+		url = "https://player.vimeo.com/video/"..id
+		video_iframe(url)
+		return true
+	end
+end
+do
+	local ptn = [[https://dai.ly/([a-z0-9]+)]]
+	function video_handlers.dailymotion(url)
+		local id = match(url,ptn)
+		if id == nil then
+			return false
+		end
+		url = "https://www.dailymotion.com/embed/video/"..id
+		video_iframe(url)
+		return true
+	end
+end
+do
+	local ptn = [[https://www.tiktok.com/[^/]+/video/([0-9]+)]]
+	function video_handlers.tiktok(url)
+		local id = match(url,ptn)
+		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><%
+		return true
+	end
+end
+do
+	local ptn = [[\.[a-zA-Z0-9]+$]]
+	function video_handlers.file(url)
+		if not matches(url,ptn) then
+			return false
+		end
+		%><video controls width="560"><source src="<%=html_encode(url)%>"></video><%
+		return true
+	end
+end
 
 function html.video(bbcode,options)
 	local url = bbcode.contents
-	local id, start = match(url,youtube_ptn1)
-	if id == nil then
-		id, start = match(url,youtube_ptn2)
-	end
-	if id ~= nil then
-		%><iframe width="560" height="315" src="https://www.youtube.com/embed/<%=id%><%
-		if start ~= nil then
-			%>?start=<%=start%><%
-		end
-		%>" frameborder="0" allowfullscreen></iframe><%
-		return
-	end
-	id = match(url,bitchute_ptn)
-	if id ~= nil then
-		%><iframe width="560" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/<%=id%>/"></iframe><%
-		return
-	end
-	if matches(url,rumble_ptn) then
-		%><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><%
-		return
+	for _, handle in pairs(video_handlers) do
+		if handle(url) then return end
 	end
 	url = html_encode(url)
 	%><a href="<%=url%>"><%=url%></a><%
--- a/src/test/tiny.html	Thu Jul 21 00:01:46 2022 -0600
+++ b/src/test/tiny.html	Thu Jul 21 23:44:49 2022 -0600
@@ -6,29 +6,94 @@
 		<style>
 		</style>
 		<script>
-			var youtubePtn1 = new RegExp('https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?');
-			var youtubePtn2 = new RegExp('https://www.youtube.com/watch\\?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?');
-			var bitchutePtn = new RegExp('https://www.bitchute.com/video/([a-zA-Z0-9]+)/');
-			var rumblePtn = new RegExp('https://rumble.com/embed/[a-z0-9]+/\\?pub=[a-z0-9]+');
-			var url2;
+			function videoIframe(url) {
+				return '<iframe data-video="'+url+'" width="560" height="315" frameborder="0" allowfullscreen src="'+url+'"></iframe>';
+			}
+
+			var videoHandlers = {};
+			{
+				let ptn1 = new RegExp('https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?');
+				let ptn2 = new RegExp('https://www.youtube.com/watch\\?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?');
+				videoHandlers.youtube = function(url) {
+					let result = url.match(ptn1) || url.match(ptn2);
+					if( result ) {
+						url = 'https://www.youtube.com/embed/' + result[1];
+						if( result[2] )
+							url += '?start=' + result[2];
+						return videoIframe(url);
+					}
+				}
+			}
+			{
+				let ptn = new RegExp('https://rumble.com/embed/[a-z0-9]+/\\?pub=[a-z0-9]+');
+				videoHandlers.rumble = function(url) {
+					if( url.match(ptn) ) {
+						return videoIframe(url);
+					}
+				}
+			}
+			{
+				let ptn = new RegExp('https://www.bitchute.com/video/([a-zA-Z0-9]+)/');
+				videoHandlers.bitchute = function(url) {
+					let result = url.match(ptn);
+					if( result ) {
+						url = 'https://www.bitchute.com/embed/' + result[1];
+						return videoIframe(url);
+					}
+				}
+			}
+			{
+				let ptn = new RegExp('https://vimeo.com/([0-9]+)');
+				videoHandlers.vimeo = function(url) {
+					let result = url.match(ptn);
+					if( result ) {
+						url = 'https://player.vimeo.com/video/' + result[1];
+						return videoIframe(url);
+					}
+				}
+			}
+			{
+				let ptn = new RegExp('https://dai.ly/([a-z0-9]+)');
+				videoHandlers.dailymotion = function(url) {
+					let result = url.match(ptn);
+					if( result ) {
+						url = 'https://www.dailymotion.com/embed/video/' + result[1];
+						return videoIframe(url);
+					}
+				}
+			}
+			{
+				let ptn = new RegExp('https://www.tiktok.com/[^/]+/video/([0-9]+)');
+				videoHandlers.tiktok = function(url) {
+					let result = url.match(ptn);
+					if( result ) {
+						let html = '<blockquote class="tiktok-embed" data-video-id="'+result[1]+'" style="max-width: 560px; margin-left: 0;"><section></section></blockquote>';
+						//html += '<script async src="https://www.tiktok.com/embed.js"></'+'script>';
+						return html;
+					}
+				}
+			}
+			{
+				let ptn = new RegExp('\\.[a-zA-Z0-9]+$');
+				videoHandlers.file = function(url) {
+					if( url.match(ptn) ) {
+						return '<video controls width="560" height><source src="'+url+'"></video>';
+					}
+				}
+			}
+
+			function videoUrlToHtml(url) {
+				for (let key in videoHandlers) {
+					let handle = videoHandlers[key];
+					let html = handle(url);
+					if(html) return html;
+				}
+				return '<a data-video="'+url+'" href="'+url+'">'+url+'</a>';
+			}
+			console.log(videoUrlToHtml('https://www.tiktok.com/@chantelleef/video/7112118342181276933?is_from_webapp=1&sender_device=pc&web_id=7073254106622838318'));
 
 			function media_url_resolver(data,resolve,reject) {
-				let html;
-				let url = data.url;
-				url2 = url;
-				let result;
-				if( result = url.match(youtubePtn1) || url.match(youtubePtn2) ) {
-					html = '<iframe data-video="'+url+'" width="560" height="315" src="https://www.youtube.com/embed/' + result[1];
-					if( result[2] )
-						html += '?start=' + result[2]
-					html += '" frameborder="0" allowfullscreen></iframe>';
-				} else if( result = url.match(bitchutePtn) ) {
-					html = '<iframe data-video="'+url+'" width="560" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/' + result[1] + '/"></iframe>';
-				} else if( url.match(rumblePtn) ) {
-					html = '<iframe data-video="'+url+'" width="560" height="315" frameborder="0" allowfullscreen src="' + url + '"></iframe>';
-				} else {
-					html = '<a data-video="'+url+'" href="'+url+'">'+url+'</a>';
-				}
+				let html = videoUrlToHtml(data.url);
 				resolve({ html: html });
 			}