diff --git a/dist/lin.bundle.lua b/dist/lin.bundle.lua index 6477963..418e942 100644 --- a/dist/lin.bundle.lua +++ b/dist/lin.bundle.lua @@ -6577,41 +6577,45 @@ lit.status = true -- Grammars function lit.g(name) - -- Variable for grammars collection + -- Variable for grammars collection local grammars = {} + 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 + -- Lexical elements - local newline = lpeg.P"\r"^-1 * lpeg.P"\n" - local space = lpeg.S" \t" - local anyspace = lpeg.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 * lpeg.P"---" * space^0 * newline - local yamlfooter = space^0 * lpeg.P"..." * space^0 * newline^-1 + local yamlheader = space^0 * P"---" * space^0 * newline + local yamlfooter = space^0 * 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.B"\\" * lpeg.P"#" * lpeg.C(id) + local id = R("az", "AZ") * R("az", "AZ", "09")^0 + local ref = -B"\\" * P"#" * C(id) local notref = (1 - ref)^0 -- Declaration grammar - grammars["declaration"] = lpeg.P { + grammars["declaration"] = P { "Declaration"; - Declaration = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name"); - YAML = lpeg.C(yamlheader * yamlbody^0 * yamlfooter); - Code = lpeg.C((any + newline)^0); + Declaration = Ct(V"YAML" * V"Code", "name"); + YAML = C(yamlheader * yamlbody^0 * yamlfooter); + Code = C((any + newline)^0); } -- References grammar - grammars["references"] = notref * lpeg.P { + grammars["references"] = notref * P { "References"; - References = lpeg.Ct((ref * notref)^0); + References = Ct((ref * notref)^0); } * -1 return grammars[name] end -- Prints located messages -function lit.puts(key, ...) +function lit.puts(yaml_key, ...) -- Returns debug level as number local function debuglevel(str) @@ -6673,16 +6677,18 @@ ERROR: 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 + for i, str in ipairs({...}) do + key = key:gsub("#" .. i, str) + end levelname, level = mtype, debuglevel(mtype) break end end - return key, levelname, level + return key, levelname, level end local verbosity = debuglevel(PANDOC_STATE.verbosity) - local msg, levelname, level = getmsg(key, ...) + local msg, levelname, level = getmsg(yaml_key, ...) if level >= verbosity then print("[" .. levelname .. "] [LIT] " .. msg) end @@ -6710,10 +6716,10 @@ function lit.exam(doc) ]]-- -- Parses code declaration - local function parse(codeblock) + local function parse(codeblock) -- Checks code declarations - local function check(parsed) + local function check(parsed) -- Indicates if checks passes local ispass = true @@ -6722,12 +6728,12 @@ 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", - }, + }, ["cmd"] = {".+"}, -- Value as string == type to match ["args"] = "table", @@ -6752,7 +6758,7 @@ function lit.exam(doc) ["lisp"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } }, } - + -- Prints error and does not pass the check local function putserr(...) lit.puts(...) @@ -6778,15 +6784,15 @@ function lit.exam(doc) end return meta end - + -- Checks for valid command local function checkcmd(meta) if meta["cmd"] then - cmd = meta["cmd"]:split()[1] + local cmd = meta["cmd"]:split()[1] if os.isunix() then - status = io.try("type", cmd) - if not(status) then - putserr("invalid_cmd", cmd) + local status = io.try("type", cmd) + if not(status) then + putserr("invalid_cmd", cmd) end else lit.puts("skip_check", "checkcmd") @@ -6795,82 +6801,82 @@ function lit.exam(doc) end -- Checks for extra and unwanted metadata - local function checkextra(meta) - for key, val in pairs(meta) do - local missing1 = metastruct["mandatory"][key] == nil - local missing2 = metastruct["optional"][key] == nil - if missing1 and missing2 then - putserr("invalid_key", key) - end - end - end + local function checkextra(meta) + for key, _ in pairs(meta) do + local missing1 = metastruct["mandatory"][key] == nil + local missing2 = metastruct["optional"][key] == nil + if missing1 and missing2 then + putserr("invalid_key", key) + end + end + end -- Checks for valid metadata - local function checkmeta(meta, kind) + local function checkmeta(metadata, 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 - putserr("invalid_path", val, key) - end - else - putserr("invalid_type", type2, key) - end - end - end + 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 + putserr("invalid_path", val, key) + end + else + putserr("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 = "" + 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 - putserr("invalid_value", val, err) - end - end + end + end + if not(err:isempty()) then + putserr("invalid_value", val, err) + end + end - for key, val in pairs(metastruct[kind]) do - if meta[key] == nil and kind == "mandatory" then - putserr("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 + for key, val in pairs(metastruct[kind]) do + if metadata[key] == nil and kind == "mandatory" then + putserr("no_key", key) + elseif metadata[key] and type(val) == "table" then + checkpattern(val, metadata, key) + elseif metadata[key] then + checktype(val, metadata, key) + end + end + end -- Parses metadata - local function parsemeta(yaml) + 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) + meta = pandoc.metatotable(res.meta) + checkmeta(meta, "mandatory") + checkmeta(meta, "optional") + checkextra(meta) checkcmd(meta) elseif isok and pandoc.utils.stringify(res.meta):isempty() then - putserr("meta_empty") - else - putserr("meta_invalid") - end + putserr("meta_empty") + else + putserr("meta_invalid") + end return addeval(meta) - end + end -- TODO -- NOTE: if not valid code, return meta["eval"] = {} local function checkcode(code, meta) - references = lpeg.match(lit.g("references"), code) + local references = lpeg.match(lit.g("references"), code) if references then for _, ref in ipairs(references) do print("reference:", ref) @@ -6879,23 +6885,23 @@ function lit.exam(doc) return "TODO" end - lit.puts("checking", table.concat(parsed, ""):indent()) - local meta = parsemeta(parsed[1]) + lit.puts("checking", table.concat(parsed, ""):indent()) + local meta = parsemeta(parsed[1]) -- TODO -- Add each meta to a variable -- Remove meta if doesn't pass checkcode meta["code"] = checkcode(parsed[2], meta) - return meta - end + return meta + end - local parsed = lpeg.match(lit.g("declaration"), codeblock.text) - local checked = (parsed ~= nil and check(parsed) or parsed) + local parsed = lpeg.match(lit.g("declaration"), codeblock.text) + local checked = (parsed ~= nil and check(parsed) or parsed) -- TODO: -- evaluated = eval(checked) -- TODO: write evaluated and do overrides with warns -- return evaluated return codeblock - end + end -- TODO -- Evals and inserts inline code @@ -6920,14 +6926,7 @@ end return { -- Parses and evals literate programming - { Pandoc = function(doc) return lit.exam(doc) end }, - { - -- Parses and evals natural programming - -- TODO - Inlines = function(inlines) - md = pandoc.utils.stringify(inlines) - md = nat.get(md) - return inlines - end, - } + { Pandoc = function(e) return lit.exam(e) end }, + -- TODO Parses and evals natural programming + -- { Inlines = function(e) return nat.get(pandoc.utils.stringify(e)) end }, } diff --git a/dist/lin.min.lua b/dist/lin.min.lua index 3d9dd10..97e20e0 100644 --- a/dist/lin.min.lua +++ b/dist/lin.min.lua @@ -20,41 +20,45 @@ lit.status = true -- Grammars function lit.g(name) - -- Variable for grammars collection + -- Variable for grammars collection local grammars = {} + 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 + -- Lexical elements - local newline = lpeg.P"\r"^-1 * lpeg.P"\n" - local space = lpeg.S" \t" - local anyspace = lpeg.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 * lpeg.P"---" * space^0 * newline - local yamlfooter = space^0 * lpeg.P"..." * space^0 * newline^-1 + local yamlheader = space^0 * P"---" * space^0 * newline + local yamlfooter = space^0 * 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.B"\\" * lpeg.P"#" * lpeg.C(id) + local id = R("az", "AZ") * R("az", "AZ", "09")^0 + local ref = -B"\\" * P"#" * C(id) local notref = (1 - ref)^0 -- Declaration grammar - grammars["declaration"] = lpeg.P { + grammars["declaration"] = P { "Declaration"; - Declaration = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name"); - YAML = lpeg.C(yamlheader * yamlbody^0 * yamlfooter); - Code = lpeg.C((any + newline)^0); + Declaration = Ct(V"YAML" * V"Code", "name"); + YAML = C(yamlheader * yamlbody^0 * yamlfooter); + Code = C((any + newline)^0); } -- References grammar - grammars["references"] = notref * lpeg.P { + grammars["references"] = notref * P { "References"; - References = lpeg.Ct((ref * notref)^0); + References = Ct((ref * notref)^0); } * -1 return grammars[name] end -- Prints located messages -function lit.puts(key, ...) +function lit.puts(yaml_key, ...) -- Returns debug level as number local function debuglevel(str) @@ -116,16 +120,18 @@ ERROR: 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 + for i, str in ipairs({...}) do + key = key:gsub("#" .. i, str) + end levelname, level = mtype, debuglevel(mtype) break end end - return key, levelname, level + return key, levelname, level end local verbosity = debuglevel(PANDOC_STATE.verbosity) - local msg, levelname, level = getmsg(key, ...) + local msg, levelname, level = getmsg(yaml_key, ...) if level >= verbosity then print("[" .. levelname .. "] [LIT] " .. msg) end @@ -153,10 +159,10 @@ function lit.exam(doc) ]]-- -- Parses code declaration - local function parse(codeblock) + local function parse(codeblock) -- Checks code declarations - local function check(parsed) + local function check(parsed) -- Indicates if checks passes local ispass = true @@ -165,12 +171,12 @@ 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", - }, + }, ["cmd"] = {".+"}, -- Value as string == type to match ["args"] = "table", @@ -195,7 +201,7 @@ function lit.exam(doc) ["lisp"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } }, } - + -- Prints error and does not pass the check local function putserr(...) lit.puts(...) @@ -221,15 +227,15 @@ function lit.exam(doc) end return meta end - + -- Checks for valid command local function checkcmd(meta) if meta["cmd"] then - cmd = meta["cmd"]:split()[1] + local cmd = meta["cmd"]:split()[1] if os.isunix() then - status = io.try("type", cmd) - if not(status) then - putserr("invalid_cmd", cmd) + local status = io.try("type", cmd) + if not(status) then + putserr("invalid_cmd", cmd) end else lit.puts("skip_check", "checkcmd") @@ -238,82 +244,82 @@ function lit.exam(doc) end -- Checks for extra and unwanted metadata - local function checkextra(meta) - for key, val in pairs(meta) do - local missing1 = metastruct["mandatory"][key] == nil - local missing2 = metastruct["optional"][key] == nil - if missing1 and missing2 then - putserr("invalid_key", key) - end - end - end + local function checkextra(meta) + for key, _ in pairs(meta) do + local missing1 = metastruct["mandatory"][key] == nil + local missing2 = metastruct["optional"][key] == nil + if missing1 and missing2 then + putserr("invalid_key", key) + end + end + end -- Checks for valid metadata - local function checkmeta(meta, kind) + local function checkmeta(metadata, 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 - putserr("invalid_path", val, key) - end - else - putserr("invalid_type", type2, key) - end - end - end + 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 + putserr("invalid_path", val, key) + end + else + putserr("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 = "" + 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 - putserr("invalid_value", val, err) - end - end + end + end + if not(err:isempty()) then + putserr("invalid_value", val, err) + end + end - for key, val in pairs(metastruct[kind]) do - if meta[key] == nil and kind == "mandatory" then - putserr("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 + for key, val in pairs(metastruct[kind]) do + if metadata[key] == nil and kind == "mandatory" then + putserr("no_key", key) + elseif metadata[key] and type(val) == "table" then + checkpattern(val, metadata, key) + elseif metadata[key] then + checktype(val, metadata, key) + end + end + end -- Parses metadata - local function parsemeta(yaml) + 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) + meta = pandoc.metatotable(res.meta) + checkmeta(meta, "mandatory") + checkmeta(meta, "optional") + checkextra(meta) checkcmd(meta) elseif isok and pandoc.utils.stringify(res.meta):isempty() then - putserr("meta_empty") - else - putserr("meta_invalid") - end + putserr("meta_empty") + else + putserr("meta_invalid") + end return addeval(meta) - end + end -- TODO -- NOTE: if not valid code, return meta["eval"] = {} local function checkcode(code, meta) - references = lpeg.match(lit.g("references"), code) + local references = lpeg.match(lit.g("references"), code) if references then for _, ref in ipairs(references) do print("reference:", ref) @@ -322,23 +328,23 @@ function lit.exam(doc) return "TODO" end - lit.puts("checking", table.concat(parsed, ""):indent()) - local meta = parsemeta(parsed[1]) + lit.puts("checking", table.concat(parsed, ""):indent()) + local meta = parsemeta(parsed[1]) -- TODO -- Add each meta to a variable -- Remove meta if doesn't pass checkcode meta["code"] = checkcode(parsed[2], meta) - return meta - end + return meta + end - local parsed = lpeg.match(lit.g("declaration"), codeblock.text) - local checked = (parsed ~= nil and check(parsed) or parsed) + local parsed = lpeg.match(lit.g("declaration"), codeblock.text) + local checked = (parsed ~= nil and check(parsed) or parsed) -- TODO: -- evaluated = eval(checked) -- TODO: write evaluated and do overrides with warns -- return evaluated return codeblock - end + end -- TODO -- Evals and inserts inline code @@ -363,14 +369,7 @@ end return { -- Parses and evals literate programming - { Pandoc = function(doc) return lit.exam(doc) end }, - { - -- Parses and evals natural programming - -- TODO - Inlines = function(inlines) - md = pandoc.utils.stringify(inlines) - md = nat.get(md) - return inlines - end, - } + { Pandoc = function(e) return lit.exam(e) end }, + -- TODO Parses and evals natural programming + -- { Inlines = function(e) return nat.get(pandoc.utils.stringify(e)) end }, } diff --git a/src/literate.lua b/src/literate.lua index 61894d3..d1e5976 100644 --- a/src/literate.lua +++ b/src/literate.lua @@ -9,41 +9,45 @@ lit.status = true -- Grammars function lit.g(name) - -- Variable for grammars collection + -- Variable for grammars collection local grammars = {} + 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 + -- Lexical elements - local newline = lpeg.P"\r"^-1 * lpeg.P"\n" - local space = lpeg.S" \t" - local anyspace = lpeg.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 * lpeg.P"---" * space^0 * newline - local yamlfooter = space^0 * lpeg.P"..." * space^0 * newline^-1 + local yamlheader = space^0 * P"---" * space^0 * newline + local yamlfooter = space^0 * 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.B"\\" * lpeg.P"#" * lpeg.C(id) + local id = R("az", "AZ") * R("az", "AZ", "09")^0 + local ref = -B"\\" * P"#" * C(id) local notref = (1 - ref)^0 -- Declaration grammar - grammars["declaration"] = lpeg.P { + grammars["declaration"] = P { "Declaration"; - Declaration = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name"); - YAML = lpeg.C(yamlheader * yamlbody^0 * yamlfooter); - Code = lpeg.C((any + newline)^0); + Declaration = Ct(V"YAML" * V"Code", "name"); + YAML = C(yamlheader * yamlbody^0 * yamlfooter); + Code = C((any + newline)^0); } -- References grammar - grammars["references"] = notref * lpeg.P { + grammars["references"] = notref * P { "References"; - References = lpeg.Ct((ref * notref)^0); + References = Ct((ref * notref)^0); } * -1 return grammars[name] end -- Prints located messages -function lit.puts(key, ...) +function lit.puts(yaml_key, ...) -- Returns debug level as number local function debuglevel(str) @@ -64,16 +68,18 @@ function lit.puts(key, ...) 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 + for i, str in ipairs({...}) do + key = key:gsub("#" .. i, str) + end levelname, level = mtype, debuglevel(mtype) break end end - return key, levelname, level + return key, levelname, level end local verbosity = debuglevel(PANDOC_STATE.verbosity) - local msg, levelname, level = getmsg(key, ...) + local msg, levelname, level = getmsg(yaml_key, ...) if level >= verbosity then print("[" .. levelname .. "] [LIT] " .. msg) end @@ -101,10 +107,10 @@ function lit.exam(doc) ]]-- -- Parses code declaration - local function parse(codeblock) + local function parse(codeblock) -- Checks code declarations - local function check(parsed) + local function check(parsed) -- Indicates if checks passes local ispass = true @@ -113,12 +119,12 @@ 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", - }, + }, ["cmd"] = {".+"}, -- Value as string == type to match ["args"] = "table", @@ -143,7 +149,7 @@ function lit.exam(doc) ["lisp"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } }, } - + -- Prints error and does not pass the check local function putserr(...) lit.puts(...) @@ -169,15 +175,15 @@ function lit.exam(doc) end return meta end - + -- Checks for valid command local function checkcmd(meta) if meta["cmd"] then - cmd = meta["cmd"]:split()[1] + local cmd = meta["cmd"]:split()[1] if os.isunix() then - status = io.try("type", cmd) - if not(status) then - putserr("invalid_cmd", cmd) + local status = io.try("type", cmd) + if not(status) then + putserr("invalid_cmd", cmd) end else lit.puts("skip_check", "checkcmd") @@ -186,82 +192,82 @@ function lit.exam(doc) end -- Checks for extra and unwanted metadata - local function checkextra(meta) - for key, val in pairs(meta) do - local missing1 = metastruct["mandatory"][key] == nil - local missing2 = metastruct["optional"][key] == nil - if missing1 and missing2 then - putserr("invalid_key", key) - end - end - end + local function checkextra(meta) + for key, _ in pairs(meta) do + local missing1 = metastruct["mandatory"][key] == nil + local missing2 = metastruct["optional"][key] == nil + if missing1 and missing2 then + putserr("invalid_key", key) + end + end + end -- Checks for valid metadata - local function checkmeta(meta, kind) + local function checkmeta(metadata, 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 - putserr("invalid_path", val, key) - end - else - putserr("invalid_type", type2, key) - end - end - end + 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 + putserr("invalid_path", val, key) + end + else + putserr("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 = "" + 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 - putserr("invalid_value", val, err) - end - end + end + end + if not(err:isempty()) then + putserr("invalid_value", val, err) + end + end - for key, val in pairs(metastruct[kind]) do - if meta[key] == nil and kind == "mandatory" then - putserr("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 + for key, val in pairs(metastruct[kind]) do + if metadata[key] == nil and kind == "mandatory" then + putserr("no_key", key) + elseif metadata[key] and type(val) == "table" then + checkpattern(val, metadata, key) + elseif metadata[key] then + checktype(val, metadata, key) + end + end + end -- Parses metadata - local function parsemeta(yaml) + 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) + meta = pandoc.metatotable(res.meta) + checkmeta(meta, "mandatory") + checkmeta(meta, "optional") + checkextra(meta) checkcmd(meta) elseif isok and pandoc.utils.stringify(res.meta):isempty() then - putserr("meta_empty") - else - putserr("meta_invalid") - end + putserr("meta_empty") + else + putserr("meta_invalid") + end return addeval(meta) - end + end -- TODO -- NOTE: if not valid code, return meta["eval"] = {} local function checkcode(code, meta) - references = lpeg.match(lit.g("references"), code) + local references = lpeg.match(lit.g("references"), code) if references then for _, ref in ipairs(references) do print("reference:", ref) @@ -270,23 +276,23 @@ function lit.exam(doc) return "TODO" end - lit.puts("checking", table.concat(parsed, ""):indent()) - local meta = parsemeta(parsed[1]) + lit.puts("checking", table.concat(parsed, ""):indent()) + local meta = parsemeta(parsed[1]) -- TODO -- Add each meta to a variable -- Remove meta if doesn't pass checkcode meta["code"] = checkcode(parsed[2], meta) - return meta - end + return meta + end - local parsed = lpeg.match(lit.g("declaration"), codeblock.text) - local checked = (parsed ~= nil and check(parsed) or parsed) + local parsed = lpeg.match(lit.g("declaration"), codeblock.text) + local checked = (parsed ~= nil and check(parsed) or parsed) -- TODO: -- evaluated = eval(checked) -- TODO: write evaluated and do overrides with warns -- return evaluated return codeblock - end + end -- TODO -- Evals and inserts inline code diff --git a/src/pandoc.lua b/src/pandoc.lua index 3133977..157445a 100644 --- a/src/pandoc.lua +++ b/src/pandoc.lua @@ -2,14 +2,7 @@ return { -- Parses and evals literate programming - { Pandoc = function(doc) return lit.exam(doc) end }, - { - -- Parses and evals natural programming - -- TODO - Inlines = function(inlines) - md = pandoc.utils.stringify(inlines) - md = nat.get(md) - return inlines - end, - } + { Pandoc = function(e) return lit.exam(e) end }, + -- TODO Parses and evals natural programming + -- { Inlines = function(e) return nat.get(pandoc.utils.stringify(e)) end }, }