From f735ef5306fbf2ccdd3e1856a947d38305db6182 Mon Sep 17 00:00:00 2001 From: bie Date: Fri, 2 Jun 2017 08:38:39 +0000 Subject: er dette the lupin of my dreams --- Makefile | 3 + blueprints/artikkel.lua | 19 ++++++ lupin | 25 ++++++++ lupin.lua | 157 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+) create mode 100644 Makefile create mode 100644 blueprints/artikkel.lua create mode 100755 lupin create mode 100755 lupin.lua 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 + diff --git a/lupin b/lupin new file mode 100755 index 0000000..78d85da --- /dev/null +++ b/lupin @@ -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) +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 + -- cgit v1.0