Autoformat
This commit is contained in:
parent
5b9d776dfb
commit
ee1d86dc86
136
src/literate.lua
136
src/literate.lua
|
@ -11,48 +11,59 @@ lit.fns = {}
|
|||
|
||||
-- Returns specific grammar
|
||||
function lit.g(name)
|
||||
|
||||
-- Stores all grammars
|
||||
local grammars = {}
|
||||
|
||||
-- Shortcuts
|
||||
local P, S, R, Cf, Cc, Ct, V, Cs, Cg, Cb, B, C, Cmt =
|
||||
lpeg.P, lpeg.S, lpeg.R, lpeg.Cf, lpeg.Cc, lpeg.Ct, lpeg.V,
|
||||
lpeg.Cs, lpeg.Cg, lpeg.Cb, lpeg.B, lpeg.C, lpeg.Cmt
|
||||
lpeg.P,
|
||||
lpeg.S,
|
||||
lpeg.R,
|
||||
lpeg.Cf,
|
||||
lpeg.Cc,
|
||||
lpeg.Ct,
|
||||
lpeg.V,
|
||||
lpeg.Cs,
|
||||
lpeg.Cg,
|
||||
lpeg.Cb,
|
||||
lpeg.B,
|
||||
lpeg.C,
|
||||
lpeg.Cmt
|
||||
|
||||
-- Lexical elements
|
||||
local newline = P"\r"^-1 * P"\n"
|
||||
local space = S" \t"
|
||||
local anyspace = S" \t\r\n"
|
||||
local newline = P("\r") ^ -1 * P("\n")
|
||||
local space = S(" \t")
|
||||
local anyspace = S(" \t\r\n")
|
||||
local spot = (1 - anyspace)
|
||||
local any = (spot + space)
|
||||
local yamlheader = space^0 * P"---" * space^0 * newline
|
||||
local yamlfooter = space^0 * P"..." * space^0 * newline^-1
|
||||
local yamlbody = -yamlfooter * any^0 * newline
|
||||
local label = R("az", "AZ") * R("az", "AZ", "09", "__")^0
|
||||
local ref = -B"\\" * P"#" * C(label)
|
||||
local notref = (1 - ref)^0
|
||||
local yamlheader = space ^ 0 * P("---") * space ^ 0 * newline
|
||||
local yamlfooter = space ^ 0 * P("...") * space ^ 0 * newline ^ -1
|
||||
local yamlbody = -yamlfooter * any ^ 0 * newline
|
||||
local label = R("az", "AZ") * R("az", "AZ", "09", "__") ^ 0
|
||||
local ref = -B("\\") * P("#") * C(label)
|
||||
local notref = (1 - ref) ^ 0
|
||||
|
||||
-- Function declaration grammar
|
||||
grammars["declaration"] = P {
|
||||
"Declaration";
|
||||
Declaration = Ct(V"YAML" * V"Code", "name");
|
||||
YAML = C(yamlheader * yamlbody^0 * yamlfooter);
|
||||
Code = C((any + newline)^0);
|
||||
}
|
||||
grammars["declaration"] = P({
|
||||
"Declaration",
|
||||
Declaration = Ct(V("YAML") * V("Code"), "name"),
|
||||
YAML = C(yamlheader * yamlbody ^ 0 * yamlfooter),
|
||||
Code = C((any + newline) ^ 0),
|
||||
})
|
||||
|
||||
-- Argument references grammar
|
||||
grammars["references"] = notref * P {
|
||||
"References";
|
||||
References = Ct((ref * notref)^0);
|
||||
} * -1
|
||||
grammars["references"] = notref
|
||||
* P({
|
||||
"References",
|
||||
References = Ct((ref * notref) ^ 0),
|
||||
})
|
||||
* -1
|
||||
|
||||
return grammars[name]
|
||||
end
|
||||
|
||||
-- Prints located messages
|
||||
function lit.puts(yaml_key, ...)
|
||||
|
||||
-- Returns debug level as number
|
||||
local function debuglevel(str)
|
||||
if str == "ERROR" then
|
||||
|
@ -72,7 +83,7 @@ function lit.puts(yaml_key, ...)
|
|||
for mtype, msgs in pairs(msg) do
|
||||
if msgs[key] then
|
||||
key = (msgs[key][lang] ~= nil and msgs[key][lang] or msgs[key]["en"])
|
||||
for i, str in ipairs({...}) do
|
||||
for i, str in ipairs({ ... }) do
|
||||
key = key:gsub("#" .. i, str)
|
||||
end
|
||||
levelname, level = mtype, debuglevel(mtype)
|
||||
|
@ -84,20 +95,15 @@ function lit.puts(yaml_key, ...)
|
|||
|
||||
local verbosity = debuglevel(PANDOC_STATE.verbosity)
|
||||
local msg, levelname, level = getmsg(yaml_key, ...)
|
||||
if level >= verbosity then
|
||||
print("[" .. levelname .. "] [LIT] " .. msg)
|
||||
end
|
||||
if level >= verbosity then print("[" .. levelname .. "] [LIT] " .. msg) end
|
||||
end
|
||||
|
||||
-- Parses and evaluates literate functions in document
|
||||
function lit.exam(doc)
|
||||
|
||||
-- Extracts function declarations
|
||||
local function extract(codeblock)
|
||||
|
||||
-- Checks function declarations
|
||||
local function check(match)
|
||||
|
||||
-- Stores valid declaration
|
||||
local declaration = {}
|
||||
|
||||
|
@ -105,13 +111,19 @@ function lit.exam(doc)
|
|||
local metastruct = {
|
||||
["mandatory"] = {
|
||||
-- Value as table == whole patterns to match
|
||||
["id"] = {"%a[_%w]*"},
|
||||
["id"] = { "%a[_%w]*" },
|
||||
},
|
||||
["optional"] = {
|
||||
["lang"] = {
|
||||
"lua", "fennel", "python", "js", "ruby", "lisp", "graphviz",
|
||||
"lua",
|
||||
"fennel",
|
||||
"python",
|
||||
"js",
|
||||
"ruby",
|
||||
"lisp",
|
||||
"graphviz",
|
||||
},
|
||||
["cmd"] = {".+"},
|
||||
["cmd"] = { ".+" },
|
||||
-- Value as string == type to match
|
||||
["args"] = "table",
|
||||
["shift"] = "boolean",
|
||||
|
@ -130,13 +142,13 @@ function lit.exam(doc)
|
|||
-- First table indicates one or more internal or external evaluations
|
||||
-- Second table indicates one or more commands to try
|
||||
local langcmds = {
|
||||
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||
["python"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||
["js"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||
["ruby"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||
["lisp"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||
["lua"] = { ["int_eval"] = { "CMD1", "CMD2" } },
|
||||
["fennel"] = { ["int_eval"] = { "CMD1", "CMD2" } },
|
||||
["python"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
|
||||
["js"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
|
||||
["ruby"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
|
||||
["lisp"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
|
||||
["graphviz"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
|
||||
}
|
||||
|
||||
-- Prints error
|
||||
|
@ -154,17 +166,17 @@ function lit.exam(doc)
|
|||
-- If lang and cmd are present, ignores lang and use cmd as eval
|
||||
if declaration["lang"] and declaration["cmd"] then
|
||||
lit.puts("ignore_lang", declaration["cmd"], declaration["lang"])
|
||||
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||
declaration["eval"] = { ["ext_eval"] = { declaration["cmd"] } }
|
||||
-- If neither lang or cmd are presents, defaults lang and eval to lua
|
||||
elseif not(declaration["lang"]) and not(declaration["cmd"])then
|
||||
elseif not declaration["lang"] and not declaration["cmd"] then
|
||||
declaration["lang"] = "lua"
|
||||
declaration["eval"] = langcmds["lua"]
|
||||
-- If only lang present, eval is lang
|
||||
elseif declaration["lang"] and not(declaration["cmd"]) then
|
||||
elseif declaration["lang"] and not declaration["cmd"] then
|
||||
declaration["eval"] = langcmds[declaration["lang"]]
|
||||
-- If only cmd present, eval is cmd
|
||||
elseif not(declaration["lang"]) and declaration["cmd"] then
|
||||
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||
elseif not declaration["lang"] and declaration["cmd"] then
|
||||
declaration["eval"] = { ["ext_eval"] = { declaration["cmd"] } }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -174,9 +186,7 @@ function lit.exam(doc)
|
|||
local cmd = declaration["cmd"]:split()[1]
|
||||
if os.isunix() then
|
||||
local status = io.try("type", cmd)
|
||||
if not(status) then
|
||||
putserr("invalid_cmd", cmd)
|
||||
end
|
||||
if not status then putserr("invalid_cmd", cmd) end
|
||||
else
|
||||
lit.puts("skip_check", "checkcmd")
|
||||
end
|
||||
|
@ -188,22 +198,19 @@ function lit.exam(doc)
|
|||
for key, _ in pairs(declaration) do
|
||||
local missing1 = metastruct["mandatory"][key] == nil
|
||||
local missing2 = metastruct["optional"][key] == nil
|
||||
if missing1 and missing2 then
|
||||
putserr("invalid_key", key)
|
||||
end
|
||||
if missing1 and missing2 then putserr("invalid_key", key) end
|
||||
end
|
||||
end
|
||||
|
||||
-- Checks for valid metadata
|
||||
local function checkmeta(kind)
|
||||
|
||||
-- Checks for valid metadata type
|
||||
local function checktype(type1, key)
|
||||
local val = declaration[key]
|
||||
local type2 = type(val)
|
||||
if type1 ~= type2 then
|
||||
if type1 == "path" then
|
||||
if not(pandoc.path.directory(val):isdir()) then
|
||||
if not (pandoc.path.directory(val):isdir()) then
|
||||
putserr("invalid_path", val, key)
|
||||
end
|
||||
else
|
||||
|
@ -222,9 +229,7 @@ function lit.exam(doc)
|
|||
break
|
||||
end
|
||||
end
|
||||
if not(err:isempty()) then
|
||||
putserr("invalid_value", val, err)
|
||||
end
|
||||
if not (err:isempty()) then putserr("invalid_value", val, err) end
|
||||
end
|
||||
|
||||
for key, val in pairs(metastruct[kind]) do
|
||||
|
@ -241,7 +246,7 @@ function lit.exam(doc)
|
|||
-- Parses metadata
|
||||
local function parsemeta(yaml)
|
||||
local isok, res = pcall(pandoc.read, yaml)
|
||||
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
||||
if isok and not (pandoc.utils.stringify(res.meta):isempty()) then
|
||||
declaration = pandoc.metatotable(res.meta)
|
||||
checkmeta("mandatory")
|
||||
checkmeta("optional")
|
||||
|
@ -287,7 +292,6 @@ function lit.exam(doc)
|
|||
|
||||
-- Calls literate functions
|
||||
local function call(el)
|
||||
|
||||
-- TODO
|
||||
-- Calls literate functions in code inlines
|
||||
local function codecall(code)
|
||||
|
@ -296,9 +300,9 @@ function lit.exam(doc)
|
|||
|
||||
local function metacall(meta)
|
||||
local function inlinescall(inlines)
|
||||
return inlines:walk {
|
||||
Code = function(code) return codecall(code) end
|
||||
}
|
||||
return inlines:walk({
|
||||
Code = function(code) return codecall(code) end,
|
||||
})
|
||||
end
|
||||
|
||||
for key, val in pairs(meta) do
|
||||
|
@ -324,14 +328,14 @@ function lit.exam(doc)
|
|||
|
||||
-- Asserts literate status
|
||||
local function assert()
|
||||
if not(lit.status) then
|
||||
if not lit.status then
|
||||
lit.puts("aborted")
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
doc = doc:walk { CodeBlock = function(block) return extract(block) end }
|
||||
doc = doc:walk { Meta = function(meta) return call(meta) end }
|
||||
doc = doc:walk { Code = function(code) return call(code) end }
|
||||
return doc:walk { Pandoc = function(_) assert() end }
|
||||
doc = doc:walk({ CodeBlock = function(block) return extract(block) end })
|
||||
doc = doc:walk({ Meta = function(meta) return call(meta) end })
|
||||
doc = doc:walk({ Code = function(code) return call(code) end })
|
||||
return doc:walk({ Pandoc = function(_) assert() end })
|
||||
end
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
local nat = {}
|
||||
|
||||
function nat.get(str)
|
||||
return str
|
||||
end
|
||||
function nat.get(str) return str end
|
||||
|
||||
return nat
|
||||
|
|
Loading…
Reference in New Issue