local std = require("lib.std")
local symbol = require("lib.symbol")
local M = {}
M.__index = M
local default_html_header = [[
{{FAVICON}}
{{TITLE}}
]]
local default_html_footer = [[
]]
local default_gemini_header = [[
]]
local default_gemini_footer = [[
=> https://git.vxserver.dev/fSD/fes Fes Powered
=> https://www.lua.org Lua Powered
=> https://git.vxserver.dev/fSD/fes/src/branch/main/LICENSE ISC Licensed
{{COPYRIGHT}}
]]
function M.fes(proto, header, footer)
proto = proto or "http"
std.proto = proto
local config = {}
local site_config = {}
local fes_mod = package.loaded.fes
if fes_mod and fes_mod.config then
config = fes_mod.config
if config.site then
site_config = config.site
end
end
if proto == "http" and site_config.favicon then
site_config.favicon =
''
end
local default_header
local default_footer
if proto == "http" then
default_header = default_html_header
default_footer = default_html_footer
elseif proto == "gemini" then
default_header = default_gemini_header
default_footer = default_gemini_footer
end
local self = {
version = site_config.version,
title = site_config.title,
copyright = site_config.copyright,
favicon = site_config.favicon,
header = header or default_header,
footer = footer or default_footer,
parts = {},
proto = proto,
}
return setmetatable(self, M)
end
function M:raw(str)
table.insert(self.parts, (str or "") .. "\n")
return self
end
for name, func in pairs(std) do
if type(func) == "function" then
M[name] = function(self, ...)
table.insert(self.parts, func(...))
return self
end
end
end
function M:build()
if self.proto == "http" then
local header = self.header:gsub("{{TITLE}}", self.title or "Document")
local favicon_html = self.favicon or ''
header = header:gsub("{{FAVICON}}", favicon_html)
local footer = self.footer:gsub("{{COPYRIGHT}}", self.copyright or symbol.legal.copyright .. "The Copyright Holder")
return header .. table.concat(self.parts, "\n") .. footer
end
return table.concat(self.parts, "\n")
end
M.__tostring = function(self)
return self:build()
end
return M