local std = require("core.std")
local M = {}
M.__index = M
local function encode(str)
return str:gsub("([^%w%-%_%.%~])", function(c)
return string.format("%%%02X", string.byte(c)) end) end
function M.fes(header, footer)
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
local raw_favicon = site_config.favicon or [[]]
local self = {
version = site_config.version or "",
title = site_config.title or "Document",
copyright = site_config.copyright or "© The Copyright Holder",
favicon = "data:image/svg+xml," .. encode(raw_favicon),
header = header or [[
{{TITLE}}
]],
footer = footer or [[
]],
parts = {}
}
return setmetatable(self, M)
end
function M:custom(str)
table.insert(self.parts, str)
return self
end
for name, func in pairs(std) do
if type(func) == "function" then
M[name] = function(self, ...)
local result = func(...)
table.insert(self.parts, result)
return self
end
end
end
function M:build()
local header = self.header
local safe_title = self.title or "Document"
local safe_favicon = self.favicon:gsub("%%", "%%%%")
header = header:gsub("{{TITLE}}", safe_title)
header = header:gsub("{{FAVICON}}", safe_favicon)
local footer = self.footer:gsub("{{COPYRIGHT}}", self.copyright or "© The Copyright Holder")
return header .. table.concat(self.parts, "\n") .. footer
end
M.__tostring = function(self)
return self:build()
end
return M