computable-pandoc/src/literate.lua

78 lines
1.6 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-11 14:14:24 -06:00
lit.space = lpeg.S" \t\r\n"
lit.spot = (1 - lit.space)
lit.t1 = lpeg.S"()" -- TMP
lit.t2 = (1 - (lit.t1 + lit.space)) -- TMP
2023-03-10 21:09:31 -06:00
2023-03-11 14:14:24 -06:00
function lit.add_call(str)
table.insert(lit.collection, {call = str})
2023-03-10 21:09:31 -06:00
end
2023-03-11 14:14:24 -06:00
function lit.add_declaration(str)
table.insert(lit.collection, {declaration = str})
2023-03-10 21:09:31 -06:00
end
2023-03-11 14:14:24 -06:00
function lit.add_lit(str)
table.insert(lit.collection, {literal = str})
2023-03-10 21:09:31 -06:00
end
2023-02-16 19:04:01 -06:00
2023-03-15 17:33:53 -06:00
-- Inlines grammar
lit.g.inlines = lpeg.P {
2023-02-16 19:04:01 -06:00
"Doc";
2023-03-11 14:14:24 -06:00
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
Call = lit.t1^1 / lit.add_call;
Declaration = lit.t2^1 / lit.add_declaration;
Literal = lit.space^1 / lit.add_lit;
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-15 17:33:53 -06:00
print(codeblock)
return codeblock
end
2023-02-16 19:04:01 -06:00
2023-03-16 16:42:44 -06:00
function lit.parse_inserts(code)
print(code)
2023-03-15 17:33:53 -06:00
-- lpeg.match(lit.g.inlines, raw)
--[[
2023-03-10 21:09:31 -06:00
local doc = ""
2023-03-11 14:14:24 -06:00
for i, t in ipairs(lit.collection) do
2023-03-10 21:09:31 -06:00
for k, v in pairs(t) do
2023-03-15 17:33:53 -06:00
print(i, k, v)
2023-03-10 21:09:31 -06:00
doc = doc .. v
end
end
return doc
2023-03-15 17:33:53 -06:00
]]--
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