computable-pandoc/src/literate.lua

65 lines
1.5 KiB
Lua
Raw Normal View History

2023-03-11 14:14:24 -06:00
----------------------------------- LITERATE ----------------------------------
2023-03-09 20:13:01 -06:00
2023-03-15 17:33:53 -06:00
-- Variable for all literate stuff
2023-03-11 14:14:24 -06:00
local lit = {}
2023-03-10 21:09:31 -06:00
-- Ordered collection of fn call, fn declarations or literal elements
2023-03-11 14:14:24 -06:00
lit.collection = {}
2023-02-16 19:04:01 -06:00
2023-03-15 17:33:53 -06:00
-- Grammars
lit.g = {}
2023-02-16 19:04:01 -06:00
-- Lexical elements
2023-03-16 18:00:24 -06:00
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.id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
lit.ref = lpeg.P"#" * lit.id
lit.yaml_header = lit.space^0 * lpeg.P"---" * lit.space^0 * lit.newline
lit.yaml_body = -lpeg.P"." * lit.any^0 * lit.newline
lit.yaml_footer = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline
2023-03-10 21:09:31 -06:00
2023-03-16 18:00:24 -06:00
-- Blocks grammar
lit.g.block = lpeg.P {
"Block";
Block = lpeg.C(lpeg.V"YAML" * lpeg.V"Code");
YAML = lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer;
Code = (lit.any + lit.newline)^0;
2023-02-16 19:04:01 -06:00
}
2023-03-06 20:17:33 -06:00
-- Evals Lisp code
2023-03-15 17:33:53 -06:00
--[[
2023-03-11 14:14:24 -06:00
function lit.eval(code)
2023-03-09 20:13:01 -06:00
local is_passed, out = pcall (
2023-03-07 16:57:14 -06:00
function () return fennel.eval(code) end,
function (e) return e end
)
2023-03-09 20:13:01 -06:00
local lua = ""
local out = tostring(out)
local preview = out:gsub("\n.*", "")
2023-03-07 16:57:14 -06:00
if is_passed then
lua = fennel.compileString(code)
2023-03-06 20:17:33 -06:00
end
2023-03-07 16:57:14 -06:00
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
2023-03-06 20:17:33 -06:00
end
2023-03-15 17:33:53 -06:00
]]--
2023-03-16 16:42:44 -06:00
function lit.parse_blocks(codeblock)
2023-03-16 18:00:24 -06:00
parsed = lpeg.match(lit.g.block, codeblock.text)
if codeblock.text ~= parsed then
print(codeblock.text)
print(parsed)
end
2023-03-15 17:33:53 -06:00
return codeblock
end
2023-02-16 19:04:01 -06:00
2023-03-16 16:42:44 -06:00
function lit.parse_inserts(code)
2023-03-16 18:00:24 -06:00
-- print(code)
2023-03-16 16:42:44 -06:00
return code
2023-03-10 21:09:31 -06:00
end
2023-03-11 14:14:24 -06:00
return lit