computable-pandoc/src/literate.lua

277 lines
6.8 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
function lit.g(name)
-- Lexical elements
local newline = lpeg.P"\r"^-1 * lpeg.P"\n"
local space = lpeg.S" \t"
local anyspace = lpeg.S" \t\r\n"
local spot = (1 - anyspace)
local any = (spot + space)
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
local id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
local ref = lpeg.P"#" * id
local grammars = {
-- Blocks grammar
["block"] = lpeg.P {
"Block";
Block = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name");
YAML = lpeg.C(yamlheader * yamlbody^0 * yamlfooter);
Code = lpeg.C((any + newline)^0);
},
}
return grammars[name]
end
-- 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.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
-- TODO
function lit.assert(e, status)
if status == false then
lit.puts("aborted")
os.exit(1)
end
return e
end
function lit.getblock(codeblock)
local function checkextra(meta)
for key, val in pairs(meta) do
local missing1 = lit.metastruct["mandatory"][key] == nil
local missing2 = lit.metastruct["optional"][key] == nil
if missing1 and missing2 then
lit.puts("invalid_key", key)
end
end
end
local function checkmeta(meta, kind)
local function checktype(type1, meta, key)
local val = meta[key]
local type2 = type(val)
if type1 ~= type2 then
if type1 == "path" then
if not(pandoc.path.directory(val):isdir()) then
lit.puts("invalid_path", val, key)
end
else
lit.puts("invalid_type", type2, key)
end
end
end
local function checktable(table, meta, key)
local function checkmatch(table, val, key)
for _, pattern in pairs(table) do
if val:match("^" .. pattern .. "$") then
return ""
end
end
return key
end
local val = meta[key]
local err = checkmatch(table, val, key)
if not(err:isempty()) then
lit.puts("invalid_value", val, err)
end
end
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
checktable(val, meta, key)
elseif meta[key] then
checktype(val, meta, key)
end
end
end
local function setmeta(meta)
local tmeta = lit.meta2table(meta)
checkmeta(tmeta, "mandatory")
checkmeta(tmeta, "optional")
checkextra(tmeta)
-- TODO checks for 2) duplicates
-- Set defaults if lit.status = true
return meta
end
local function 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 setmeta(yaml)
end
else
lit.puts("yaml_invalid")
return nil
end
end
local function inspect(parsed)
lit.puts("parsing", table.concat(parsed, ""):indent())
local yaml = parseyaml(parsed[1])
-- TODO: parsecode
return "TODO"
end
local parsed = lpeg.match(lit.g("block"), codeblock.text)
parsed = (parsed ~= nil and inspect(parsed) or parsed)
return codeblock, lit.status
end
function lit.getinsert(code)
return code, lit.status
end
return lit