Files
fes/lib/std.lua

131 lines
2.4 KiB
Lua

local M = {}
M.proto = "http"
local function isHttp()
return M.proto == "http"
end
local function isGemini()
return M.proto == "gemini"
end
local function esc(s)
s = s or ""
s = s:gsub("&", "&")
s = s:gsub("<", "&lt;")
s = s:gsub(">", "&gt;")
return s
end
M.p = function(s)
s = s or ""
if isHttp() then
return "<p>" .. esc(s) .. "</p>"
elseif isGemini() then
return s
end
end
M.h = function(level, s)
level = tonumber(level) or 1
if level < 1 then level = 1 end
if level > 6 then level = 6 end
s = s or ""
if isHttp() then
return "<h" .. level .. ">" .. esc(s) .. "</h" .. level .. ">"
elseif isGemini() then
return string.rep("#", level) .. " " .. s
end
end
M.codeblock = function(s)
s = s or ""
if isHttp() then
return "<pre><code>" .. esc(s) .. "</code></pre>"
elseif isGemini() then
return "```\n" .. s .. "\n```"
end
end
M.inline = function(s)
s = s or ""
if isHttp() then
return "<code>" .. esc(s) .. "</code>"
elseif isGemini() then
return "`" .. s .. "`"
end
end
M.link = function(text, url)
text = text or ""
url = url or ""
if isHttp() then
return "<a href=\"" .. esc(url) .. "\">" .. esc(text) .. "</a>"
elseif isGemini() then
return "=> " .. url .. " " .. text
end
end
M.list = function(items, ordered)
items = items or {}
if isHttp() then
local tag = ordered and "ol" or "ul"
local out = "<" .. tag .. ">"
for _, v in ipairs(items) do
out = out .. "<li>" .. esc(v) .. "</li>"
end
out = out .. "</" .. tag .. ">"
return out
elseif isGemini() then
local out = {}
for i, v in ipairs(items) do
if ordered then
table.insert(out, i .. ". " .. v)
else
table.insert(out, "* " .. v)
end
end
return table.concat(out, "\n")
end
end
M.blockquote = function(s)
s = s or ""
if isHttp() then
return "<blockquote>" .. esc(s) .. "</blockquote>"
elseif isGemini() then
return "> " .. s
end
end
M.rule = function()
if isHttp() then
return "<hr />"
elseif isGemini() then
return "---"
end
end
M.image = function(alt, src)
alt = alt or ""
src = src or ""
if isHttp() then
return "<img src=\"" .. esc(src) .. "\" alt=\"" .. esc(alt) .. "\" />"
elseif isGemini() then
return "=> " .. src .. " " .. alt
end
end
M.file = function(text, url)
text = text or ""
url = url or ""
if isHttp() then
return "<a href=\"" .. esc(url) .. "\" download>" .. esc(text) .. "</a>"
elseif isGemini() then
return "=> " .. url .. " " .. text
end
end
return M