summaryrefslogtreecommitdiff
path: root/lupin.lua
diff options
context:
space:
mode:
authorbie <bie@blekksprut.net>2017-06-02 08:38:39 +0000
committerbie <bie@blekksprut.net>2017-06-02 08:38:39 +0000
commitf735ef5306fbf2ccdd3e1856a947d38305db6182 (patch)
tree7cd52048317a7a7c097d19bbc54b370afa657555 /lupin.lua
downloadlupin-f735ef5306fbf2ccdd3e1856a947d38305db6182.tar.xz
er dette the lupin of my dreams
Diffstat (limited to 'lupin.lua')
-rwxr-xr-xlupin.lua157
1 files changed, 157 insertions, 0 deletions
diff --git a/lupin.lua b/lupin.lua
new file mode 100755
index 0000000..c2ba662
--- /dev/null
+++ b/lupin.lua
@@ -0,0 +1,157 @@
+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>%s</label>", label)
+end
+function html.orz(k, v)
+ printf("<img class='orz' data-name=%s src='%s' data-uri='/orz/'>", k, v)
+ printf("<input name='%s' value='%s' type='hidden'>", k, v)
+end
+function html.date(k, v, s)
+ printf("<input type=date name=%s size=%s value='%s'>", k, s or 10, v or '')
+end
+function html.text(k, v, s)
+ printf("<input type=text name=%s size=%s value='%s'>", k, s or 18, v or '')
+end
+function html.hidden(k, v)
+ printf("<input type=hidden name=%s value='%s'>", k, v)
+end
+function html.textarea(k, v, r, c)
+ c = c or 72
+ r = r or 16
+ printf("<textarea name=%s cols=%s rows=%s>%s</textarea>", k, c, r, v or '')
+end
+function html.print(s)
+ printf("%s", s)
+end
+function html.select(k, options, selected)
+ local p = ""
+ printf("<select name='%s'>", k)
+ for k,v in pairs(options) do
+ if selected and selected == tostring(v) then
+ p = "selected "
+ end
+ printf("<option %svalue='%s'>%s</option>", p, v, v)
+ p = ""
+ end
+ print("</select>")
+end
+function html.submit(value)
+ printf("<input type=submit value='%s'>", value)
+end
+function html.p(...)
+ printf("<p>%s", ... or '')
+end
+function html.h1(...)
+ printf("<h1>%s</h1>", ... 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("<h2>%s</h2>", data.title)
+ return
+ elseif data.mode == "full" then
+ printf("<h1>%s</h1>", data.title)
+ end
+ printf(corz.marxup(body, io.stdout))
+ if not next(data) then return end
+ print("<dl>")
+ for key, value in pairs(data) do
+ printf("<dt>%s</dt><dd>%s</dd>", key, value)
+ end
+ print("</dl>")
+ 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
+