273 lines
6.6 KiB
Lua
273 lines
6.6 KiB
Lua
---------------------------------- LITERATE ----------------------------------
|
|
|
|
-- Variable for all literate stuff
|
|
local lit = {}
|
|
|
|
-- Status indicator
|
|
lit.status = true
|
|
|
|
-- Valid metadata structure
|
|
lit.metastruct = {
|
|
["mandatory"] = {
|
|
-- Value as table == whole patterns to match
|
|
["id"] = {"%a[_%w]*"},
|
|
},
|
|
["optional"] = {
|
|
["lang"] = {"lua", "fennel", "python", "js", "ruby", "lisp", "graphviz"},
|
|
["cmd"] = {".+"},
|
|
-- Value as string == type to match
|
|
["args"] = "table",
|
|
["shift"] = "boolean",
|
|
["wipe"] = "boolean",
|
|
["typed"] = "boolean",
|
|
["link"] = "path",
|
|
["img"] = "path",
|
|
["alt"] = "string",
|
|
["dump"] = "path",
|
|
["quote"] = "boolean",
|
|
},
|
|
}
|
|
|
|
-- Grammars
|
|
lit.g = {}
|
|
|
|
-- Lexical elements
|
|
lit.newline = lpeg.P"\r"^-1 * lpeg.P"\n"
|
|
lit.space = lpeg.S" \t"
|
|
lit.anyspace = lpeg.S" \t\r\n"
|
|
lit.spot = (1 - lit.anyspace)
|
|
lit.any = (lit.spot + lit.space)
|
|
lit.yamlheader = lit.space^0 * lpeg.P"---" * lit.space^0 * lit.newline
|
|
lit.yamlfooter = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline^-1
|
|
lit.yamlbody = -lit.yamlfooter * lit.any^0 * lit.newline
|
|
lit.id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
|
|
lit.ref = lpeg.P"#" * lit.id
|
|
|
|
-- Blocks grammar
|
|
lit.g.block = lpeg.P {
|
|
"Block";
|
|
Block = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name");
|
|
YAML = lpeg.C(lit.yamlheader * lit.yamlbody^0 * lit.yamlfooter);
|
|
Code = lpeg.C((lit.any + lit.newline)^0);
|
|
}
|
|
|
|
-- Evals Lisp code
|
|
--[[
|
|
function lit.eval(code)
|
|
local is_passed, out = pcall (
|
|
function () return fennel.eval(code) end,
|
|
function (e) return e end
|
|
)
|
|
local lua = ""
|
|
local out = tostring(out)
|
|
local preview = out:gsub("\n.*", "")
|
|
if is_passed then
|
|
lua = fennel.compileString(code)
|
|
end
|
|
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
|
|
end
|
|
]]--
|
|
|
|
function lit.stringify_quoted(quoted)
|
|
local quote = (quoted.quotetype == SingleQuoted and '"' or "'")
|
|
quoted = pandoc.utils.stringify(quoted.content)
|
|
return pandoc.Str(quote .. quoted .. quote)
|
|
end
|
|
|
|
function lit.stringify_rawinline(rawinline)
|
|
local str = rawinline.text
|
|
str = str:gsub("\\n", "\n"):gsub("\\t", "\t")
|
|
return pandoc.Str(str)
|
|
end
|
|
|
|
function lit.stringify(content)
|
|
if pandoc.utils.type(content) == "Inlines" then
|
|
return pandoc.utils.stringify(content:walk {
|
|
Quoted = function(e) return lit.stringify_quoted(e) end,
|
|
RawInline = function(e) return lit.stringify_rawinline(e) end,
|
|
Inline = function(inline) return pandoc.utils.stringify(inline) end,
|
|
})
|
|
else
|
|
return pandoc.utils.stringify(content)
|
|
end
|
|
end
|
|
|
|
function lit.string2other(str)
|
|
if str == "true" or str == "false" then
|
|
return str == "true"
|
|
-- WARN "1" always num; cfr. https://pandoc.org/lua-filters.html#type-metavalue
|
|
elseif tonumber(str) then
|
|
return tonumber(str)
|
|
else
|
|
return str
|
|
end
|
|
end
|
|
|
|
function lit.meta2table(meta)
|
|
for k, v in pairs(meta) do
|
|
if pandoc.utils.type(v) == "table" then
|
|
meta[k] = lit.meta2table(v)
|
|
else
|
|
meta[k] = lit.string2other(lit.stringify(v))
|
|
end
|
|
end
|
|
return meta
|
|
end
|
|
|
|
function lit.debuglevel(str)
|
|
if str == "ERROR" then
|
|
lit.status = false
|
|
return 2
|
|
elseif str == "WARNING" then
|
|
return 1
|
|
else
|
|
return 0
|
|
end
|
|
end
|
|
|
|
function lit.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
|
|
|
|
function lit.getmsg(key, ...)
|
|
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 = lit.setmsg(msgs[key], ...)
|
|
levelname, level = mtype, lit.debuglevel(mtype)
|
|
break
|
|
end
|
|
end
|
|
return key, levelname, level
|
|
end
|
|
|
|
function lit.puts(key, ...)
|
|
local verbosity = lit.debuglevel(PANDOC_STATE.verbosity)
|
|
local msg, levelname, level = lit.getmsg(key, ...)
|
|
if level >= verbosity then
|
|
print("[" .. levelname .. "] [LIT] " .. msg)
|
|
end
|
|
end
|
|
|
|
function lit.getmetaval(meta, key)
|
|
local mtype = pandoc.utils.type(meta[key])
|
|
local mval = meta[key]
|
|
if mtype == "Inlines" then
|
|
mval = pandoc.utils.stringify(mval)
|
|
mtype = pandoc.utils.type(mval)
|
|
end
|
|
return mval, mtype
|
|
end
|
|
|
|
function lit.checkextra(meta)
|
|
for key, val in pairs(meta) do
|
|
local missing1 = lit.metastruct["mandatory"][key] == nil
|
|
local missing2 = lit.metastruct["optional"][key] == nil
|
|
local mval = lit.getmetaval(meta, key)
|
|
if missing1 and missing2 then
|
|
lit.puts("invalid_key", key)
|
|
end
|
|
end
|
|
end
|
|
|
|
function lit.checkmeta_type(type, meta, key)
|
|
local mval, mtype = lit.getmetaval(meta, key)
|
|
if type ~= mtype then
|
|
if type == "path" then
|
|
if not(pandoc.path.directory(mval):isdir()) then
|
|
lit.puts("invalid_path", mval, key)
|
|
end
|
|
else
|
|
lit.puts("invalid_type", mtype, key)
|
|
end
|
|
end
|
|
end
|
|
|
|
function lit.checkmeta_match(table, mval, key)
|
|
for _, pattern in pairs(table) do
|
|
if mval:match("^" .. pattern .. "$") then
|
|
return ""
|
|
end
|
|
end
|
|
return key
|
|
end
|
|
|
|
function lit.checkmeta_table(table, meta, key)
|
|
local mval = lit.getmetaval(meta, key)
|
|
local err = lit.checkmeta_match(table, mval, key)
|
|
if not(err:isempty()) then
|
|
lit.puts("invalid_value", mval, err)
|
|
end
|
|
end
|
|
|
|
function lit.checkmeta(meta, kind)
|
|
for key, val in pairs(lit.metastruct[kind]) do
|
|
if meta[key] == nil and kind == "mandatory" then
|
|
lit.puts("no_key", key)
|
|
elseif meta[key] and type(val) == "table" then
|
|
lit.checkmeta_table(val, meta, key)
|
|
elseif meta[key] then
|
|
lit.checkmeta_type(val, meta, key)
|
|
end
|
|
end
|
|
end
|
|
|
|
function lit.setmeta(meta)
|
|
lit.checkmeta(meta, "mandatory")
|
|
lit.checkmeta(meta, "optional")
|
|
lit.checkextra(meta)
|
|
-- TODO checks for 2) duplicates
|
|
-- Set defaults if lit.status = true
|
|
return meta
|
|
end
|
|
|
|
-- TODO
|
|
function lit.assert(e, status)
|
|
if status == false then
|
|
lit.puts("aborted")
|
|
os.exit(1)
|
|
end
|
|
return e
|
|
end
|
|
|
|
function lit.parseyaml(rawyaml)
|
|
-- TODO io.try
|
|
local yaml = io.popen("pandoc -t json <<<\"" .. rawyaml .. "\" 2>&1")
|
|
if yaml:read("*l"):sub(1, 1) == "{" then
|
|
yaml = pandoc.read(rawyaml).meta
|
|
if pandoc.utils.stringify(yaml):isempty() then
|
|
lit.puts("yaml_empty")
|
|
else
|
|
return lit.setmeta(yaml)
|
|
end
|
|
else
|
|
lit.puts("yaml_invalid")
|
|
return nil
|
|
end
|
|
end
|
|
|
|
function lit.parseblock(parsed)
|
|
lit.puts("parsing", table.concat(parsed, ""):indent())
|
|
local yaml = lit.parseyaml(parsed[1])
|
|
-- TODO: parsecode
|
|
return "TODO"
|
|
end
|
|
|
|
function lit.getblock(codeblock)
|
|
local parsed = lpeg.match(lit.g.block, codeblock.text)
|
|
parsed = (parsed ~= nil and lit.parseblock(parsed) or parsed)
|
|
return codeblock, lit.status
|
|
end
|
|
|
|
function lit.getinsert(code)
|
|
return code, lit.status
|
|
end
|
|
|
|
return lit
|