posix = require 'posix'
require 'corz'
local function printf(...) print(string.format(...)) end
local function setfenv(fn, env)
local i = 1
while true do
local name = debug.getupvalue(fn, i)
if name == "_ENV" then
debug.upvaluejoin(fn, i, (function()
return env
end), 1)
break
elseif not name then
break
end
i = i + 1
end
return fn
end
local html = {}
function html.label(label)
printf("", label)
end
function html.orz(k, v)
printf("", k, v)
printf("", k, v)
end
function html.date(k, v, s)
printf("", k, s or 10, v or '')
end
function html.text(k, v, s)
printf("", k, s or 18, v or '')
end
function html.hidden(k, v)
printf("", k, v)
end
function html.textarea(k, v, r, c)
c = c or 72
r = r or 16
printf("", k, c, r, v or '')
end
function html.print(s)
printf("%s", s)
end
function html.select(k, options, selected)
local p = ""
printf("")
end
function html.submit(value)
printf("", value)
end
function html.p(...)
printf("
%s", ... or '')
end
function html.h1(...)
printf("
%s
", ... or '')
end
help = { html = html, os = os, posix = posix, pairs = pairs }
lupin = {}
lupin.blueprints = {}
lupin.blueprints.default = {
purestrain = function(body, data)
print(data.title)
if data.type ~= "default" then
printf("# type: %s", data.type)
end
data.title = nil
data.type = nil
print(body)
for key, value in pairs(data) do
print(("# %s: %s"):format(key, value))
end
end,
form = function(body, data)
help.form.text('overskrift', data.title)
end,
html = function(body, data)
if data.mode == "title" then
printf("
%s
", data.title)
return
elseif data.mode == "full" then
printf("
%s
", data.title)
end
printf(corz.marxup(body, io.stdout))
if not next(data) then return end
print("
")
for key, value in pairs(data) do
printf("
%s
%s
", key, value)
end
print("
")
end
}
function help.blueprint(id)
return setmetatable({ id = id }, { __index = lupin.blueprints.default })
end
function lupin.loadblueprints(path)
local blueprints = posix.glob(path .. "/*.lua")
if blueprints then
for i, file in pairs(blueprints) do
local chunk = loadfile(file)
setfenv(chunk, help)
local blueprint = chunk()
lupin.blueprints[blueprint.id] = blueprint
end
end
end
function lupin.transform(format, env, mode)
local raw, data = "", {}
if env then
data.type = os.getenv("POST_type") or "default"
data.title = os.getenv("POST_title") or "default"
raw = os.getenv("POST_text") or ""
raw = raw:match("(.-)%s*$")
if lupin.blueprints[data.type] and lupin.blueprints[data.type].env then
lupin.blueprints[data.type].env(data)
end
else
for line in io.lines() do
if line:match("^#") then
local key, value = line:match("^#%s*(.-):%s*(.+)")
if key and value then data[key] = value end
else
if not data.title then
data.title = line
else
raw = raw .. line .. "\n"
end
end
end
data.type = data.type or "default"
end
data.mode = mode
if data.mode == "short" then
raw = raw:match("[^\r\n]+")
end
lupin.blueprints[data.type][format](raw, data)
end