diff --git a/src/literate.lua b/src/literate.lua index 6223537..ee8e084 100644 --- a/src/literate.lua +++ b/src/literate.lua @@ -40,6 +40,7 @@ function lit.g(name) local yamlheader = space^0 * lpeg.P"---" * space^0 * newline local yamlfooter = space^0 * lpeg.P"..." * space^0 * newline^-1 local yamlbody = -yamlfooter * any^0 * newline + -- Still not used local id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0 local ref = lpeg.P"#" * id @@ -56,6 +57,82 @@ function lit.g(name) return grammars[name] end +function lit.metatotable(meta) + + local function stringify(content) + if pandoc.utils.type(content) == "Inlines" then + return pandoc.utils.stringify(content:walk { + Quoted = function(quoted) + local q = (quoted.quotetype == SingleQuoted and '"' or "'") + return pandoc.Str(q .. pandoc.utils.stringify(quoted.content) .. q) + end, + RawInline = function(rawinline) + return pandoc.Str(rawinline.text:gsub("\\n", "\n"):gsub("\\t", "\t")) + end, + Inline = function(inline) return pandoc.utils.stringify(inline) end, + }) + end + return pandoc.utils.stringify(content) + end + + for k, v in pairs(meta) do + if pandoc.utils.type(v) == "table" then + meta[k] = lit.metatotable(v) + else + meta[k] = stringify(v) + if meta[k] == "true" or meta[k] == "false" then + meta[k] = meta[k] == "true" + -- Note that "1" always num because Pandoc doesn't make distinctions + -- cfr. https://pandoc.org/lua-filters.html#type-metavalue + elseif tonumber(meta[k]) then + meta[k] = tonumber(meta[k]) + end + end + end + + return meta +end + +function lit.puts(key, ...) + + local function debuglevel(str) + if str == "ERROR" then + lit.status = false + return 2 + elseif str == "WARNING" then + return 1 + else + return 0 + end + end + + local function getmsg(key, ...) + local msg = lit.metatotable(pandoc.read([[#locale()]]).meta) + local levelname, level, lang = "INFO", 0, os.lang() + for mtype, msgs in pairs(msg) do + if msgs[key] ~= nil then + key = (msgs[key][lang] ~= nil and msgs[key][lang] or msgs[key]["en"]) + for i, str in ipairs({...}) do key = key:gsub("#" .. i, str) end + levelname, level = mtype, debuglevel(mtype) + break + end + end + return key, levelname, level + end + + local verbosity = debuglevel(PANDOC_STATE.verbosity) + local msg, levelname, level = getmsg(key, ...) + if level >= verbosity then + print("[" .. levelname .. "] [LIT] " .. msg) + end + +end + +-- TODO +function lit.insert(code) + return code +end + -- Evals Lisp code --[[ function lit.eval(code) @@ -73,102 +150,6 @@ function lit.eval(code) end ]]-- -function lit.meta2table(meta) - - local function stringify(content) - - local function stringify_quoted(quoted) - local quote = (quoted.quotetype == SingleQuoted and '"' or "'") - quoted = pandoc.utils.stringify(quoted.content) - return pandoc.Str(quote .. quoted .. quote) - end - - local function stringify_rawinline(rawinline) - local str = rawinline.text - str = str:gsub("\\n", "\n"):gsub("\\t", "\t") - return pandoc.Str(str) - end - - if pandoc.utils.type(content) == "Inlines" then - return pandoc.utils.stringify(content:walk { - Quoted = function(e) return stringify_quoted(e) end, - RawInline = function(e) return stringify_rawinline(e) end, - Inline = function(inline) return pandoc.utils.stringify(inline) end, - }) - else - return pandoc.utils.stringify(content) - end - end - - local function string2other(str) - if str == "true" or str == "false" then - return str == "true" - -- Note that "1" always num; - -- cfr. https://pandoc.org/lua-filters.html#type-metavalue - elseif tonumber(str) then - return tonumber(str) - else - return str - end - end - - for k, v in pairs(meta) do - if pandoc.utils.type(v) == "table" then - meta[k] = lit.meta2table(v) - else - meta[k] = string2other(stringify(v)) - end - end - return meta -end - -function lit.puts(key, ...) - - local function debuglevel(str) - if str == "ERROR" then - lit.status = false - return 2 - elseif str == "WARNING" then - return 1 - else - return 0 - end - end - - local function getmsg(key, ...) - - local function setmsg(msg, ...) - local lang = os.lang() - msg = (msg[lang] ~= nil and msg[lang] or msg) - for i, str in ipairs({...}) do - msg = msg:gsub("#" .. i, str) - end - return msg - end - - local msg = lit.meta2table(pandoc.read([[#locale()]]).meta) - local levelname, level = "INFO", 0 - for mtype, msgs in pairs(msg) do - if msgs[key] ~= nil then - key = setmsg(msgs[key], ...) - levelname, level = mtype, debuglevel(mtype) - break - end - end - return key, levelname, level - end - - local verbosity = debuglevel(PANDOC_STATE.verbosity) - local msg, levelname, level = getmsg(key, ...) - if level >= verbosity then - print("[" .. levelname .. "] [LIT] " .. msg) - end -end - -function lit.insert(code) - return code -end - function lit.parse(blocks) local function parse(codeblock, islast) @@ -226,10 +207,11 @@ function lit.parse(blocks) checktype(val, meta, key) end end + end local function setmeta(meta) - local tmeta = lit.meta2table(meta) + local tmeta = lit.metatotable(meta) checkmeta(tmeta, "mandatory") checkmeta(tmeta, "optional") checkextra(tmeta) @@ -283,6 +265,7 @@ function lit.parse(blocks) return parse(codeblock, curr == lastblockindex()) end } + end return lit