changeset 13:24668255cede

preprocess urls
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Jul 2022 23:59:41 -0600
parents ad1604c72156
children 7d0c96408abf
files src/edit.html.luan src/lib/Bbcode.luan src/lib/Shared.luan src/reply.html.luan
diffstat 4 files changed, 73 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/edit.html.luan	Sun Jul 03 22:01:23 2022 -0600
+++ b/src/edit.html.luan	Sun Jul 03 23:59:41 2022 -0600
@@ -12,6 +12,8 @@
 local forum_title = Forum.title or error()
 local Post = require "site:/lib/Post.luan"
 local User = require "site:/lib/User.luan"
+local Bbcode = require "site:/lib/Bbcode.luan"
+local bbcode_preprocess = Bbcode.preprocess or error()
 
 
 return function()
@@ -20,7 +22,9 @@
 	local post_id = Http.request.parameters.post or error()
 	local post = Post.get_by_id(post_id) or error()
 	if Http.request.method == "POST" then
-		post.content = Http.request.parameters.content or error()
+		local content = Http.request.parameters.content or error()
+		content = bbcode_preprocess(content)
+		post.content = content
 		post.save()
 		Http.response.send_redirect("/thread.html?root="..post.root_id)
 		return
--- a/src/lib/Bbcode.luan	Sun Jul 03 22:01:23 2022 -0600
+++ b/src/lib/Bbcode.luan	Sun Jul 03 23:59:41 2022 -0600
@@ -3,13 +3,19 @@
 local type = Luan.type or error()
 local ipairs = Luan.ipairs or error()
 local stringify = Luan.stringify or error()
+local Io = require "luan:Io.luan"
+local output_of = Io.output_of or error()
 local Parsers = require "luan:Parsers.luan"
 local bbcode_parse = Parsers.bbcode_parse or error()
 local Html = require "luan:Html.luan"
 local html_encode = Html.encode or error()
 local Table = require "luan:Table.luan"
 local is_list = Table.is_list or error()
+local String = require "luan:String.luan"
+local gsub = String.gsub 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()
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "Bbcode"
 
@@ -111,4 +117,47 @@
 	%><div post><% to_html(bbcode) %></div><%
 end
 
+
+local doesnt_nest = list_to_set{
+	"url"
+	"code"
+	"img"
+	"video"
+}
+
+local function preprocess(bbcode)
+	if type(bbcode) == "string" then
+		bbcode = gsub( bbcode, [[(^|\s)(https?://\S+)]], "$1[url]$2[/url]" )
+		%><%= bbcode %><%
+	else
+		type(bbcode) == "table" or error()
+		if is_list(bbcode) then
+			for _, v in ipairs(bbcode) do
+				preprocess(v)
+			end
+		else
+			local name = bbcode.name
+			local param = bbcode.param
+			%>[<%=name%><%
+			if param ~= nil then
+				%>=<%=param%><%
+			end
+			%>]<%
+			if doesnt_nest[name] then
+				%><%=bbcode.contents%><%
+			else
+				preprocess(bbcode.contents)
+			end
+			%>[/<%=name%>]<%
+		end
+	end
+end
+
+function Bbcode.preprocess(bbcode)
+	bbcode = bbcode_parse(bbcode)
+	return output_of(function()
+		preprocess(bbcode)
+	end)
+end
+
 return Bbcode
--- a/src/lib/Shared.luan	Sun Jul 03 22:01:23 2022 -0600
+++ b/src/lib/Shared.luan	Sun Jul 03 23:59:41 2022 -0600
@@ -1,5 +1,7 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local set_metatable = Luan.set_metatable or error()
 local Http = require "luan:http/Http.luan"
 local Io = require "luan:Io.luan"
 local uri = Io.uri or error()
@@ -69,4 +71,18 @@
 	return json_parse(response)
 end
 
+local set_mt = {}
+function set_mt.__index(table,key)
+	return false
+end
+
+function Shared.list_to_set(list)
+	local set = {}
+	for _, v in ipairs(list) do
+		set[v] = true
+	end
+	set_metatable(set,set_mt)
+	return set
+end
+
 return Shared
--- a/src/reply.html.luan	Sun Jul 03 22:01:23 2022 -0600
+++ b/src/reply.html.luan	Sun Jul 03 23:59:41 2022 -0600
@@ -10,6 +10,8 @@
 local forum_title = Forum.title or error()
 local Post = require "site:/lib/Post.luan"
 local User = require "site:/lib/User.luan"
+local Bbcode = require "site:/lib/Bbcode.luan"
+local bbcode_preprocess = Bbcode.preprocess or error()
 
 
 return function()
@@ -19,6 +21,7 @@
 	local parent = Post.get_by_id(parent_id) or error()
 	if Http.request.method == "POST" then
 		local content = Http.request.parameters.content or error()
+		content = bbcode_preprocess(content)
 		local post = parent.reply(user,content)
 		Http.response.send_redirect("/thread.html?root="..post.root_id)
 		return