changeset 20:3ea49246d6a7

bbcode work
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 13 Jul 2022 22:00:00 -0600
parents da006d1c1eba
children 33731231093a
files src/lib/Bbcode.luan src/lib/Shared.luan src/site.css src/site.js src/test/editor.html src/thread.html.luan
diffstat 6 files changed, 127 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/Bbcode.luan	Wed Jul 13 08:47:13 2022 -0600
+++ b/src/lib/Bbcode.luan	Wed Jul 13 22:00:00 2022 -0600
@@ -16,6 +16,7 @@
 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"
 
@@ -25,46 +26,60 @@
 local to_html
 local html = {}
 
-function html.b(bbcode)
-	%><b><% to_html(bbcode.contents) %></b><%
+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.i(bbcode)
-	%><i><% to_html(bbcode.contents) %></i><%
+function html.s(bbcode,options)
+	%><s><% to_html(bbcode.contents,options) %></s><%
 end
 
-function html.u(bbcode)
-	%><u><% to_html(bbcode.contents) %></u><%
+function html.sup(bbcode,options)
+	%><sup><% to_html(bbcode.contents,options) %></sup><%
 end
 
-function html.url(bbcode)
+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) %></a><%
+		%><a href="<%=url%>"><% to_html(bbcode.contents,options) %></a><%
 	end
 end
 
-function html.code(bbcode)
-	%><code><%= html_encode(bbcode.contents) %></code><%
+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)
+function html.img(bbcode,options)
 	%><img src="<%= html_encode(bbcode.contents) %>"><%
 end
 
-function html.color(bbcode)
-	%><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents) %></span><%
+function html.color(bbcode,options)
+	%><span style="color:<%=bbcode.param%>"><% to_html(bbcode.contents,options) %></span><%
 end
 
-function html.size(bbcode)
-	%><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents) %></span><%
+function html.size(bbcode,options)
+	%><span style="font-size:<%=bbcode.param%>%"><% to_html(bbcode.contents,options) %></span><%
 end
 
-function html.quote(bbcode)
+function html.quote(bbcode,options)
 	%><blockquote><%
 	local user_name = bbcode.param
 	if user_name ~= nil then
@@ -74,12 +89,15 @@
 		else
 			%><a href="/user_something"><%= user_name %></a> wrote:<%
 		end
+	else
+		options.strip_newline = true
 	end
-	to_html(bbcode.contents)
+	to_html(bbcode.contents,options)
 	%></blockquote><%
+	options.strip_newline = true
 end
 
-function html.video(bbcode)
+function html.video(bbcode,options)
 	local url = html_encode(bbcode.contents)
 	local site = bbcode.site
 	if site == "youtube" then
@@ -96,25 +114,55 @@
 	end
 end
 
-function to_html(bbcode)
+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)
+				to_html(v,options)
 			end
 		else
 			local fn = html[bbcode.name] or error(bbcode.name.." not handled")
-			fn(bbcode)
+			fn(bbcode,options)
 		end
 	end
 end
 
 function Bbcode.to_html(bbcode)
 	bbcode = bbcode_parse(bbcode)
-	%><div message><% to_html(bbcode) %></div><%
+	%><div message><%
+	to_html(bbcode,{strip_newline=false})
+	%></div><%
 end
 
 
@@ -148,7 +196,11 @@
 			else
 				preprocess(bbcode.contents)
 			end
-			%>[/<%=name%>]<%
+			if name == "code" and param ~= nil then
+				%>[/<%=name%>=<%=param%>]<%
+			else
+				%>[/<%=name%>]<%
+			end
 		end
 	end
 end
--- a/src/lib/Shared.luan	Wed Jul 13 08:47:13 2022 -0600
+++ b/src/lib/Shared.luan	Wed Jul 13 22:00:00 2022 -0600
@@ -2,6 +2,7 @@
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
 local set_metatable = Luan.set_metatable or error()
