Fixed workflow with checks; added (incomplete) call function
This commit is contained in:
parent
efa6cd3b69
commit
5b9d776dfb
|
@ -6602,18 +6602,22 @@ dog.import()
|
||||||
|
|
||||||
---------------------------------- LITERATE ----------------------------------
|
---------------------------------- LITERATE ----------------------------------
|
||||||
|
|
||||||
-- Variable for all literate stuff
|
-- Stores all literate stuff
|
||||||
local lit = {}
|
local lit = {}
|
||||||
|
|
||||||
-- Status indicator
|
-- Indicates status
|
||||||
lit.status = true
|
lit.status = true
|
||||||
|
|
||||||
-- Grammars
|
-- Stores literate functions
|
||||||
|
lit.fns = {}
|
||||||
|
|
||||||
|
-- Returns specific grammar
|
||||||
function lit.g(name)
|
function lit.g(name)
|
||||||
|
|
||||||
-- Variable for grammars collection
|
-- Stores all grammars
|
||||||
local grammars = {}
|
local grammars = {}
|
||||||
|
|
||||||
|
-- Shortcuts
|
||||||
local P, S, R, Cf, Cc, Ct, V, Cs, Cg, Cb, B, C, Cmt =
|
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.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.Cs, lpeg.Cg, lpeg.Cb, lpeg.B, lpeg.C, lpeg.Cmt
|
||||||
|
@ -6627,11 +6631,11 @@ function lit.g(name)
|
||||||
local yamlheader = space^0 * P"---" * space^0 * newline
|
local yamlheader = space^0 * P"---" * space^0 * newline
|
||||||
local yamlfooter = space^0 * P"..." * space^0 * newline^-1
|
local yamlfooter = space^0 * P"..." * space^0 * newline^-1
|
||||||
local yamlbody = -yamlfooter * any^0 * newline
|
local yamlbody = -yamlfooter * any^0 * newline
|
||||||
local id = R("az", "AZ") * R("az", "AZ", "09")^0
|
local label = R("az", "AZ") * R("az", "AZ", "09", "__")^0
|
||||||
local ref = -B"\\" * P"#" * C(id)
|
local ref = -B"\\" * P"#" * C(label)
|
||||||
local notref = (1 - ref)^0
|
local notref = (1 - ref)^0
|
||||||
|
|
||||||
-- Declaration grammar
|
-- Function declaration grammar
|
||||||
grammars["declaration"] = P {
|
grammars["declaration"] = P {
|
||||||
"Declaration";
|
"Declaration";
|
||||||
Declaration = Ct(V"YAML" * V"Code", "name");
|
Declaration = Ct(V"YAML" * V"Code", "name");
|
||||||
|
@ -6639,7 +6643,7 @@ function lit.g(name)
|
||||||
Code = C((any + newline)^0);
|
Code = C((any + newline)^0);
|
||||||
}
|
}
|
||||||
|
|
||||||
-- References grammar
|
-- Argument references grammar
|
||||||
grammars["references"] = notref * P {
|
grammars["references"] = notref * P {
|
||||||
"References";
|
"References";
|
||||||
References = Ct((ref * notref)^0);
|
References = Ct((ref * notref)^0);
|
||||||
|
@ -6709,7 +6713,7 @@ ERROR:
|
||||||
]]).meta)
|
]]).meta)
|
||||||
local levelname, level, lang = "INFO", 0, os.lang()
|
local levelname, level, lang = "INFO", 0, os.lang()
|
||||||
for mtype, msgs in pairs(msg) do
|
for mtype, msgs in pairs(msg) do
|
||||||
if msgs[key] ~= nil then
|
if msgs[key] then
|
||||||
key = (msgs[key][lang] ~= nil and msgs[key][lang] or msgs[key]["en"])
|
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)
|
key = key:gsub("#" .. i, str)
|
||||||
|
@ -6728,35 +6732,17 @@ ERROR:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Examinates (parses and evaluates) document
|
-- Parses and evaluates literate functions in document
|
||||||
function lit.exam(doc)
|
function lit.exam(doc)
|
||||||
|
|
||||||
-- Evaluates code declaration
|
-- Extracts function declarations
|
||||||
-- Evals Lisp code
|
local function extract(codeblock)
|
||||||
--[[
|
|
||||||
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 declaration
|
-- Checks function declarations
|
||||||
local function parse(codeblock)
|
local function check(match)
|
||||||
|
|
||||||
-- Checks code declarations
|
-- Stores valid declaration
|
||||||
local function check(parsed)
|
local declaration = {}
|
||||||
|
|
||||||
-- Indicates if checks passes
|
|
||||||
local ispass = true
|
|
||||||
|
|
||||||
-- Valid metadata structure
|
-- Valid metadata structure
|
||||||
local metastruct = {
|
local metastruct = {
|
||||||
|
@ -6783,6 +6769,9 @@ function lit.exam(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Language commands
|
-- Language commands
|
||||||
|
-- Eachs command is a table of table;
|
||||||
|
-- First table indicates one or more internal or external evaluations
|
||||||
|
-- Second table indicates one or more commands to try
|
||||||
local langcmds = {
|
local langcmds = {
|
||||||
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||||
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||||
|
@ -6793,36 +6782,39 @@ function lit.exam(doc)
|
||||||
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Prints error and does not pass the check
|
-- Prints error
|
||||||
|
-- Since error messages debuglevel are "ERROR", lit.status turns false
|
||||||
|
-- Flush the declaration to avoid storage in lit.fns
|
||||||
local function putserr(...)
|
local function putserr(...)
|
||||||
lit.puts(...)
|
lit.puts(...)
|
||||||
ispass = false
|
declaration = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Adds 'eval' key in meta
|
-- Adds 'eval' key in declaration
|
||||||
-- This keys indicates what to eval
|
-- This key indicates what to eval
|
||||||
local function addeval(meta)
|
local function addeval()
|
||||||
meta["eval"] = {}
|
declaration["eval"] = {}
|
||||||
if ispass then
|
-- If lang and cmd are present, ignores lang and use cmd as eval
|
||||||
if meta["lang"] and meta["cmd"] then
|
if declaration["lang"] and declaration["cmd"] then
|
||||||
lit.puts("ignore_lang", meta["cmd"], meta["lang"])
|
lit.puts("ignore_lang", declaration["cmd"], declaration["lang"])
|
||||||
meta["eval"] = {["ext_eval"] = { meta["cmd"] } }
|
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||||
elseif not(meta["lang"]) and not(meta["cmd"])then
|
-- If neither lang or cmd are presents, defaults lang and eval to lua
|
||||||
meta["lang"] = "lua"
|
elseif not(declaration["lang"]) and not(declaration["cmd"])then
|
||||||
meta["eval"] = langcmds["lua"]
|
declaration["lang"] = "lua"
|
||||||
elseif meta["lang"] and not(meta["cmd"]) then
|
declaration["eval"] = langcmds["lua"]
|
||||||
meta["eval"] = langcmds[meta["lang"]]
|
-- If only lang present, eval is lang
|
||||||
elseif not(meta["lang"]) and meta["cmd"] then
|
elseif declaration["lang"] and not(declaration["cmd"]) then
|
||||||
meta["eval"] = {["ext_eval"] = { meta["cmd"] } }
|
declaration["eval"] = langcmds[declaration["lang"]]
|
||||||
end
|
-- If only cmd present, eval is cmd
|
||||||
|
elseif not(declaration["lang"]) and declaration["cmd"] then
|
||||||
|
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||||
end
|
end
|
||||||
return meta
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid command
|
-- Checks for valid command for Unix systems
|
||||||
local function checkcmd(meta)
|
local function checkcmd()
|
||||||
if meta["cmd"] then
|
if declaration["cmd"] then
|
||||||
local cmd = meta["cmd"]:split()[1]
|
local cmd = declaration["cmd"]:split()[1]
|
||||||
if os.isunix() then
|
if os.isunix() then
|
||||||
local status = io.try("type", cmd)
|
local status = io.try("type", cmd)
|
||||||
if not(status) then
|
if not(status) then
|
||||||
|
@ -6834,9 +6826,9 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for extra and unwanted metadata
|
-- Checks for extra, unwanted and wrong metadata
|
||||||
local function checkextra(meta)
|
local function checkextra()
|
||||||
for key, _ in pairs(meta) do
|
for key, _ in pairs(declaration) do
|
||||||
local missing1 = metastruct["mandatory"][key] == nil
|
local missing1 = metastruct["mandatory"][key] == nil
|
||||||
local missing2 = metastruct["optional"][key] == nil
|
local missing2 = metastruct["optional"][key] == nil
|
||||||
if missing1 and missing2 then
|
if missing1 and missing2 then
|
||||||
|
@ -6846,11 +6838,11 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid metadata
|
-- Checks for valid metadata
|
||||||
local function checkmeta(metadata, kind)
|
local function checkmeta(kind)
|
||||||
|
|
||||||
-- Checks for valid metadata type
|
-- Checks for valid metadata type
|
||||||
local function checktype(type1, meta, key)
|
local function checktype(type1, key)
|
||||||
local val = meta[key]
|
local val = declaration[key]
|
||||||
local type2 = type(val)
|
local type2 = type(val)
|
||||||
if type1 ~= type2 then
|
if type1 ~= type2 then
|
||||||
if type1 == "path" then
|
if type1 == "path" then
|
||||||
|
@ -6864,8 +6856,8 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid metadata pattern
|
-- Checks for valid metadata pattern
|
||||||
local function checkpattern(table, meta, key)
|
local function checkpattern(table, key)
|
||||||
local val = tostring(meta[key])
|
local val = tostring(declaration[key])
|
||||||
local err = key
|
local err = key
|
||||||
for _, pattern in pairs(table) do
|
for _, pattern in pairs(table) do
|
||||||
if val:match("^" .. pattern .. "$") then
|
if val:match("^" .. pattern .. "$") then
|
||||||
|
@ -6879,12 +6871,12 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
for key, val in pairs(metastruct[kind]) do
|
for key, val in pairs(metastruct[kind]) do
|
||||||
if metadata[key] == nil and kind == "mandatory" then
|
if declaration[key] == nil and kind == "mandatory" then
|
||||||
putserr("no_key", key)
|
putserr("no_key", key)
|
||||||
elseif metadata[key] and type(val) == "table" then
|
elseif declaration[key] and type(val) == "table" then
|
||||||
checkpattern(val, metadata, key)
|
checkpattern(val, key)
|
||||||
elseif metadata[key] then
|
elseif declaration[key] then
|
||||||
checktype(val, metadata, key)
|
checktype(val, key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6892,55 +6884,85 @@ function lit.exam(doc)
|
||||||
-- Parses metadata
|
-- Parses metadata
|
||||||
local function parsemeta(yaml)
|
local function parsemeta(yaml)
|
||||||
local isok, res = pcall(pandoc.read, yaml)
|
local isok, res = pcall(pandoc.read, yaml)
|
||||||
local meta = {}
|
|
||||||
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
||||||
meta = pandoc.metatotable(res.meta)
|
declaration = pandoc.metatotable(res.meta)
|
||||||
checkmeta(meta, "mandatory")
|
checkmeta("mandatory")
|
||||||
checkmeta(meta, "optional")
|
checkmeta("optional")
|
||||||
checkextra(meta)
|
checkextra()
|
||||||
checkcmd(meta)
|
checkcmd()
|
||||||
|
if next(declaration) then addeval() end
|
||||||
elseif isok and pandoc.utils.stringify(res.meta):isempty() then
|
elseif isok and pandoc.utils.stringify(res.meta):isempty() then
|
||||||
putserr("meta_empty")
|
putserr("meta_empty")
|
||||||
else
|
else
|
||||||
putserr("meta_invalid")
|
putserr("meta_invalid")
|
||||||
end
|
end
|
||||||
return addeval(meta)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- TODO
|
||||||
-- NOTE: if not valid code, return meta["eval"] = {}
|
-- Parses code
|
||||||
local function checkcode(code, meta)
|
local function parsecode(rawcode)
|
||||||
local references = lpeg.match(lit.g("references"), code)
|
local code = ""
|
||||||
|
local references = lpeg.match(lit.g("references"), rawcode)
|
||||||
if references then
|
if references then
|
||||||
for _, ref in ipairs(references) do
|
for _, ref in ipairs(references) do
|
||||||
print("reference:", ref)
|
print("reference:", ref)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return "TODO"
|
declaration["code"] = code
|
||||||
end
|
end
|
||||||
|
|
||||||
lit.puts("checking", table.concat(parsed, ""):indent())
|
if match then
|
||||||
local meta = parsemeta(parsed[1])
|
lit.puts("checking", table.concat(match, ""):indent())
|
||||||
-- TODO
|
parsemeta(match[1])
|
||||||
-- Add each meta to a variable
|
if next(declaration) then parsecode(match[2]) end
|
||||||
-- Remove meta if doesn't pass checkcode
|
end
|
||||||
meta["code"] = checkcode(parsed[2], meta)
|
return declaration
|
||||||
return meta
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local parsed = lpeg.match(lit.g("declaration"), codeblock.text)
|
local parsed = check(lpeg.match(lit.g("declaration"), codeblock.text))
|
||||||
local checked = (parsed ~= nil and check(parsed) or parsed)
|
if next(parsed) then
|
||||||
-- TODO:
|
-- TODO:
|
||||||
-- evaluated = eval(checked)
|
-- Eval parsed lit.fns and modify code block accordingly
|
||||||
-- TODO: write evaluated and do overrides with warns
|
lit.fns[parsed["id"]] = parsed
|
||||||
-- return evaluated
|
end
|
||||||
return codeblock
|
return codeblock
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- Calls literate functions
|
||||||
-- Evals and inserts inline code
|
local function call(el)
|
||||||
local function insert(code)
|
|
||||||
return code
|
-- TODO
|
||||||
|
-- Calls literate functions in code inlines
|
||||||
|
local function codecall(code)
|
||||||
|
return pandoc.Str("EVAL: " .. pandoc.utils.stringify(code))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function metacall(meta)
|
||||||
|
local function inlinescall(inlines)
|
||||||
|
return inlines:walk {
|
||||||
|
Code = function(code) return codecall(code) end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
for key, val in pairs(meta) do
|
||||||
|
if pandoc.utils.type(val) == "Inlines" then
|
||||||
|
meta[key] = inlinescall(val)
|
||||||
|
elseif pandoc.utils.type(val) == "List" then
|
||||||
|
for i, item in ipairs(val) do
|
||||||
|
if pandoc.utils.type(item) == "Inlines" then
|
||||||
|
meta[key][i] = inlinescall(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return meta
|
||||||
|
end
|
||||||
|
|
||||||
|
if pandoc.utils.type(el) == "Meta" then
|
||||||
|
return metacall(el)
|
||||||
|
else
|
||||||
|
return codecall(el)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Asserts literate status
|
-- Asserts literate status
|
||||||
|
@ -6951,9 +6973,10 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return doc:walk { CodeBlock = function(block) return parse(block) end }
|
doc = doc:walk { CodeBlock = function(block) return extract(block) end }
|
||||||
:walk { Code = function(inline) return insert(inline) end }
|
doc = doc:walk { Meta = function(meta) return call(meta) end }
|
||||||
:walk { Pandoc = function(_) assert() end }
|
doc = doc:walk { Code = function(code) return call(code) end }
|
||||||
|
return doc:walk { Pandoc = function(_) assert() end }
|
||||||
end
|
end
|
||||||
|
|
||||||
------------------------------------ PANDOC -----------------------------------
|
------------------------------------ PANDOC -----------------------------------
|
||||||
|
|
|
@ -14,18 +14,22 @@ dog.import()
|
||||||
|
|
||||||
---------------------------------- LITERATE ----------------------------------
|
---------------------------------- LITERATE ----------------------------------
|
||||||
|
|
||||||
-- Variable for all literate stuff
|
-- Stores all literate stuff
|
||||||
local lit = {}
|
local lit = {}
|
||||||
|
|
||||||
-- Status indicator
|
-- Indicates status
|
||||||
lit.status = true
|
lit.status = true
|
||||||
|
|
||||||
-- Grammars
|
-- Stores literate functions
|
||||||
|
lit.fns = {}
|
||||||
|
|
||||||
|
-- Returns specific grammar
|
||||||
function lit.g(name)
|
function lit.g(name)
|
||||||
|
|
||||||
-- Variable for grammars collection
|
-- Stores all grammars
|
||||||
local grammars = {}
|
local grammars = {}
|
||||||
|
|
||||||
|
-- Shortcuts
|
||||||
local P, S, R, Cf, Cc, Ct, V, Cs, Cg, Cb, B, C, Cmt =
|
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.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.Cs, lpeg.Cg, lpeg.Cb, lpeg.B, lpeg.C, lpeg.Cmt
|
||||||
|
@ -39,11 +43,11 @@ function lit.g(name)
|
||||||
local yamlheader = space^0 * P"---" * space^0 * newline
|
local yamlheader = space^0 * P"---" * space^0 * newline
|
||||||
local yamlfooter = space^0 * P"..." * space^0 * newline^-1
|
local yamlfooter = space^0 * P"..." * space^0 * newline^-1
|
||||||
local yamlbody = -yamlfooter * any^0 * newline
|
local yamlbody = -yamlfooter * any^0 * newline
|
||||||
local id = R("az", "AZ") * R("az", "AZ", "09")^0
|
local label = R("az", "AZ") * R("az", "AZ", "09", "__")^0
|
||||||
local ref = -B"\\" * P"#" * C(id)
|
local ref = -B"\\" * P"#" * C(label)
|
||||||
local notref = (1 - ref)^0
|
local notref = (1 - ref)^0
|
||||||
|
|
||||||
-- Declaration grammar
|
-- Function declaration grammar
|
||||||
grammars["declaration"] = P {
|
grammars["declaration"] = P {
|
||||||
"Declaration";
|
"Declaration";
|
||||||
Declaration = Ct(V"YAML" * V"Code", "name");
|
Declaration = Ct(V"YAML" * V"Code", "name");
|
||||||
|
@ -51,7 +55,7 @@ function lit.g(name)
|
||||||
Code = C((any + newline)^0);
|
Code = C((any + newline)^0);
|
||||||
}
|
}
|
||||||
|
|
||||||
-- References grammar
|
-- Argument references grammar
|
||||||
grammars["references"] = notref * P {
|
grammars["references"] = notref * P {
|
||||||
"References";
|
"References";
|
||||||
References = Ct((ref * notref)^0);
|
References = Ct((ref * notref)^0);
|
||||||
|
@ -121,7 +125,7 @@ ERROR:
|
||||||
]]).meta)
|
]]).meta)
|
||||||
local levelname, level, lang = "INFO", 0, os.lang()
|
local levelname, level, lang = "INFO", 0, os.lang()
|
||||||
for mtype, msgs in pairs(msg) do
|
for mtype, msgs in pairs(msg) do
|
||||||
if msgs[key] ~= nil then
|
if msgs[key] then
|
||||||
key = (msgs[key][lang] ~= nil and msgs[key][lang] or msgs[key]["en"])
|
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)
|
key = key:gsub("#" .. i, str)
|
||||||
|
@ -140,35 +144,17 @@ ERROR:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Examinates (parses and evaluates) document
|
-- Parses and evaluates literate functions in document
|
||||||
function lit.exam(doc)
|
function lit.exam(doc)
|
||||||
|
|
||||||
-- Evaluates code declaration
|
-- Extracts function declarations
|
||||||
-- Evals Lisp code
|
local function extract(codeblock)
|
||||||
--[[
|
|
||||||
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 declaration
|
-- Checks function declarations
|
||||||
local function parse(codeblock)
|
local function check(match)
|
||||||
|
|
||||||
-- Checks code declarations
|
-- Stores valid declaration
|
||||||
local function check(parsed)
|
local declaration = {}
|
||||||
|
|
||||||
-- Indicates if checks passes
|
|
||||||
local ispass = true
|
|
||||||
|
|
||||||
-- Valid metadata structure
|
-- Valid metadata structure
|
||||||
local metastruct = {
|
local metastruct = {
|
||||||
|
@ -195,6 +181,9 @@ function lit.exam(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Language commands
|
-- Language commands
|
||||||
|
-- Eachs command is a table of table;
|
||||||
|
-- First table indicates one or more internal or external evaluations
|
||||||
|
-- Second table indicates one or more commands to try
|
||||||
local langcmds = {
|
local langcmds = {
|
||||||
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||||
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||||
|
@ -205,36 +194,39 @@ function lit.exam(doc)
|
||||||
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Prints error and does not pass the check
|
-- Prints error
|
||||||
|
-- Since error messages debuglevel are "ERROR", lit.status turns false
|
||||||
|
-- Flush the declaration to avoid storage in lit.fns
|
||||||
local function putserr(...)
|
local function putserr(...)
|
||||||
lit.puts(...)
|
lit.puts(...)
|
||||||
ispass = false
|
declaration = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Adds 'eval' key in meta
|
-- Adds 'eval' key in declaration
|
||||||
-- This keys indicates what to eval
|
-- This key indicates what to eval
|
||||||
local function addeval(meta)
|
local function addeval()
|
||||||
meta["eval"] = {}
|
declaration["eval"] = {}
|
||||||
if ispass then
|
-- If lang and cmd are present, ignores lang and use cmd as eval
|
||||||
if meta["lang"] and meta["cmd"] then
|
if declaration["lang"] and declaration["cmd"] then
|
||||||
lit.puts("ignore_lang", meta["cmd"], meta["lang"])
|
lit.puts("ignore_lang", declaration["cmd"], declaration["lang"])
|
||||||
meta["eval"] = {["ext_eval"] = { meta["cmd"] } }
|
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||||
elseif not(meta["lang"]) and not(meta["cmd"])then
|
-- If neither lang or cmd are presents, defaults lang and eval to lua
|
||||||
meta["lang"] = "lua"
|
elseif not(declaration["lang"]) and not(declaration["cmd"])then
|
||||||
meta["eval"] = langcmds["lua"]
|
declaration["lang"] = "lua"
|
||||||
elseif meta["lang"] and not(meta["cmd"]) then
|
declaration["eval"] = langcmds["lua"]
|
||||||
meta["eval"] = langcmds[meta["lang"]]
|
-- If only lang present, eval is lang
|
||||||
elseif not(meta["lang"]) and meta["cmd"] then
|
elseif declaration["lang"] and not(declaration["cmd"]) then
|
||||||
meta["eval"] = {["ext_eval"] = { meta["cmd"] } }
|
declaration["eval"] = langcmds[declaration["lang"]]
|
||||||
end
|
-- If only cmd present, eval is cmd
|
||||||
|
elseif not(declaration["lang"]) and declaration["cmd"] then
|
||||||
|
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||||
end
|
end
|
||||||
return meta
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid command
|
-- Checks for valid command for Unix systems
|
||||||
local function checkcmd(meta)
|
local function checkcmd()
|
||||||
if meta["cmd"] then
|
if declaration["cmd"] then
|
||||||
local cmd = meta["cmd"]:split()[1]
|
local cmd = declaration["cmd"]:split()[1]
|
||||||
if os.isunix() then
|
if os.isunix() then
|
||||||
local status = io.try("type", cmd)
|
local status = io.try("type", cmd)
|
||||||
if not(status) then
|
if not(status) then
|
||||||
|
@ -246,9 +238,9 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for extra and unwanted metadata
|
-- Checks for extra, unwanted and wrong metadata
|
||||||
local function checkextra(meta)
|
local function checkextra()
|
||||||
for key, _ in pairs(meta) do
|
for key, _ in pairs(declaration) do
|
||||||
local missing1 = metastruct["mandatory"][key] == nil
|
local missing1 = metastruct["mandatory"][key] == nil
|
||||||
local missing2 = metastruct["optional"][key] == nil
|
local missing2 = metastruct["optional"][key] == nil
|
||||||
if missing1 and missing2 then
|
if missing1 and missing2 then
|
||||||
|
@ -258,11 +250,11 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid metadata
|
-- Checks for valid metadata
|
||||||
local function checkmeta(metadata, kind)
|
local function checkmeta(kind)
|
||||||
|
|
||||||
-- Checks for valid metadata type
|
-- Checks for valid metadata type
|
||||||
local function checktype(type1, meta, key)
|
local function checktype(type1, key)
|
||||||
local val = meta[key]
|
local val = declaration[key]
|
||||||
local type2 = type(val)
|
local type2 = type(val)
|
||||||
if type1 ~= type2 then
|
if type1 ~= type2 then
|
||||||
if type1 == "path" then
|
if type1 == "path" then
|
||||||
|
@ -276,8 +268,8 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid metadata pattern
|
-- Checks for valid metadata pattern
|
||||||
local function checkpattern(table, meta, key)
|
local function checkpattern(table, key)
|
||||||
local val = tostring(meta[key])
|
local val = tostring(declaration[key])
|
||||||
local err = key
|
local err = key
|
||||||
for _, pattern in pairs(table) do
|
for _, pattern in pairs(table) do
|
||||||
if val:match("^" .. pattern .. "$") then
|
if val:match("^" .. pattern .. "$") then
|
||||||
|
@ -291,12 +283,12 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
for key, val in pairs(metastruct[kind]) do
|
for key, val in pairs(metastruct[kind]) do
|
||||||
if metadata[key] == nil and kind == "mandatory" then
|
if declaration[key] == nil and kind == "mandatory" then
|
||||||
putserr("no_key", key)
|
putserr("no_key", key)
|
||||||
elseif metadata[key] and type(val) == "table" then
|
elseif declaration[key] and type(val) == "table" then
|
||||||
checkpattern(val, metadata, key)
|
checkpattern(val, key)
|
||||||
elseif metadata[key] then
|
elseif declaration[key] then
|
||||||
checktype(val, metadata, key)
|
checktype(val, key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -304,55 +296,85 @@ function lit.exam(doc)
|
||||||
-- Parses metadata
|
-- Parses metadata
|
||||||
local function parsemeta(yaml)
|
local function parsemeta(yaml)
|
||||||
local isok, res = pcall(pandoc.read, yaml)
|
local isok, res = pcall(pandoc.read, yaml)
|
||||||
local meta = {}
|
|
||||||
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
||||||
meta = pandoc.metatotable(res.meta)
|
declaration = pandoc.metatotable(res.meta)
|
||||||
checkmeta(meta, "mandatory")
|
checkmeta("mandatory")
|
||||||
checkmeta(meta, "optional")
|
checkmeta("optional")
|
||||||
checkextra(meta)
|
checkextra()
|
||||||
checkcmd(meta)
|
checkcmd()
|
||||||
|
if next(declaration) then addeval() end
|
||||||
elseif isok and pandoc.utils.stringify(res.meta):isempty() then
|
elseif isok and pandoc.utils.stringify(res.meta):isempty() then
|
||||||
putserr("meta_empty")
|
putserr("meta_empty")
|
||||||
else
|
else
|
||||||
putserr("meta_invalid")
|
putserr("meta_invalid")
|
||||||
end
|
end
|
||||||
return addeval(meta)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- TODO
|
||||||
-- NOTE: if not valid code, return meta["eval"] = {}
|
-- Parses code
|
||||||
local function checkcode(code, meta)
|
local function parsecode(rawcode)
|
||||||
local references = lpeg.match(lit.g("references"), code)
|
local code = ""
|
||||||
|
local references = lpeg.match(lit.g("references"), rawcode)
|
||||||
if references then
|
if references then
|
||||||
for _, ref in ipairs(references) do
|
for _, ref in ipairs(references) do
|
||||||
print("reference:", ref)
|
print("reference:", ref)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return "TODO"
|
declaration["code"] = code
|
||||||
end
|
end
|
||||||
|
|
||||||
lit.puts("checking", table.concat(parsed, ""):indent())
|
if match then
|
||||||
local meta = parsemeta(parsed[1])
|
lit.puts("checking", table.concat(match, ""):indent())
|
||||||
-- TODO
|
parsemeta(match[1])
|
||||||
-- Add each meta to a variable
|
if next(declaration) then parsecode(match[2]) end
|
||||||
-- Remove meta if doesn't pass checkcode
|
end
|
||||||
meta["code"] = checkcode(parsed[2], meta)
|
return declaration
|
||||||
return meta
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local parsed = lpeg.match(lit.g("declaration"), codeblock.text)
|
local parsed = check(lpeg.match(lit.g("declaration"), codeblock.text))
|
||||||
local checked = (parsed ~= nil and check(parsed) or parsed)
|
if next(parsed) then
|
||||||
-- TODO:
|
-- TODO:
|
||||||
-- evaluated = eval(checked)
|
-- Eval parsed lit.fns and modify code block accordingly
|
||||||
-- TODO: write evaluated and do overrides with warns
|
lit.fns[parsed["id"]] = parsed
|
||||||
-- return evaluated
|
end
|
||||||
return codeblock
|
return codeblock
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- Calls literate functions
|
||||||
-- Evals and inserts inline code
|
local function call(el)
|
||||||
local function insert(code)
|
|
||||||
return code
|
-- TODO
|
||||||
|
-- Calls literate functions in code inlines
|
||||||
|
local function codecall(code)
|
||||||
|
return pandoc.Str("EVAL: " .. pandoc.utils.stringify(code))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function metacall(meta)
|
||||||
|
local function inlinescall(inlines)
|
||||||
|
return inlines:walk {
|
||||||
|
Code = function(code) return codecall(code) end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
for key, val in pairs(meta) do
|
||||||
|
if pandoc.utils.type(val) == "Inlines" then
|
||||||
|
meta[key] = inlinescall(val)
|
||||||
|
elseif pandoc.utils.type(val) == "List" then
|
||||||
|
for i, item in ipairs(val) do
|
||||||
|
if pandoc.utils.type(item) == "Inlines" then
|
||||||
|
meta[key][i] = inlinescall(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return meta
|
||||||
|
end
|
||||||
|
|
||||||
|
if pandoc.utils.type(el) == "Meta" then
|
||||||
|
return metacall(el)
|
||||||
|
else
|
||||||
|
return codecall(el)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Asserts literate status
|
-- Asserts literate status
|
||||||
|
@ -363,9 +385,10 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return doc:walk { CodeBlock = function(block) return parse(block) end }
|
doc = doc:walk { CodeBlock = function(block) return extract(block) end }
|
||||||
:walk { Code = function(inline) return insert(inline) end }
|
doc = doc:walk { Meta = function(meta) return call(meta) end }
|
||||||
:walk { Pandoc = function(_) assert() end }
|
doc = doc:walk { Code = function(code) return call(code) end }
|
||||||
|
return doc:walk { Pandoc = function(_) assert() end }
|
||||||
end
|
end
|
||||||
|
|
||||||
------------------------------------ PANDOC -----------------------------------
|
------------------------------------ PANDOC -----------------------------------
|
||||||
|
|
|
@ -25,7 +25,7 @@ local function make_dist(name, bundle)
|
||||||
local fnl = chomp(optpath .. "fennel.lua"):gsub("\nreturn mod\n", "\nlocal fnl = mod\n")
|
local fnl = chomp(optpath .. "fennel.lua"):gsub("\nreturn mod\n", "\nlocal fnl = mod\n")
|
||||||
local dog = chomp(optpath .. "dog.lua"):gsub("\nreturn dog\n", "")
|
local dog = chomp(optpath .. "dog.lua"):gsub("\nreturn dog\n", "")
|
||||||
local nat = chomp("src/natural.lua"):gsub("\nreturn nat\n", "")
|
local nat = chomp("src/natural.lua"):gsub("\nreturn nat\n", "")
|
||||||
local lit = chomp("src/literate.lua"):gsub("#locale%(%)", ("src/locale.yaml"):readtext())
|
local lit = chomp("src/literate.lua"):gsub("`locale%(%)`", ("src/locale.yaml"):readtext())
|
||||||
local pan = chomp("src/pandoc.lua")
|
local pan = chomp("src/pandoc.lua")
|
||||||
local license = string.strip([[
|
local license = string.strip([[
|
||||||
Computable Pandoc:
|
Computable Pandoc:
|
||||||
|
|
227
src/literate.lua
227
src/literate.lua
|
@ -1,17 +1,21 @@
|
||||||
---------------------------------- LITERATE ----------------------------------
|
---------------------------------- LITERATE ----------------------------------
|
||||||
|
|
||||||
-- Variable for all literate stuff
|
-- Stores all literate stuff
|
||||||
local lit = {}
|
local lit = {}
|
||||||
|
|
||||||
-- Status indicator
|
-- Indicates status
|
||||||
lit.status = true
|
lit.status = true
|
||||||
|
|
||||||
-- Grammars
|
-- Stores literate functions
|
||||||
|
lit.fns = {}
|
||||||
|
|
||||||
|
-- Returns specific grammar
|
||||||
function lit.g(name)
|
function lit.g(name)
|
||||||
|
|
||||||
-- Variable for grammars collection
|
-- Stores all grammars
|
||||||
local grammars = {}
|
local grammars = {}
|
||||||
|
|
||||||
|
-- Shortcuts
|
||||||
local P, S, R, Cf, Cc, Ct, V, Cs, Cg, Cb, B, C, Cmt =
|
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.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.Cs, lpeg.Cg, lpeg.Cb, lpeg.B, lpeg.C, lpeg.Cmt
|
||||||
|
@ -25,11 +29,11 @@ function lit.g(name)
|
||||||
local yamlheader = space^0 * P"---" * space^0 * newline
|
local yamlheader = space^0 * P"---" * space^0 * newline
|
||||||
local yamlfooter = space^0 * P"..." * space^0 * newline^-1
|
local yamlfooter = space^0 * P"..." * space^0 * newline^-1
|
||||||
local yamlbody = -yamlfooter * any^0 * newline
|
local yamlbody = -yamlfooter * any^0 * newline
|
||||||
local id = R("az", "AZ") * R("az", "AZ", "09")^0
|
local label = R("az", "AZ") * R("az", "AZ", "09", "__")^0
|
||||||
local ref = -B"\\" * P"#" * C(id)
|
local ref = -B"\\" * P"#" * C(label)
|
||||||
local notref = (1 - ref)^0
|
local notref = (1 - ref)^0
|
||||||
|
|
||||||
-- Declaration grammar
|
-- Function declaration grammar
|
||||||
grammars["declaration"] = P {
|
grammars["declaration"] = P {
|
||||||
"Declaration";
|
"Declaration";
|
||||||
Declaration = Ct(V"YAML" * V"Code", "name");
|
Declaration = Ct(V"YAML" * V"Code", "name");
|
||||||
|
@ -37,7 +41,7 @@ function lit.g(name)
|
||||||
Code = C((any + newline)^0);
|
Code = C((any + newline)^0);
|
||||||
}
|
}
|
||||||
|
|
||||||
-- References grammar
|
-- Argument references grammar
|
||||||
grammars["references"] = notref * P {
|
grammars["references"] = notref * P {
|
||||||
"References";
|
"References";
|
||||||
References = Ct((ref * notref)^0);
|
References = Ct((ref * notref)^0);
|
||||||
|
@ -63,10 +67,10 @@ function lit.puts(yaml_key, ...)
|
||||||
|
|
||||||
-- Gets located message
|
-- Gets located message
|
||||||
local function getmsg(key, ...)
|
local function getmsg(key, ...)
|
||||||
local msg = pandoc.metatotable(pandoc.read([[#locale()]]).meta)
|
local msg = pandoc.metatotable(pandoc.read([[`locale()`]]).meta)
|
||||||
local levelname, level, lang = "INFO", 0, os.lang()
|
local levelname, level, lang = "INFO", 0, os.lang()
|
||||||
for mtype, msgs in pairs(msg) do
|
for mtype, msgs in pairs(msg) do
|
||||||
if msgs[key] ~= nil then
|
if msgs[key] then
|
||||||
key = (msgs[key][lang] ~= nil and msgs[key][lang] or msgs[key]["en"])
|
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)
|
key = key:gsub("#" .. i, str)
|
||||||
|
@ -85,35 +89,17 @@ function lit.puts(yaml_key, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Examinates (parses and evaluates) document
|
-- Parses and evaluates literate functions in document
|
||||||
function lit.exam(doc)
|
function lit.exam(doc)
|
||||||
|
|
||||||
-- Evaluates code declaration
|
-- Extracts function declarations
|
||||||
-- Evals Lisp code
|
local function extract(codeblock)
|
||||||
--[[
|
|
||||||
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 declaration
|
-- Checks function declarations
|
||||||
local function parse(codeblock)
|
local function check(match)
|
||||||
|
|
||||||
-- Checks code declarations
|
-- Stores valid declaration
|
||||||
local function check(parsed)
|
local declaration = {}
|
||||||
|
|
||||||
-- Indicates if checks passes
|
|
||||||
local ispass = true
|
|
||||||
|
|
||||||
-- Valid metadata structure
|
-- Valid metadata structure
|
||||||
local metastruct = {
|
local metastruct = {
|
||||||
|
@ -140,6 +126,9 @@ function lit.exam(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Language commands
|
-- Language commands
|
||||||
|
-- Eachs command is a table of table;
|
||||||
|
-- First table indicates one or more internal or external evaluations
|
||||||
|
-- Second table indicates one or more commands to try
|
||||||
local langcmds = {
|
local langcmds = {
|
||||||
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||||
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } },
|
||||||
|
@ -150,36 +139,39 @@ function lit.exam(doc)
|
||||||
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } },
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Prints error and does not pass the check
|
-- Prints error
|
||||||
|
-- Since error messages debuglevel are "ERROR", lit.status turns false
|
||||||
|
-- Flush the declaration to avoid storage in lit.fns
|
||||||
local function putserr(...)
|
local function putserr(...)
|
||||||
lit.puts(...)
|
lit.puts(...)
|
||||||
ispass = false
|
declaration = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Adds 'eval' key in meta
|
-- Adds 'eval' key in declaration
|
||||||
-- This keys indicates what to eval
|
-- This key indicates what to eval
|
||||||
local function addeval(meta)
|
local function addeval()
|
||||||
meta["eval"] = {}
|
declaration["eval"] = {}
|
||||||
if ispass then
|
-- If lang and cmd are present, ignores lang and use cmd as eval
|
||||||
if meta["lang"] and meta["cmd"] then
|
if declaration["lang"] and declaration["cmd"] then
|
||||||
lit.puts("ignore_lang", meta["cmd"], meta["lang"])
|
lit.puts("ignore_lang", declaration["cmd"], declaration["lang"])
|
||||||
meta["eval"] = {["ext_eval"] = { meta["cmd"] } }
|
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||||
elseif not(meta["lang"]) and not(meta["cmd"])then
|
-- If neither lang or cmd are presents, defaults lang and eval to lua
|
||||||
meta["lang"] = "lua"
|
elseif not(declaration["lang"]) and not(declaration["cmd"])then
|
||||||
meta["eval"] = langcmds["lua"]
|
declaration["lang"] = "lua"
|
||||||
elseif meta["lang"] and not(meta["cmd"]) then
|
declaration["eval"] = langcmds["lua"]
|
||||||
meta["eval"] = langcmds[meta["lang"]]
|
-- If only lang present, eval is lang
|
||||||
elseif not(meta["lang"]) and meta["cmd"] then
|
elseif declaration["lang"] and not(declaration["cmd"]) then
|
||||||
meta["eval"] = {["ext_eval"] = { meta["cmd"] } }
|
declaration["eval"] = langcmds[declaration["lang"]]
|
||||||
end
|
-- If only cmd present, eval is cmd
|
||||||
|
elseif not(declaration["lang"]) and declaration["cmd"] then
|
||||||
|
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } }
|
||||||
end
|
end
|
||||||
return meta
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid command
|
-- Checks for valid command for Unix systems
|
||||||
local function checkcmd(meta)
|
local function checkcmd()
|
||||||
if meta["cmd"] then
|
if declaration["cmd"] then
|
||||||
local cmd = meta["cmd"]:split()[1]
|
local cmd = declaration["cmd"]:split()[1]
|
||||||
if os.isunix() then
|
if os.isunix() then
|
||||||
local status = io.try("type", cmd)
|
local status = io.try("type", cmd)
|
||||||
if not(status) then
|
if not(status) then
|
||||||
|
@ -191,9 +183,9 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for extra and unwanted metadata
|
-- Checks for extra, unwanted and wrong metadata
|
||||||
local function checkextra(meta)
|
local function checkextra()
|
||||||
for key, _ in pairs(meta) do
|
for key, _ in pairs(declaration) do
|
||||||
local missing1 = metastruct["mandatory"][key] == nil
|
local missing1 = metastruct["mandatory"][key] == nil
|
||||||
local missing2 = metastruct["optional"][key] == nil
|
local missing2 = metastruct["optional"][key] == nil
|
||||||
if missing1 and missing2 then
|
if missing1 and missing2 then
|
||||||
|
@ -203,11 +195,11 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid metadata
|
-- Checks for valid metadata
|
||||||
local function checkmeta(metadata, kind)
|
local function checkmeta(kind)
|
||||||
|
|
||||||
-- Checks for valid metadata type
|
-- Checks for valid metadata type
|
||||||
local function checktype(type1, meta, key)
|
local function checktype(type1, key)
|
||||||
local val = meta[key]
|
local val = declaration[key]
|
||||||
local type2 = type(val)
|
local type2 = type(val)
|
||||||
if type1 ~= type2 then
|
if type1 ~= type2 then
|
||||||
if type1 == "path" then
|
if type1 == "path" then
|
||||||
|
@ -221,8 +213,8 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Checks for valid metadata pattern
|
-- Checks for valid metadata pattern
|
||||||
local function checkpattern(table, meta, key)
|
local function checkpattern(table, key)
|
||||||
local val = tostring(meta[key])
|
local val = tostring(declaration[key])
|
||||||
local err = key
|
local err = key
|
||||||
for _, pattern in pairs(table) do
|
for _, pattern in pairs(table) do
|
||||||
if val:match("^" .. pattern .. "$") then
|
if val:match("^" .. pattern .. "$") then
|
||||||
|
@ -236,12 +228,12 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
for key, val in pairs(metastruct[kind]) do
|
for key, val in pairs(metastruct[kind]) do
|
||||||
if metadata[key] == nil and kind == "mandatory" then
|
if declaration[key] == nil and kind == "mandatory" then
|
||||||
putserr("no_key", key)
|
putserr("no_key", key)
|
||||||
elseif metadata[key] and type(val) == "table" then
|
elseif declaration[key] and type(val) == "table" then
|
||||||
checkpattern(val, metadata, key)
|
checkpattern(val, key)
|
||||||
elseif metadata[key] then
|
elseif declaration[key] then
|
||||||
checktype(val, metadata, key)
|
checktype(val, key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -249,55 +241,85 @@ function lit.exam(doc)
|
||||||
-- Parses metadata
|
-- Parses metadata
|
||||||
local function parsemeta(yaml)
|
local function parsemeta(yaml)
|
||||||
local isok, res = pcall(pandoc.read, yaml)
|
local isok, res = pcall(pandoc.read, yaml)
|
||||||
local meta = {}
|
|
||||||
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then
|
||||||
meta = pandoc.metatotable(res.meta)
|
declaration = pandoc.metatotable(res.meta)
|
||||||
checkmeta(meta, "mandatory")
|
checkmeta("mandatory")
|
||||||
checkmeta(meta, "optional")
|
checkmeta("optional")
|
||||||
checkextra(meta)
|
checkextra()
|
||||||
checkcmd(meta)
|
checkcmd()
|
||||||
|
if next(declaration) then addeval() end
|
||||||
elseif isok and pandoc.utils.stringify(res.meta):isempty() then
|
elseif isok and pandoc.utils.stringify(res.meta):isempty() then
|
||||||
putserr("meta_empty")
|
putserr("meta_empty")
|
||||||
else
|
else
|
||||||
putserr("meta_invalid")
|
putserr("meta_invalid")
|
||||||
end
|
end
|
||||||
return addeval(meta)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- TODO
|
||||||
-- NOTE: if not valid code, return meta["eval"] = {}
|
-- Parses code
|
||||||
local function checkcode(code, meta)
|
local function parsecode(rawcode)
|
||||||
local references = lpeg.match(lit.g("references"), code)
|
local code = ""
|
||||||
|
local references = lpeg.match(lit.g("references"), rawcode)
|
||||||
if references then
|
if references then
|
||||||
for _, ref in ipairs(references) do
|
for _, ref in ipairs(references) do
|
||||||
print("reference:", ref)
|
print("reference:", ref)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return "TODO"
|
declaration["code"] = code
|
||||||
end
|
end
|
||||||
|
|
||||||
lit.puts("checking", table.concat(parsed, ""):indent())
|
if match then
|
||||||
local meta = parsemeta(parsed[1])
|
lit.puts("checking", table.concat(match, ""):indent())
|
||||||
-- TODO
|
parsemeta(match[1])
|
||||||
-- Add each meta to a variable
|
if next(declaration) then parsecode(match[2]) end
|
||||||
-- Remove meta if doesn't pass checkcode
|
end
|
||||||
meta["code"] = checkcode(parsed[2], meta)
|
return declaration
|
||||||
return meta
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local parsed = lpeg.match(lit.g("declaration"), codeblock.text)
|
local parsed = check(lpeg.match(lit.g("declaration"), codeblock.text))
|
||||||
local checked = (parsed ~= nil and check(parsed) or parsed)
|
if next(parsed) then
|
||||||
-- TODO:
|
-- TODO:
|
||||||
-- evaluated = eval(checked)
|
-- Eval parsed lit.fns and modify code block accordingly
|
||||||
-- TODO: write evaluated and do overrides with warns
|
lit.fns[parsed["id"]] = parsed
|
||||||
-- return evaluated
|
end
|
||||||
return codeblock
|
return codeblock
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- Calls literate functions
|
||||||
-- Evals and inserts inline code
|
local function call(el)
|
||||||
local function insert(code)
|
|
||||||
return code
|
-- TODO
|
||||||
|
-- Calls literate functions in code inlines
|
||||||
|
local function codecall(code)
|
||||||
|
return pandoc.Str("EVAL: " .. pandoc.utils.stringify(code))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function metacall(meta)
|
||||||
|
local function inlinescall(inlines)
|
||||||
|
return inlines:walk {
|
||||||
|
Code = function(code) return codecall(code) end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
for key, val in pairs(meta) do
|
||||||
|
if pandoc.utils.type(val) == "Inlines" then
|
||||||
|
meta[key] = inlinescall(val)
|
||||||
|
elseif pandoc.utils.type(val) == "List" then
|
||||||
|
for i, item in ipairs(val) do
|
||||||
|
if pandoc.utils.type(item) == "Inlines" then
|
||||||
|
meta[key][i] = inlinescall(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return meta
|
||||||
|
end
|
||||||
|
|
||||||
|
if pandoc.utils.type(el) == "Meta" then
|
||||||
|
return metacall(el)
|
||||||
|
else
|
||||||
|
return codecall(el)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Asserts literate status
|
-- Asserts literate status
|
||||||
|
@ -308,7 +330,8 @@ function lit.exam(doc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return doc:walk { CodeBlock = function(block) return parse(block) end }
|
doc = doc:walk { CodeBlock = function(block) return extract(block) end }
|
||||||
:walk { Code = function(inline) return insert(inline) end }
|
doc = doc:walk { Meta = function(meta) return call(meta) end }
|
||||||
:walk { Pandoc = function(_) assert() end }
|
doc = doc:walk { Code = function(code) return call(code) end }
|
||||||
|
return doc:walk { Pandoc = function(_) assert() end }
|
||||||
end
|
end
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -109,7 +109,7 @@ Infinite loop:
|
||||||
args:
|
args:
|
||||||
x: 1
|
x: 1
|
||||||
...
|
...
|
||||||
#fn1(2) * #x
|
`fn1(2)` * #x
|
||||||
|
|
||||||
Invalid code:
|
Invalid code:
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
|
---
|
||||||
|
bool: true
|
||||||
|
int: 1
|
||||||
|
float: 1.1
|
||||||
|
string: foo all
|
||||||
|
list:
|
||||||
|
- true
|
||||||
|
- 1
|
||||||
|
- 1.1
|
||||||
|
- foo all
|
||||||
|
- "`fn1()`"
|
||||||
|
- Another test `fn2()`
|
||||||
|
fn1_test: "`fn1()`"
|
||||||
|
fn2_test: Another test `fn2()`
|
||||||
|
...
|
||||||
|
|
||||||
# Calls Before Declarations
|
# Calls Before Declarations
|
||||||
|
|
||||||
- `fn1()``fn2()`
|
- `fn1()``fn2()`
|
||||||
|
@ -88,7 +104,7 @@ With link and alt:
|
||||||
id: fn8
|
id: fn8
|
||||||
lang: graphviz
|
lang: graphviz
|
||||||
link: ./graph.png
|
link: ./graph.png
|
||||||
alt: A graph.
|
alt: A graph.
|
||||||
...
|
...
|
||||||
digraph G {
|
digraph G {
|
||||||
a -> b;
|
a -> b;
|
||||||
|
@ -102,7 +118,7 @@ With img and alt:
|
||||||
id: fn9
|
id: fn9
|
||||||
lang: graphviz
|
lang: graphviz
|
||||||
img: ./graph.png
|
img: ./graph.png
|
||||||
alt: A graph.
|
alt: A graph.
|
||||||
...
|
...
|
||||||
digraph G {
|
digraph G {
|
||||||
a -> b;
|
a -> b;
|
||||||
|
@ -125,19 +141,19 @@ With inner function:
|
||||||
---
|
---
|
||||||
id: fn11
|
id: fn11
|
||||||
args:
|
args:
|
||||||
x: 2
|
x: 2
|
||||||
...
|
...
|
||||||
#fn1() * x
|
`fn1()` * x
|
||||||
|
|
||||||
With inner function with args:
|
With inner function with args:
|
||||||
|
|
||||||
---
|
---
|
||||||
id: fn12
|
id: fn12
|
||||||
args:
|
args:
|
||||||
y: 1
|
y: 1
|
||||||
z: 2
|
z: 2
|
||||||
...
|
...
|
||||||
#y + #fn4(3, 4) + #z
|
#y + `fn4(3, 4)` + #z
|
||||||
|
|
||||||
With inner inner function:
|
With inner inner function:
|
||||||
|
|
||||||
|
@ -146,7 +162,7 @@ With inner inner function:
|
||||||
args:
|
args:
|
||||||
a: 1
|
a: 1
|
||||||
...
|
...
|
||||||
#a + #fn4(#a, #fn1())
|
#a + `fn4(#a, `fn1()`)`
|
||||||
|
|
||||||
# Code Blocks That Are Not Declarations
|
# Code Blocks That Are Not Declarations
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue