diff options
Diffstat (limited to 'prose.lua')
-rw-r--r-- | prose.lua | 39 |
1 files changed, 29 insertions, 10 deletions
@@ -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") |