+local type = Luan.type or error()
 local Http = require "luan:http/Http.luan"
 local Io = require "luan:Io.luan"
 local uri = Io.uri or error()
@@ -85,4 +86,14 @@
 	return set
 end
 
+function Shared.to_list(input)
+	if input == nil then
+		return {}
+	elseif type(input) == "table" then
+		return input
+	else
+		return {input}
+	end
+end
+
 return Shared
--- a/src/site.css	Wed Jul 13 08:47:13 2022 -0600
+++ b/src/site.css	Wed Jul 13 22:00:00 2022 -0600
@@ -15,11 +15,6 @@
 	text-decoration: underline;
 }
 
-textarea {
-	font: inherit;
-	xpadding: 7px;
-	xborder-color: #DDDDDD;
-}
 div[contenteditable] {
 	padding: 7px;
 	border: 1px solid #777;
--- a/src/site.js	Wed Jul 13 08:47:13 2022 -0600
+++ b/src/site.js	Wed Jul 13 22:00:00 2022 -0600
@@ -32,6 +32,25 @@
 	ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
 };
 
-function time(time) {
-	document.write(new Date(time).toLocaleString());
+function editorEnter() {
+	let s = getSelection();
+	let r = s.getRangeAt(0);
+	r.deleteContents();
+	let t = document.createTextNode('\n');
+	r.insertNode(t);
+	let isFucked = getSelection().getRangeAt(0).collapsed;
+	//console.log(isFucked);
+	r.collapse();
+	if(isFucked) {
+		s.removeAllRanges();
+		s.addRange(r);
+	}
 }
+
+function editorKey() {
+	//console.log(event);
+	if( event.code === 'Enter' ) {
+		editorEnter();
+		return false;
+	}
+}
--- a/src/test/editor.html	Wed Jul 13 08:47:13 2022 -0600
+++ b/src/test/editor.html	Wed Jul 13 22:00:00 2022 -0600
@@ -9,7 +9,23 @@
 				white-space: pre-wrap;
 			}
 		</style>
+		<script src="/site.js"></script>
 		<script>
+			function getBrowser() {
+				let userAgent = navigator.userAgent;
+				if( userAgent.match(/chrome|chromium|crios/i) )
+					return 'chrome';
+				if( userAgent.match(/firefox|fxios/i) )
+					return 'firefox';
+				if( userAgent.match(/safari/i) )
+					return 'safari';
+				if( userAgent.match(/opr\//i) )
+					return 'opera';
+				if( userAgent.match(/edg/i) )
+					return 'edge';
+				return "unknown";
+			}
+
 			function log() {
 				let edit = document.querySelector('[contentEditable]');
 				console.log(edit.innerHTML);
@@ -18,25 +34,19 @@
 			function test() {
 				let edit = document.querySelector('[contentEditable]');
 				edit.focus();
-				let s = getSelection();
-				let r = s.getRangeAt(0);
-				//console.log(s);
-				r.deleteContents();
-				let t = document.createTextNode('\n');
-				r.insertNode(t);
-				r.collapse();
+				editorEnter();
 			}
 		</script>
 	</head>
 	<body>
 		<p>top</p>
-		<div contentEditable>
+		<div contentEditable onkeypress="return editorKey()">
 aaa <b>bbb</b> <i>iii</i>
 1
 2
 3
 zzz
-		</div>
+</div>
 		<p>
 			<button onclick="log()">log</button>
 			<button onclick="test()">test</button>
--- a/src/thread.html.luan	Wed Jul 13 08:47:13 2022 -0600
+++ b/src/thread.html.luan	Wed Jul 13 22:00:00 2022 -0600
@@ -134,7 +134,7 @@
 		<span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span>
 		<span hidden undelete><%deletePost()%></span>
 		<div hidden edit>
-			<div contentEditable></div>
+			<div contentEditable onkeypress="return editorKey()"></div>
 			<p>
 				<button onclick="saveEdit(this)">save</button>
 				<button onclick="cancelEdit(this)">cancel</button>