----------------------------------- LITERATE ---------------------------------- -- Types functions extensions -- Changes newlines so everything is in one line function string:linearize() return self:gsub("\n", "\\\\n") end -- Checks if is empty function string:isempty() return self == '' end -- Checks if is directory function string:isdir() if self ~= "." and os.rename(self, self) == nil then return false end return true end -- 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", }, } 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.checkmetatype(type, meta, key) local mval, mtype = lit.getmetaval(meta, key) local err = "" if type ~= mtype then if type == "path" then if not(pandoc.path.directory(mval):isdir()) then err = "Invalid path '" .. mval .. "' in key '" .. key .. "'" end else err = "Invalid type '" .. mtype .. "' in key '" .. key .. "'" end end if not(err:isempty()) then lit.puts(err, "ERROR") end end function lit.checkmetatable(table, meta, key) local mval = lit.getmetaval(meta, key) local err = "" for _, pattern in pairs(table) do if mval:match("^" .. pattern .. "$") then err = "" break else err = "Invalid value '" .. mval .. "' in key '" .. key .. "'" end end if not(err:isempty()) then lit.puts(err, "ERROR") 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("Key '" .. key .. "' not found", "ERROR") 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") -- TODO checks for 1) extra keys and 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 ]]-- function lit.assert(e, status) if status == false then lit.puts("Aborted due previous errors", "ERROR") os.exit(1) end return e end 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.puts(msg, kind) local kind = kind or "INFO" local verbosity = lit.debuglevel(PANDOC_STATE.verbosity) level = lit.debuglevel(kind) if level >= verbosity then print("[" .. kind .. "] [LIT] " .. msg) end end function lit.parseyaml(rawyaml) -- TODO: avoid popen? 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("Empty YAML", "ERROR") else return lit.setmeta(yaml) end else lit.puts("Invalid YAML", "ERROR") return nil end end function lit.parseblock(parsed) lit.puts("Parsing " .. table.concat(parsed, "\n"):linearize()) 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