235 lines
6.1 KiB
Lua
235 lines
6.1 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
|
|
-- Still not used
|
|
local id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
|
|
local ref = lpeg.P"#" * id
|
|
|
|
local grammars = {
|
|
-- Block 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
|
|
|
|
-- Prints located messages
|
|
function lit.puts(key, ...)
|
|
|
|
-- Returns debug level as number
|
|
local function debuglevel(str)
|
|
if str == "ERROR" then
|
|
lit.status = false
|
|
return 2
|
|
elseif str == "WARNING" then
|
|
return 1
|
|
else
|
|
return 0
|
|
end
|
|
end
|
|
|
|
-- Gets located message
|
|
local function getmsg(key, ...)
|
|
local msg = pandoc.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
|
|
-- Inserts evaluations in inline code
|
|
function lit.insert(code)
|
|
return code
|
|
end
|
|
|
|
-- Examinates (parses and evaluates) code blocks
|
|
function lit.exam(blocks)
|
|
|
|
-- Evaluates code block
|
|
-- Evals Lisp code
|
|
--[[
|
|
local function 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
|
|
]]--
|
|
|
|
-- Parses code block
|
|
local function parse(codeblock, islast)
|
|
|
|
-- Checks code block
|
|
local function check(parsed)
|
|
|
|
-- Checks for extra and unwanted metadata
|
|
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
|
|
|
|
-- Checks for valid metadata
|
|
local function checkmeta(meta, kind)
|
|
|
|
-- Checks for valid metadata type
|
|
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
|
|
|
|
-- Checks for valid metadata pattern
|
|
local function checkpattern(table, meta, key)
|
|
local val = tostring(meta[key])
|
|
local err = key
|
|
for _, pattern in pairs(table) do
|
|
if val:match("^" .. pattern .. "$") then
|
|
err = ""
|
|
break
|
|
end
|
|
end
|
|
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
|
|
checkpattern(val, meta, key)
|
|
elseif meta[key] then
|
|
checktype(val, meta, key)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Parses metadata
|
|
local function parsemeta(yaml)
|
|
local isok, res = pcall(pandoc.read, yaml)
|
|
local meta = {}
|
|
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
|
meta = pandoc.metatotable(res.meta)
|
|
checkmeta(meta, "mandatory")
|
|
checkmeta(meta, "optional")
|
|
checkextra(meta)
|
|
-- TODO: checks for duplicates
|
|
elseif isok and pandoc.utils.stringify(res.meta):isempty() then
|
|
lit.puts("meta_empty")
|
|
else
|
|
lit.puts("meta_invalid")
|
|
end
|
|
return meta
|
|
end
|
|
|
|
lit.puts("checking", table.concat(parsed, ""):indent())
|
|
local meta = parsemeta(parsed[1])
|
|
-- TODO:
|
|
-- meta["code"] = checkcode(parsed[2], meta)
|
|
-- return meta
|
|
return meta
|
|
end
|
|
|
|
local parsed = lpeg.match(lit.g("block"), codeblock.text)
|
|
local checked = (parsed ~= nil and check(parsed) or parsed)
|
|
-- TODO:
|
|
-- evaluated = eval(checked)
|
|
if islast and not(lit.status) then
|
|
lit.puts("aborted")
|
|
os.exit(1)
|
|
end
|
|
-- TODO:
|
|
-- return evaluated
|
|
return codeblock
|
|
end
|
|
|
|
local curr, last = 0, 0
|
|
blocks:walk { CodeBlock = function(_) last = last + 1 end }
|
|
return blocks:walk {
|
|
CodeBlock = function(codeblock)
|
|
curr = curr + 1
|
|
return parse(codeblock, curr == last)
|
|
end
|
|
}
|
|
end
|
|
|
|
return lit
|