diff src/lib/Bbcode.luan @ 29:a1db5223ced1

luan changes
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 25 Jul 2022 21:28:10 -0600
parents d9d7aa2a79db
children 8ff35379cc89
line wrap: on
line diff
--- a/src/lib/Bbcode.luan	Thu Jul 21 23:44:49 2022 -0600
+++ b/src/lib/Bbcode.luan	Mon Jul 25 21:28:10 2022 -0600
@@ -15,9 +15,8 @@
 local is_list = Table.is_list or error()
 local concat = Table.concat or error()
 local String = require "luan:String.luan"
-local gsub = String.gsub or error()
-local matches = String.matches or error()
-local match = String.match or error()
+local regex = String.regex or error()
+local ends_with = String.ends_with or error()
 local User = require "site:/lib/User.luan"
 local Shared = require "site:/lib/Shared.luan"
 local list_to_set = Shared.list_to_set or error()
@@ -28,6 +27,8 @@
 
 local Bbcode = {}
 
+local starting_cr_regex = regex[[^\n]]
+
 local to_html
 local html = {}
 
@@ -67,7 +68,7 @@
 end
 
 function html.code(bbcode,options)
-	local s = gsub(bbcode.contents,[[^\n]],"")
+	local s = starting_cr_regex.gsub(bbcode.contents,"")
 	%><code><%= html_encode(s) %></code><%
 	options.strip_newline = true
 end
@@ -108,12 +109,12 @@
 
 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)?]]
+	local ptn1 = regex[[https://youtu.be/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
+	local ptn2 = regex[[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)
+		local id, start = ptn1.match(url)
 		if id == nil then
-			id, start = match(url,ptn2)
+			id, start = ptn2.match(url)
 		end
 		if id == nil then
 			return false
@@ -127,9 +128,9 @@
 	end
 end
 do
-	local ptn = [[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]]
+	local ptn = regex[[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]]
 	function video_handlers.rumble(url)
-		if not matches(url,ptn) then
+		if not ptn.matches(url) then
 			return false
 		end
 		video_iframe(url)
@@ -137,9 +138,9 @@
 	end
 end
 do
-	local ptn = [[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]]
+	local ptn = regex[[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]]
 	function video_handlers.bitchute(url)
-		local id = match(url,ptn)
+		local id = ptn.match(url)
 		if id == nil then
 			return false
 		end
@@ -149,9 +150,9 @@
 	end
 end
 do
-	local ptn = [[https://vimeo.com/([0-9]+)]]
+	local ptn = regex[[https://vimeo.com/([0-9]+)]]
 	function video_handlers.vimeo(url)
-		local id = match(url,ptn)
+		local id = ptn.match(url)
 		if id == nil then
 			return false
 		end
@@ -161,9 +162,9 @@
 	end
 end
 do
-	local ptn = [[https://dai.ly/([a-z0-9]+)]]
+	local ptn = regex[[https://dai.ly/([a-z0-9]+)]]
 	function video_handlers.dailymotion(url)
-		local id = match(url,ptn)
+		local id = ptn.match(url)
 		if id == nil then
 			return false
 		end
@@ -173,9 +174,9 @@
 	end
 end
 do
-	local ptn = [[https://www.tiktok.com/[^/]+/video/([0-9]+)]]
+	local ptn = regex[[https://www.tiktok.com/[^/]+/video/([0-9]+)]]
 	function video_handlers.tiktok(url)
-		local id = match(url,ptn)
+		local id = ptn.match(url)
 		if id == nil then
 			return false
 		end
@@ -185,9 +186,9 @@
 	end
 end
 do
-	local ptn = [[\.[a-zA-Z0-9]+$]]
+	local ptn = regex[[\.[a-zA-Z0-9]+$]]
 	function video_handlers.file(url)
-		if not matches(url,ptn) then
+		if not ptn.matches(url) then
 			return false
 		end
 		%><video controls width="560"><source src="<%=html_encode(url)%>"></video><%
@@ -229,7 +230,7 @@
 function to_html(bbcode,options)
 	if options.strip_newline then
 		if type(bbcode) == "string" then
-			bbcode = gsub(bbcode,[[^\n]],"")
+			bbcode = starting_cr_regex.gsub(bbcode,"")
 		end
 		options.strip_newline = false
 	end
@@ -263,9 +264,11 @@
 	"video"
 }
 
+local url_regex = regex[[(^|\s)(https?://\S+)]]
+
 local function preprocess(bbcode)
 	if type(bbcode) == "string" then
-		bbcode = gsub( bbcode, [[(^|\s)(https?://\S+)]], "$1[url]$2[/url]" )
+		bbcode = url_regex.gsub( bbcode, "$1[url]$2[/url]" )
 		%><%= bbcode %><%
 	else
 		type(bbcode) == "table" or error()
@@ -303,7 +306,7 @@
 end
 
 function Bbcode.remove_html(text)
-	local ends_with_newline = matches(text,[[\n$]])
+	local ends_with_newline = ends_with(text,"\n")
 	local t = {}
 	local html = html_parse(text)
 	local is_first = true