----------------------------------- 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", }, } -- TODO function lit.stringify(content) if pandoc.utils.type(content) == "Inlines" then return pandoc.utils.stringify(content:walk { Quoted = function(quoted) local quote = (quoted.quotetype == SingleQuoted and '"' or "'") quoted = pandoc.utils.stringify(quoted.content) return pandoc.Str(quote .. quoted .. quote) end, Inline = function(inline) return pandoc.utils.stringify(inline) end, }) else return pandoc.utils.stringify(content) end end function lit.meta2table(meta) for k, v in pairs(meta) do if pandoc.utils.type(v) == "table" then lit.meta2table(v) else v = lit.stringify(v) -- print(k, v) end end return meta end lit.test = lit.meta2table(pandoc.read(("src/locate.yaml"):read_text()).meta) -- Messages for the user lit.msg = { ["INFO"] = { ["parsing"] = { ["en"] = "Parsing:\n#1", ["es"] = "Realizando análisis sintáctico:\n#1", }, }, ["WARNING"] = { }, ["ERROR"] = { ["invalid_key"] = { ["en"] = "Invalid key '#1' with value '#2'", ["es"] = "Clave '#1' inválida con valor '#2'", }, ["invalid_path"] = { ["en"] = "Invalid path '#1' in key '#2'", ["es"] = "Ruta '#1' inválida en clave '#2'", }, ["invalid_type"] = { ["en"] = "Invalid type '#1' in key '#2'", ["es"] = "Tipo '#1' inválido en clave '#2'", }, ["invalid_value"] = { ["en"] = "Invalid value '#1' in key '#2'", ["es"] = "Valor '#1' inválido en clave '#2'", }, ["no_key"] = { ["en"] = "Key '#1' not found", ["es"] = "Clave '#1' no encontrada", }, ["aborted"] = { ["en"] = "Aborted due previous errors", ["es"] = "Abortado debido a previos errores", }, ["yaml_empty"] = { ["en"] = "Empty YAML", ["es"] = "YAML vacío", }, ["yaml_invalid"] = { ["en"] = "Invalid YAML", ["es"] = "YAML inválido", }, }, } function lit.debuglevel(str) if str == "ERROR" then lit.status = false return 2 elseif str == "WARNING" then return 1 else return 0 end end function lit.setmsg(msg, ...) local lang = os.lang() msg = (msg[lang] ~= nil and msg[lang] or msg) for i, str in ipairs({...}) do msg = msg:gsub("#" .. i, str) end return msg end function lit.getmsg(key, ...) local levelname = "INFO" local level = 0 for mtype, msgs in pairs(lit.msg) do if msgs[key] ~= nil then key = lit.setmsg(msgs[key], ...) levelname, level = mtype, lit.debuglevel(mtype) break end end return key, levelname, level end function lit.puts(key, ...) local verbosity = lit.debuglevel(PANDOC_STATE.verbosity) local msg, levelname, level = lit.getmsg(key, ...) if level >= verbosity then print("[" .. levelname .. "] [LIT] " .. msg) end end function lit.getmetaval(meta, key) local mtype = pandoc.utils.type(meta[key]) local mval = meta[key] if mtype == "Inlines" then mval = pandoc.utils.stringify(mval) mtype = pandoc.utils.type(mval) end return mval, mtype end function lit.checkmetaextra(meta) for key, val in pairs(meta) do local missing1 = lit.metastruct["mandatory"][key] == nil local missing2 = lit.metastruct["optional"][key] == nil local mval = lit.getmetaval(meta, key) if missing1 and missing2 then lit.puts("invalid_key", key, mval) end end end function lit.checkmetatype(type, meta, key) local mval, mtype = lit.getmetaval(meta, key) if type ~= mtype then if type == "path" then if not(pandoc.path.directory(mval):isdir()) then lit.puts("invalid_path", mval, key) end else lit.puts("invalid_type", mtype, key) end end end -- TODO: has bug function lit.checkmetatable(table, meta, key) local mval = lit.getmetaval(meta, key) for _, pattern in pairs(table) do if mval:match("^" .. pattern .. "$") == nil then lit.puts("invalid_value", mval, key) end end end function lit.checkmeta(meta, kind) for key, val in pairs(lit.metastruct[kind]) do if meta[key] == nil then if kind == "mandatory" then lit.puts("no_key", key) end else if type(val) == "table" then lit.checkmetatable(val, meta, key) else lit.checkmetatype(val, meta, key) end end end end function lit.setmeta(meta) lit.checkmeta(meta, "mandatory") lit.checkmeta(meta, "optional") lit.checkmetaextra(meta) -- TODO checks for 2) duplicates -- Set defaults if lit.status = true return meta end -- Grammars lit.g = {} -- Lexical elements lit.newline = lpeg.P"\r"^-1 * lpeg.P"\n" lit.space = lpeg.S" \t" lit.anyspace = lpeg.S" \t\r\n" lit.spot = (1 - lit.anyspace) lit.any = (lit.spot + lit.space) lit.yamlheader = lit.space^0 * lpeg.P"---" * lit.space^0 * lit.newline lit.yamlfooter = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline^-1 lit.yamlbody = -lit.yamlfooter * lit.any^0 * lit.newline lit.id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0 lit.ref = lpeg.P"#" * lit.id -- Blocks grammar lit.g.block = lpeg.P { "Block"; Block = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name"); YAML = lpeg.C(lit.yamlheader * lit.yamlbody^0 * lit.yamlfooter); Code = lpeg.C((lit.any + lit.newline)^0); } -- Evals Lisp code --[[ function lit.eval(code) local is_passed, out = pcall ( function () return fennel.eval(code) end, function (e) return e end ) local lua = "" local out = tostring(out) local preview = out:gsub("\n.*", "") if is_passed then lua = fennel.compileString(code) end return {is_passed = is_passed, preview = preview, out = out, lua = lua} end ]]-- -- TODO function lit.assert(e, status) if status == false then lit.puts("aborted") os.exit(1) end return e end function lit.parseyaml(rawyaml) local yaml = io.popen("pandoc -t json <<<\"" .. rawyaml .. "\" 2>&1") if yaml:read("*l"):sub(1, 1) == "{" then yaml = pandoc.read(rawyaml).meta if pandoc.utils.stringify(yaml):isempty() then lit.puts("yaml_empty") else return lit.setmeta(yaml) end else lit.puts("yaml_invalid") return nil end end function lit.parseblock(parsed) lit.puts("parsing", table.concat(parsed, ""):indent()) local yaml = lit.parseyaml(parsed[1]) -- TODO: parsecode return "TODO" end function lit.getblock(codeblock) local parsed = lpeg.match(lit.g.block, codeblock.text) parsed = (parsed ~= nil and lit.parseblock(parsed) or parsed) return codeblock, lit.status end function lit.getinsert(code) return code, lit.status end return lit