save
This commit is contained in:
89
lib/std.lua
89
lib/std.lua
@@ -1,6 +1,7 @@
|
||||
local M = {}
|
||||
|
||||
M.proto = "http"
|
||||
M.__fes_banner_set = false
|
||||
|
||||
local function isHttp()
|
||||
return M.proto == "http"
|
||||
@@ -10,18 +11,10 @@ local function isGemini()
|
||||
return M.proto == "gemini"
|
||||
end
|
||||
|
||||
local function esc(s)
|
||||
s = s or ""
|
||||
s = s:gsub("&", "&")
|
||||
s = s:gsub("<", "<")
|
||||
s = s:gsub(">", ">")
|
||||
return s
|
||||
end
|
||||
|
||||
M.p = function(s)
|
||||
s = s or ""
|
||||
if isHttp() then
|
||||
return "<p>" .. esc(s) .. "</p>"
|
||||
return "<p>" .. s .. "</p>"
|
||||
elseif isGemini() then
|
||||
return s
|
||||
end
|
||||
@@ -33,7 +26,7 @@ M.h = function(level, s)
|
||||
if level > 6 then level = 6 end
|
||||
s = s or ""
|
||||
if isHttp() then
|
||||
return "<h" .. level .. ">" .. esc(s) .. "</h" .. level .. ">"
|
||||
return "<h" .. level .. ">" .. s .. "</h" .. level .. ">"
|
||||
elseif isGemini() then
|
||||
return string.rep("#", level) .. " " .. s
|
||||
end
|
||||
@@ -42,7 +35,7 @@ end
|
||||
M.codeblock = function(s)
|
||||
s = s or ""
|
||||
if isHttp() then
|
||||
return "<pre><code>" .. esc(s) .. "</code></pre>"
|
||||
return "<pre><code>" .. s .. "</code></pre>"
|
||||
elseif isGemini() then
|
||||
return "```\n" .. s .. "\n```"
|
||||
end
|
||||
@@ -51,17 +44,17 @@ end
|
||||
M.inline = function(s)
|
||||
s = s or ""
|
||||
if isHttp() then
|
||||
return "<code>" .. esc(s) .. "</code>"
|
||||
return "<code>" .. s .. "</code>"
|
||||
elseif isGemini() then
|
||||
return "`" .. s .. "`"
|
||||
end
|
||||
end
|
||||
|
||||
M.link = function(text, url)
|
||||
text = text or ""
|
||||
M.link = function(url, text)
|
||||
url = url or ""
|
||||
text = text or url
|
||||
if isHttp() then
|
||||
return "<a href=\"" .. esc(url) .. "\">" .. esc(text) .. "</a>"
|
||||
return "<a href=\"" .. url .. "\">" .. text .. "</a>"
|
||||
elseif isGemini() then
|
||||
return "=> " .. url .. " " .. text
|
||||
end
|
||||
@@ -73,7 +66,7 @@ M.list = function(items, ordered)
|
||||
local tag = ordered and "ol" or "ul"
|
||||
local out = "<" .. tag .. ">"
|
||||
for _, v in ipairs(items) do
|
||||
out = out .. "<li>" .. esc(v) .. "</li>"
|
||||
out = out .. "<li>" .. v .. "</li>"
|
||||
end
|
||||
out = out .. "</" .. tag .. ">"
|
||||
return out
|
||||
@@ -93,7 +86,7 @@ end
|
||||
M.blockquote = function(s)
|
||||
s = s or ""
|
||||
if isHttp() then
|
||||
return "<blockquote>" .. esc(s) .. "</blockquote>"
|
||||
return "<blockquote>" .. s .. "</blockquote>"
|
||||
elseif isGemini() then
|
||||
return "> " .. s
|
||||
end
|
||||
@@ -111,7 +104,7 @@ M.image = function(alt, src)
|
||||
alt = alt or ""
|
||||
src = src or ""
|
||||
if isHttp() then
|
||||
return "<img src=\"" .. esc(src) .. "\" alt=\"" .. esc(alt) .. "\" />"
|
||||
return "<img src=\"" .. src .. "\" alt=\"" .. alt .. "\" />"
|
||||
elseif isGemini() then
|
||||
return "=> " .. src .. " " .. alt
|
||||
end
|
||||
@@ -121,7 +114,7 @@ M.file = function(text, url)
|
||||
text = text or ""
|
||||
url = url or ""
|
||||
if isHttp() then
|
||||
return "<a href=\"" .. esc(url) .. "\" download>" .. esc(text) .. "</a>"
|
||||
return "<a href=\"" .. url .. "\" download>" .. text .. "</a>"
|
||||
elseif isGemini() then
|
||||
return "=> " .. url .. " " .. text
|
||||
end
|
||||
@@ -130,53 +123,25 @@ end
|
||||
M.note = function(text)
|
||||
text = text or ""
|
||||
if isHttp() then
|
||||
return "<div class=\"note\">" .. esc(text) .. "</div>"
|
||||
return "<div class=\"note\">" .. text .. "</div>"
|
||||
elseif isGemini() then
|
||||
local width = 31
|
||||
return "\n\n" .. text .. "\n\n"
|
||||
end
|
||||
end
|
||||
|
||||
local function wrap_line(line)
|
||||
local out = {}
|
||||
local i = 1
|
||||
local len = #line
|
||||
M.banner = function (text)
|
||||
text = text or ""
|
||||
|
||||
if len == 0 then
|
||||
out[#out + 1] = ""
|
||||
return out
|
||||
end
|
||||
if M.__fes_banner_set then
|
||||
error("Page already contains header")
|
||||
return ""
|
||||
end
|
||||
|
||||
while i <= len do
|
||||
out[#out + 1] = line:sub(i, i + width - 1)
|
||||
i = i + width
|
||||
end
|
||||
|
||||
return out
|
||||
end
|
||||
|
||||
local lines = {}
|
||||
|
||||
for line in (text .. "\n"):gmatch("(.-)\n") do
|
||||
local wrapped = wrap_line(line)
|
||||
for _, w in ipairs(wrapped) do
|
||||
lines[#lines + 1] = w
|
||||
end
|
||||
end
|
||||
|
||||
local border = "+" .. string.rep("=", width + 2) .. "+"
|
||||
local empty = "|" .. string.rep(" ", width + 2) .. "|"
|
||||
|
||||
local out = {}
|
||||
out[#out + 1] = border
|
||||
out[#out + 1] = empty
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
local padding = width - #line
|
||||
out[#out + 1] = "| " .. line .. string.rep(" ", padding) .. " |"
|
||||
end
|
||||
|
||||
out[#out + 1] = empty
|
||||
out[#out + 1] = border
|
||||
|
||||
return table.concat(out, "\n")
|
||||
M.__fes_banner_set = true
|
||||
if isHttp() then
|
||||
return "<div class=\"banner\">" .. text .. "</div>"
|
||||
elseif isGemini() then
|
||||
return text .. "\n"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user