diff src/lib/Bbcode.luan @ 21:33731231093a

fix bbcode editing
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 Jul 2022 08:24:52 -0600
parents 3ea49246d6a7
children cdcd1b70c15e
line wrap: on
line diff
--- a/src/lib/Bbcode.luan	Wed Jul 13 22:00:00 2022 -0600
+++ b/src/lib/Bbcode.luan	Sun Jul 17 08:24:52 2022 -0600
@@ -9,10 +9,13 @@
 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 User = require "site:/lib/User.luan"
 local Shared = require "site:/lib/Shared.luan"
 local list_to_set = Shared.list_to_set or error()
@@ -212,4 +215,33 @@
 	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