large changes
This commit is contained in:
155
lib/std.lua
155
lib/std.lua
@@ -1,155 +1,157 @@
|
||||
local M = {}
|
||||
|
||||
function M.fes_version()
|
||||
local fes_mod = package.loaded.fes
|
||||
if fes_mod and fes_mod.config and fes_mod.config.fes and fes_mod.config.fes.version then
|
||||
return fes_mod.config.fes.version
|
||||
end
|
||||
return ""
|
||||
end
|
||||
function M.element(tag, attrs, content)
|
||||
local out = { "<", tag }
|
||||
|
||||
function M.site_version()
|
||||
local fes_mod = package.loaded.fes
|
||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.version then
|
||||
return fes_mod.config.site.version
|
||||
if attrs then
|
||||
for k, v in pairs(attrs) do
|
||||
if v ~= false and v ~= nil then
|
||||
if v == true then
|
||||
out[#out + 1] = " " .. k
|
||||
else
|
||||
out[#out + 1] = " " .. k .. "=\"" .. tostring(v) .. "\""
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return ""
|
||||
|
||||
if content == nil then
|
||||
out[#out + 1] = " />"
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
out[#out + 1] = ">"
|
||||
out[#out + 1] = tostring(content)
|
||||
out[#out + 1] = "</"
|
||||
out[#out + 1] = tag
|
||||
out[#out + 1] = ">"
|
||||
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
function M.a(link, str)
|
||||
link = link or "https://example.com"
|
||||
str = str or link
|
||||
return '<a href="' .. link .. '">' .. str .. "</a>"
|
||||
return M.element("a", { href = link }, str)
|
||||
end
|
||||
|
||||
function M.ha(link, str)
|
||||
link = link or "https://example.com"
|
||||
str = str or link
|
||||
return '<a class="hidden" href="' .. link .. '">' .. str .. "</a>"
|
||||
return M.element("a", { href = link, class = "hidden" }, str)
|
||||
end
|
||||
|
||||
function M.external(link, str)
|
||||
return '<a target="_blank" href="' .. link .. '">' .. str .. "</a>"
|
||||
return M.element("a", { href = link, target = "_blank" }, str)
|
||||
end
|
||||
|
||||
function M.note(str)
|
||||
return '<div class="note">' .. str .. "</div>"
|
||||
return M.element("div", { class = "note" }, str)
|
||||
end
|
||||
|
||||
function M.muted(str)
|
||||
return '<div class="muted">' .. str .. "</div>"
|
||||
return M.element("div", { class = "muted" }, str)
|
||||
end
|
||||
|
||||
function M.callout(str)
|
||||
return '<div class="callout">' .. str .. "</div>"
|
||||
return M.element("div", { class = "callout" }, str)
|
||||
end
|
||||
|
||||
function M.h1(str)
|
||||
return "<h1>" .. (str or "") .. "</h1>"
|
||||
return M.element("h1", nil, str or "")
|
||||
end
|
||||
|
||||
function M.h2(str)
|
||||
return "<h2>" .. (str or "") .. "</h2>"
|
||||
return M.element("h2", nil, str or "")
|
||||
end
|
||||
|
||||
function M.h3(str)
|
||||
return "<h3>" .. (str or "") .. "</h3>"
|
||||
return M.element("h3", nil, str or "")
|
||||
end
|
||||
|
||||
function M.h4(str)
|
||||
return "<h4>" .. (str or "") .. "</h4>"
|
||||
return M.element("h4", nil, str or "")
|
||||
end
|
||||
|
||||
function M.h5(str)
|
||||
return "<h5>" .. (str or "") .. "</h5>"
|
||||
return M.element("h5", nil, str or "")
|
||||
end
|
||||
|
||||
function M.h6(str)
|
||||
return "<h6>" .. (str or "") .. "</h6>"
|
||||
return M.element("h6", nil, str or "")
|
||||
end
|
||||
|
||||
function M.p(str)
|
||||
return "<p>" .. (str or "") .. "</p>"
|
||||
return M.element("p", nil, str or "")
|
||||
end
|
||||
|
||||
function M.pre(str)
|
||||
return "<pre>" .. (str or "") .. "</pre>"
|
||||
return M.element("pre", nil, str or "")
|
||||
end
|
||||
|
||||
function M.code(str)
|
||||
return "<pre><code>" .. (str or "") .. "</code></pre>"
|
||||
return M.element("pre", nil, M.element("code", nil, str or ""))
|
||||
end
|
||||
|
||||
function M.ul(items)
|
||||
items = items or {}
|
||||
local html = "<ul>"
|
||||
local out = {}
|
||||
for _, item in ipairs(items) do
|
||||
html = html .. "<li>" .. tostring(item) .. "</li>"
|
||||
out[#out + 1] = M.element("li", nil, item)
|
||||
end
|
||||
html = html .. "</ul>"
|
||||
return html
|
||||
return M.element("ul", nil, table.concat(out))
|
||||
end
|
||||
|
||||
function M.ol(items)
|
||||
items = items or {}
|
||||
local html = "<ol>"
|
||||
local out = {}
|
||||
for _, item in ipairs(items) do
|
||||
html = html .. "<li>" .. tostring(item) .. "</li>"
|
||||
out[#out + 1] = M.element("li", nil, item)
|
||||
end
|
||||
html = html .. "</ol>"
|
||||
return html
|
||||
return M.element("ol", nil, table.concat(out))
|
||||
end
|
||||
|
||||
function M.tl(items)
|
||||
items = items or {}
|
||||
local html = '<ul class="tl">'
|
||||
local out = {}
|
||||
for _, item in ipairs(items) do
|
||||
html = html .. "<li>" .. tostring(item) .. "</li>"
|
||||
out[#out + 1] = M.element("li", nil, item)
|
||||
end
|
||||
html = html .. "</ul>"
|
||||
return html
|
||||
return M.element("ul", { class = "tl" }, table.concat(out))
|
||||
end
|
||||
|
||||
function M.blockquote(str)
|
||||
return "<blockquote>" .. (str or "") .. "</blockquote>"
|
||||
return M.element("blockquote", nil, str or "")
|
||||
end
|
||||
|
||||
function M.hr()
|
||||
return "<hr>"
|
||||
return M.element("hr")
|
||||
end
|
||||
|
||||
function M.img(src, alt)
|
||||
src = src or ""
|
||||
alt = alt or ""
|
||||
return '<img src="' .. src .. '" alt="' .. alt .. '">'
|
||||
return M.element("img", { src = src or "", alt = alt or "" })
|
||||
end
|
||||
|
||||
function M.strong(str)
|
||||
return "<strong>" .. (str or "") .. "</strong>"
|
||||
return M.element("strong", nil, str or "")
|
||||
end
|
||||
|
||||
function M.em(str)
|
||||
return "<em>" .. (str or "") .. "</em>"
|
||||
return M.element("em", nil, str or "")
|
||||
end
|
||||
|
||||
function M.br()
|
||||
return "<br>"
|
||||
return M.element("br")
|
||||
end
|
||||
|
||||
function M.div(content, class)
|
||||
content = content or ""
|
||||
class = class or ""
|
||||
local class_attr = class ~= "" and (' class="' .. class .. '"') or ""
|
||||
return "<div" .. class_attr .. ">" .. content .. "</div>"
|
||||
return M.element("div", class and { class = class } or nil, content or "")
|
||||
end
|
||||
|
||||
function M.span(content, class)
|
||||
content = content or ""
|
||||
class = class or ""
|
||||
local class_attr = class ~= "" and (' class="' .. class .. '"') or ""
|
||||
return "<span" .. class_attr .. ">" .. content .. "</span>"
|
||||
return M.element("span", class and { class = class } or nil, content or "")
|
||||
end
|
||||
|
||||
-- HTML escaping utility
|
||||
function M.escape(str)
|
||||
str = tostring(str or "")
|
||||
str = str:gsub("&", "&")
|
||||
@@ -160,55 +162,28 @@ function M.escape(str)
|
||||
return str
|
||||
end
|
||||
|
||||
-- Get site name from config
|
||||
function M.site_name()
|
||||
local fes_mod = package.loaded.fes
|
||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.name then
|
||||
return fes_mod.config.site.name
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
-- Get site title from config
|
||||
function M.site_title()
|
||||
local fes_mod = package.loaded.fes
|
||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.title then
|
||||
return fes_mod.config.site.title
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
-- Get site authors from config
|
||||
function M.site_authors()
|
||||
local fes_mod = package.loaded.fes
|
||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.authors then
|
||||
return fes_mod.config.site.authors
|
||||
end
|
||||
return {}
|
||||
end
|
||||
|
||||
function M.highlight(str)
|
||||
return '<span class="highlight">' .. (str or "") .. "</span>"
|
||||
return M.element("span", { class = "highlight" }, str or "")
|
||||
end
|
||||
|
||||
function M.banner(str)
|
||||
return '<div class="banner">' .. (str or "") .. "</div>"
|
||||
return M.element("div", { class = "banner" }, str or "")
|
||||
end
|
||||
|
||||
function M.center(str)
|
||||
return '<div class="center">' .. (str or "") .. "</div>"
|
||||
return M.element("div", { class = "center" }, str or "")
|
||||
end
|
||||
|
||||
function M.nav(link, str)
|
||||
link = link or "example.com"
|
||||
str = str or link
|
||||
return '<a class="nav" href="' .. link .. '">' .. str .. "</a>"
|
||||
return M.element("a", { href = link, class = "nav" }, str)
|
||||
end
|
||||
|
||||
function M.rl(r, l)
|
||||
r = r or ""
|
||||
l = l or ""
|
||||
return string.format('<span class="left">%s</span><span class="right">%s</span>', r, l)
|
||||
return
|
||||
M.element("span", { class = "left" }, r or "") ..
|
||||
M.element("span", { class = "right" }, l or "")
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user