changeset 12:ad1604c72156

fix bbcode
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Jul 2022 22:01:23 -0600
parents 3ed1e3f3a53a
children 24668255cede
files src/lib/Bbcode.luan src/thread.html.luan
diffstat 2 files changed, 117 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
diff -r 3ed1e3f3a53a -r ad1604c72156 src/lib/Bbcode.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Bbcode.luan	Sun Jul 03 22:01:23 2022 -0600
@@ -0,0 +1,114 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local type = Luan.type or error()
+local ipairs = Luan.ipairs or error()
+local stringify = Luan.stringify 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 User = require "site:/lib/User.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "Bbcode"
+
+
+local Bbcode = {}
+
+local to_html
+local html = {}
+
+function html.b(bbcode)
+	%><b><% to_html(bbcode.contents) %></b><%
+end
+
+function html.i(bbcode)
+	%><i><% to_html(bbcode.contents) %></i><%
+end
+
+function html.u(bbcode)
+	%><u><% to_html(bbcode.contents) %></u><%
+end
+
+function html.url(bbcode)
+	local url = bbcode.param
+	if url == nil then
+		url = html_encode(bbcode.contents)
+		%><a href="<%=url%>"><%=url%></a><%
+	else
+		url = html_encode(url)
+		%><a href="<%=url%>"><% to_html(bbcode.contents) %></a><%
+	end
+end
+
+function html.code(bbcode)
+	%><code><%= html_encode(bbcode.contents) %></code><%
+end
+
+function html.img(bbcode)
+	%><img src="<%= html_encode(bbcode.contents) %>"><%
+end
+
+function html.color(bbcode)
+	%><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents) %></span><%
+end
+
+function html.size(bbcode)
+	%><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents) %></span><%
+end
+
+function html.quote(bbcode)
+	%><blockquote><%
+	local user_name = bbcode.param
+	if user_name ~= nil then
+		local user = User.get_by_name(user_name)
+		if user == nil then
+			%><%= user_name %> wrote:<%
+		else
+			%><a href="/user_something"><%= user_name %></a> wrote:<%
+		end
+	end
+	to_html(bbcode.contents)
+	%></blockquote><%
+end
+
+function html.video(bbcode)
+	local url = html_encode(bbcode.contents)
+	local site = bbcode.site
+	if site == "youtube" then
+		%><iframe width="420" height="315" src="https://www.youtube.com/embed/<%=bbcode.id%><%
+		local start = bbcode.start
+		if start ~= nil then
+			%>?start=<%=start%><%
+		end
+		%>" frameborder="0" allowfullscreen></iframe><%
+	elseif site == "bitchute" then
+		%><iframe width="420" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/<%=bbcode.id%>/"></iframe><%
+	else
+		%><a href="<%=url%>"><%=url%></a><%
+	end
+end
+
+function to_html(bbcode)
+	if type(bbcode) == "string" then
+		%><%= html_encode(bbcode) %><%
+	else
+		type(bbcode) == "table" or error()
+		if is_list(bbcode) then
+			for _, v in ipairs(bbcode) do
+				to_html(v)
+			end
+		else
+			local fn = html[bbcode.name] or error(bbcode.name.." not handled")
+			fn(bbcode)
+		end
+	end
+end
+
+function Bbcode.to_html(bbcode)
+	bbcode = bbcode_parse(bbcode)
+	%><div post><% to_html(bbcode) %></div><%
+end
+
+return Bbcode
diff -r 3ed1e3f3a53a -r ad1604c72156 src/thread.html.luan
--- a/src/thread.html.luan	Thu Jun 30 20:05:11 2022 -0600
+++ b/src/thread.html.luan	Sun Jul 03 22:01:23 2022 -0600
@@ -1,12 +1,7 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
-local Html = require "luan:Html.luan"
-local html_encode = Html.encode or error()
-local Parsers = require "luan:Parsers.luan"
-local bbcode_to_html = Parsers.bbcode_to_html or error()
 local Io = require "luan:Io.luan"
-local output_of = Io.output_of or error()
 local Http = require "luan:http/Http.luan"
 local Shared = require "site:/lib/Shared.luan"
 local head = Shared.head or error()
@@ -16,24 +11,9 @@
 local forum_title = Forum.title or error()
 local Db = require "site:/lib/Db.luan"
 local Post = require "site:/lib/Post.luan"
-local User = require "site:/lib/User.luan"
-
+local Bbcode = require "site:/lib/Bbcode.luan"
+local bbcode_to_html = Bbcode.to_html or error()
 
-local function quoter(content,user_name)
-	return output_of( function() 
-		%><blockquote><%
-		if user_name ~= nil then
-			local user = User.get_by_name(user_name)
-			if user == nil then
-				%><%= html_encode(user_name) %> wrote:<%
-			else
-				%><a href="/user_somthing"><%= html_encode(user_name) %></a> wrote:<%
-			end
-		end
-		%><%= content %><%
-		%></blockquote><%
-	end_function )
-end
 
 return function()
 	local root_id = Http.request.parameters.root or error()
@@ -59,7 +39,7 @@
 			<h1><%=subject_html%></h1>
 <%	for _, post in ipairs(posts) do %>
 			<hr>
-			<div post><%=bbcode_to_html(post.content,quoter)%></div>
+			<% bbcode_to_html(post.content) %>
 			<p>
 				<a href="/reply.html?parent=<%=post.id%>">reply</a>
 				- <a href="/edit.html?post=<%=post.id%>">edit</a>