Skip to content

Commit

Permalink
feat(inputters): Add AST inputter for reading back SILE's AST outputter
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Oct 3, 2024
1 parent 0b3dde7 commit bcab257
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/sile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ function SILE.process (ast)
end
end

local preloadedinputters = { "xml", "lua", "sil" }
local preloadedinputters = { "xml", "lua", "ast", "sil" }

local function detectFormat (doc, filename)
-- Preload default reader types so content detection has something to work with
Expand Down
42 changes: 42 additions & 0 deletions inputters/ast.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local base = require("inputters.base")
local serpent = require("serpent")

local inputter = pl.class(base)
inputter._name = "ast"

inputter.order = 35

function inputter.appropriate (round, filename, doc)
if round == 1 then
return filename:match(".ast$")
elseif round == 2 then
local sniff = doc:sub(1, 100)
local promising = sniff:match("^{\n [^ ]") or sniff:match("command =") or sniff:match("loadstring or load")
return promising and inputter.appropriate(3, filename, doc) or false
elseif round == 3 then
local status, _ = serpent.load(doc, { safe = true })
return status and true or false
end
end

function inputter.parse (_, doc)
local status, result = serpent.load(doc, { safe = true })
if not status then
SU.error(result)
end
return result
end

function inputter:process (doc)
local tree = self:parse(doc)
if not tree.type then
-- hoping tree is an AST
self:requireClass(tree)
return SILE.process(tree)
else
SILE.use(tree, self.options)
end
end

return inputter

2 changes: 2 additions & 0 deletions outputters/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function outputter:preProcess (ast)
}
)
outfile:write(serialized)
-- Only dump the *first* AST we're given dodging SILE generated ones (like folios) later
self.preProcess = function () end
end

function outputter:finish ()
Expand Down

0 comments on commit bcab257

Please sign in to comment.