view src/lib/Bbcode.luan @ 23:cdcd1b70c15e

tinymce work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 Jul 2022 23:50:48 -0600
parents 33731231093a
children 6871eec2cf4c
line wrap: on
line source

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 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 html_parse = Html.parse or error()
local Table = require "luan:Table.luan"
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 User = require "site:/lib/User.luan"
local Shared = require "site:/lib/Shared.luan"
local list_to_set = Shared.list_to_set or error()
local to_list = Shared.to_list or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "Bbcode"


local Bbcode = {}

local to_html
local html = {}

function html.b(bbcode,options)
	%><b><% to_html(bbcode.contents,options) %></b><%
end

function html.i(bbcode,options)
	%><i><% to_html(bbcode.contents,options) %></i><%
end

function html.u(bbcode,options)
	%><u><% to_html(bbcode.contents,options) %></u><%
end

function html.s(bbcode,options)
	%><s><% to_html(bbcode.contents,options) %></s><%
end

function html.sup(bbcode,options)
	%><sup><% to_html(bbcode.contents,options) %></sup><%
end

function html.brackets(bbcode,options)
	%>[<% to_html(bbcode.contents,options) %>]<%
end

function html.url(bbcode,options)
	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,options) %></a><%
	end
end

function html.code(bbcode,options)
	local s = gsub(bbcode.contents,[[^\n]],"")
	%><code><%= html_encode(s) %></code><%
	options.strip_newline = true
end

function html.img(bbcode,options)
	%><img src="<%= html_encode(bbcode.contents) %>"><%
end

function html.color(bbcode,options)
	%><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><%
end

function html.size(bbcode,options)
	%><span style="font-size:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><%
end

function html.quote(bbcode,options)
	%><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
	else
		options.strip_newline = true
	end
	to_html(bbcode.contents,options)
	%></blockquote><%
	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]+)/]]

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
	url = html_encode(url)
	%><a href="<%=url%>"><%=url%></a><%
end

local function list_to_html(bbcode,options)
	local list = to_list(bbcode.contents)
	for _, item in ipairs(list) do
		if type(item) == "table" and item.name == "li" then
			%><li><% to_html(item.contents,options) %></li><%
		end
	end
	options.strip_newline = true
end

function html.ul(bbcode,options)
	%><ul><%
	list_to_html(bbcode,options)
	%></ul><%
end

function html.ol(bbcode,options)
	%><ol><%
	list_to_html(bbcode,options)
	%></ol><%
end

function to_html(bbcode,options)
	if options.strip_newline then
		if type(bbcode) == "string" then
			bbcode = gsub(bbcode,[[^\n]],"")
		end
		options.strip_newline = false
	end
	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,options)
			end
		else
			local fn = html[bbcode.name] or error(bbcode.name.." not handled")
			fn(bbcode,options)
		end
	end
end

function Bbcode.to_html(bbcode)
	bbcode = bbcode_parse(bbcode)
	%><div message><%
	to_html(bbcode,{strip_newline=false})
	%></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
			if name == "code" and param ~= nil then
				%>[/<%=name%>=<%=param%>]<%
			else
				%>[/<%=name%>]<%
			end
		end
	end
end

function Bbcode.preprocess(bbcode)
	bbcode = bbcode_parse(bbcode)
	return output_of(function()
		preprocess(bbcode)
	end)
end

function Bbcode.remove_html(text)
	local ends_with_newline = matches(text,[[\n$]])
	local t = {}
	local html = html_parse(text)
	local is_first = true
	for _, v in ipairs(html) do
		if type(v) == "string" then
			t[#t+1] = v
		else
			local name = v.name
			if name == "div" then
				if not is_first then
					t[#t+1] = "\n"
				end
			elseif name == "/div" or name == "br" then
				-- ignore
			else
				error("unexpected tag: "..name)
			end
		end
		is_first = false
	end
	if not ends_with_newline then
		t[#t+1] = "\n"
	end
	return concat(t)
end


return Bbcode