-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(inputters): Add AST inputter for reading back SILE's AST outputter
- Loading branch information
Showing
3 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters