view src/lib/Shared.luan @ 60:8b5b1bce7d6b

bcode menus
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Nov 2022 23:50:52 -0700
parents 7ce54f6d93f2
children
line wrap: on
line source

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 type = Luan.type or error()
local Html = require "luan:Html.luan"
local html_encode = Html.encode or error()
local Http = require "luan:http/Http.luan"
local Io = require "luan:Io.luan"
local uri = Io.uri or error()
local Parsers = require "luan:Parsers.luan"
local json_parse = Parsers.json_parse or error()
local Forum = require "site:/lib/Forum.luan"
local forum_title = Forum.title or error()
local mailer_url = Forum.mailer_url or error()
local User = require "site:/lib/User.luan"
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "Shared"


local Shared = {}

function Shared.head()
%>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<style>
			@import "/site.css";
		</style>
		<script src="/site.js"></script>
		<script src="/bbcode/bbcode.js"></script>
<%
end

function Shared.header()
	local user = User.current()
%>
		<p style="text-align:center">Yes the aesthetics suck.  Will be fixed later.</p>
		<div header>
			<a href="/"><%=forum_title%></a>
<%	if user == nil then %>
			<span>
				<a href="/login.html">login</a>
				/ <a href="/register.html">register</a>
			</span>
<%	else %>
			<a href="/account.html"><%=user.name_html%></a>
<%	end %>
		</div>
<%
end

function Shared.footer()
%>
		<div footer>
			<a href="/config.html">Configuration</a>
		</div>
<%
end

function Shared.admin_header()
%>
		<div header>
			<a href="/private/admin.html">Admin</a>
		</div>
<%
end

function Shared.base_url()
	return Http.request.scheme.."://"..Http.request.headers["host"]
end

function Shared.call_mail_api(cmd,parameters)
	local url = mailer_url.."/api/"..cmd..".json"
	local options = {
		parameters = parameters
	}
	local response = uri(url,options).read_text()
	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

function Shared.to_list(input)
	if input == nil then
		return {}
	elseif type(input) == "table" then
		return input
	else
		return {input}
	end
end

local function delete_post()
	%><a href="javascript:" onclick="deletePost(parentNode)">delete</a><%
end
Shared.delete_post = delete_post

function Shared.show_post(post,now)
	local Bbcode = require "site:/bbcode/Bbcode.luan"
	local bbcode_to_html = Bbcode.to_html or error()
%>
			<div post="<%=post.id%>">
				<hr>
				<div author>
					<img src="/images/profile.png">
					<a href="/whatever"><%= post.author.name %></a>
					<span ago date="<%=post.date%>"><% post.ago(now) %> ago</span>
				</div>
				<div output>
					<% bbcode_to_html(post.content) %>
					<p logged_in>
						<a href="#">reply</a>
						<span only_user="<%=html_encode(post.author.name)%>">
							- <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a>
							<span delete_opton> - <span delete><%delete_post()%></span> </span>
						<span>
					</p>
				</div>
				<div edit></div>
			</div>
<%
end

return Shared