computable-pandoc/src/literate.lua

90 lines
2.4 KiB
Lua

-------------------------------------------------------------------------------
-- IMPORTANT: these lines are for development setup
-- IMPORTANT: these lines are changed to "local fennel = mod"
-- Enables Fennel <https://fennel-lang.org> for Lisp-Lua compatibility
local src_root = pandoc.path.directory(PANDOC_SCRIPT_FILE)
local fennel_lua = pandoc.path.join({src_root, "../opt/fennel.lua"})
package.path = package.path .. ";" .. fennel_lua
local fennel = require("fennel")
-------------------------------------------------------------------------------
------------------------------- GRAMMAR related -------------------------------
-- Ordered collection of fn call, fn declarations or literal elements
local collection = {}
-- Lua LPeg shortcuts
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 space = S" \t\r\n"
local spot = (1 - space) -- TMP
local t1 = S"()" -- TMP
local t2 = (1 - (t1 + space)) -- TMP
local function add_call(str)
table.insert(collection, {call = str})
end
local function add_declaration(str)
table.insert(collection, {declaration = str})
end
local function add_lit(str)
table.insert(collection, {literal = str})
end
-- Grammar
G = P {
"Doc";
Doc = (V"Call" + V"Declaration" + V"Literal")^0;
Call = t1^1 / add_call;
Declaration = t2^1 / add_declaration;
Literal = space^1 / add_lit;
}
--------------------------------- LNP related ---------------------------------
-- Evals Lisp code
local function 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 parse(_)
local doc = ""
for i, t in ipairs(collection) do
for k, v in pairs(t) do
print(i, k, v)
doc = doc .. v
end
end
return doc
end
function read(sources)
raw = ""
for _, src in ipairs(sources) do
raw = raw .. src.text
end
return raw
end
-- Calls Pandoc Reader.
function Reader(sources, options)
local raw = read(sources)
local doc = parse(lpeg.match(G, raw))
return pandoc.read(doc, FORMAT, options)
end