diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | blueprints/artikkel.lua | 19 | ||||
-rwxr-xr-x | lupin | 25 | ||||
-rwxr-xr-x | lupin.lua | 157 |
4 files changed, 204 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fe350fc --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +install: + install -c lupin /usr/bin/ + install -c lupin.lua /usr/share/lua/5.3 diff --git a/blueprints/artikkel.lua b/blueprints/artikkel.lua new file mode 100644 index 0000000..926f53c --- /dev/null +++ b/blueprints/artikkel.lua @@ -0,0 +1,19 @@ +local artikkel = blueprint("default") + +function artikkel.env(data) + data.bilde = os.getenv("POST_bilde") + data.publisert = os.getenv("POST_publisert") +end + +function artikkel.form(body, data) + html.p() + form.label("Overskrift:") + form.text('overskrift', data.overskrift, 32) + form.orz('bilde', data.bilde or '') + html.p() + form.textarea('tekst', body) + form.submit('Lagre') +end + +return artikkel + @@ -0,0 +1,25 @@ +#!/usr/bin/env lua + +require 'lupin' + +local env = false +local format = "purestrain" +local blueprints = "lupin" + +local mode = "full" +for i, opt in ipairs(arg) do + if opt == "-e" then env = true + elseif opt == "-f" then format = "form" + elseif opt == "-h" then format = "html" + elseif opt == "-l" then format = "purestrain" + elseif opt == "-s" then mode = "short" + elseif opt == "-t" then mode = "title" + + else blueprints = opt + end +end + +lupin.loadblueprints(blueprints) + +lupin.transform(format, env, mode) + 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 + |