From 16e9f4ef0ab0ad51a7a05fd98ee1870c81b20a73 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Wed, 13 Nov 2024 22:03:17 +0000 Subject: alter extension parsing --- prose.lua | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'prose.lua') diff --git a/prose.lua b/prose.lua index 90172ec..83e6f5d 100644 --- a/prose.lua +++ b/prose.lua @@ -24,17 +24,24 @@ local function prose(S, text) function(a,x,b) return a..emplace(T[tagname](x))..b end } end - + local function strip(s) + local q = s:match("^%s*(.-)%s*$") + return q or s + end + local function split(s) + local r = {} + for u in s:gmatch("%w+") do table.insert(r,u) end + return r + end local function extension(str) - local verb, rest = str:match("^{(%w+);%s*(.*)}$") - if verb then - return emplace(extensions[verb](S, rest)) - end - local verbonly = str:match("^{(%w+)}$") - if verbonly then - return emplace(extensions[verbonly](S)) - end - error("invalid extension syntax: "..str) + local verb, rest = assert(str:match("^{%s*([^%s;]+)(.*)}$")) + local args, body = rest:match("(.-);(.*)") + if not args then args = rest; body = '' end + verb = strip(verb) + args = split(args) + body = strip(body) + -- return verb, args, body + return emplace(extensions[verb](S, body, table.unpack(args))) end local subs = { @@ -100,6 +107,18 @@ extensions.a = function(S,text) return T.a({href=url},body) end +extensions.def = function(S,text,name) + if extensions[name] then + io.stderr:write("warning: redefining extension "..name.."\n") + end + local fn = assert(load( + string.format("return %s", text), + "def "..name, "t", + setmetatable({S=S, T=T, html=html, prose=prose}, {__index=_G}) + )) + extensions[name]=fn() +end + setmetatable(extensions, {__index = function(t, k) return function(S, text) io.stderr:write("warning: unknown extension "..k.."\n") -- cgit v1.2.3