require("core.globals")
local ProFi
SILE = {}
SILE.version = require("core.version")
SILE.features = require("core.features")
SILE.lua_version = _VERSION:sub(-3)
SILE.lua_isjit = type(jit) == "table"
SILE.full_version = string.format("SILE %s (%s)", SILE.version, SILE.lua_isjit and jit.version or _VERSION)
SILE.quiet = false
if not SILE.lua_isjit and SILE.lua_version < "5.3" then
require("compat53")
end
SILE.utilities = require("core.utilities")
SU = SILE.utilities
require("core/deprecations")
local function core_loader (scope)
return setmetatable({}, {
__index = function (self, key)
local m = require(("%s.%s"):format(scope, key))
self[key] = m
return m
end,
})
end
SILE.Commands = {}
SILE.Help = {}
SILE.debugFlags = {}
SILE.nodeMakers = {}
SILE.tokenizers = {}
SILE.status = {}
SILE.scratch = {}
SILE.documentState = {}
SILE.rawHandlers = {}
SILE.input = {
filenames = {},
evaluates = {},
evaluateAfters = {},
uses = {},
options = {},
preambles = {}, postambles = {}, }
SILE.inputters = core_loader("inputters")
SILE.shapers = core_loader("shapers")
SILE.outputters = core_loader("outputters")
SILE.classes = core_loader("classes")
SILE.packages = core_loader("packages")
SILE.typesetters = core_loader("typesetters")
SILE.pagebuilders = core_loader("pagebuilders")
SILE.types = core_loader("types")
SILE.parserBits = require("core.parserbits")
SILE.frameParser = require("core.frameparser")
SILE.fontManager = require("core.fontmanager")
SILE.papersize = require("core.papersize")
local function runEvals (evals, arg)
for _, snippet in ipairs(evals) do
local pId = SILE.traceStack:pushText(snippet)
local status, func = pcall(load, snippet)
if status then
func()
else
SU.error(("Error parsing code provided in --%s snippet: %s"):format(arg, func))
end
SILE.traceStack:pop(pId)
end
end
function SILE.init ()
if not SILE.backend then
SILE.backend = "libtexpdf"
end
if SILE.backend == "libtexpdf" then
SILE.shaper = SILE.shapers.harfbuzz()
SILE.outputter = SILE.outputters.libtexpdf()
elseif SILE.backend == "cairo" then
SILE.shaper = SILE.shapers.pango()
SILE.outputter = SILE.outputters.cairo()
elseif SILE.backend == "debug" then
SILE.shaper = SILE.shapers.harfbuzz()
SILE.outputter = SILE.outputters.debug()
elseif SILE.backend == "text" then
SILE.shaper = SILE.shapers.harfbuzz()
SILE.outputter = SILE.outputters.text()
elseif SILE.backend == "dummy" then
SILE.shaper = SILE.shapers.harfbuzz()
SILE.outputter = SILE.outputters.dummy()
end
SILE.pagebuilder = SILE.pagebuilders.base()
io.stdout:setvbuf("no")
if SU.debugging("profile") then
ProFi = require("ProFi")
ProFi:start()
end
if SILE.makeDeps then
SILE.makeDeps:add(_G.executablePath)
end
runEvals(SILE.input.evaluates, "evaluate")
end
local function suggest_luarocks (module)
local guessed_module_name = module:gsub(".*%.", "") .. ".sile"
return ([[
If the expected module is a 3rd party extension you may need to install it
using LuaRocks. The details of how to do this are highly dependent on
your system and preferred installation method, but as an example installing
a 3rd party SILE module to a project-local tree where might look like this:
luarocks --lua-version %s --tree lua_modules install %s
This will install the LuaRocks to your project, then you need to tell your
shell to pass along that info about available LuaRocks paths to SILE. This
only needs to be done once in each shell.
eval $(luarocks --lua-version %s --tree lua_modules path)
Thereafter running SILE again should work as expected:
sile %s
]]):format(SILE.lua_version, guessed_module_name, SILE.lua_version, pl.stringx.join(" ", _G.arg or {}))
end
function SILE.use (module, options, reload)
local status, pack
if type(module) == "string" then
if module:match("/") then
SU.warn(([[Module names should not include platform-specific path separators
Using slashes like '/' or '\' in a module name looks like a path segment. This
may appear to work in some cases, but breaks cross platform compatibility.
Even on the platform with the matching separator, this can lead to packages
getting loaded more than once because Lua will cache one each of the different
formats. Please use '.' separators which are automatically translated to the
correct platform one. For example a correct use statement would be:
\use[module=%s] instead of \use[module=%s].
]]):format(module:gsub("/", "."), module))
end
status, pack = pcall(require, module)
if not status then
SU.error(
("Unable to use '%s':\n%s%s"):format(
module,
SILE.traceback and (" Lua " .. pack) or "",
suggest_luarocks(module)
)
)
end
elseif type(module) == "table" then
pack = module
end
local name = pack._name
local class = SILE.documentState.documentClass
if not pack.type then
SU.error("Modules must declare their type")
elseif pack.type == "class" then
SILE.classes[name] = pack
if class then
SU.error("Cannot load a class after one is already instantiated")
end
SILE.scratch.class_from_uses = pack
elseif pack.type == "inputter" then
SILE.inputters[name] = pack
SILE.inputter = pack(options)
elseif pack.type == "outputter" then
SILE.outputters[name] = pack
SILE.outputter = pack(options)
elseif pack.type == "shaper" then
SILE.shapers[name] = pack
SILE.shaper = pack(options)
elseif pack.type == "typesetter" then
SILE.typesetters[name] = pack
SILE.typesetter = pack(options)
elseif pack.type == "pagebuilder" then
SILE.pagebuilders[name] = pack
SILE.pagebuilder = pack(options)
elseif pack.type == "package" then
SILE.packages[pack._name] = pack
if class then
class:loadPackage(pack, options, reload)
else
table.insert(SILE.input.preambles, { pack = pack, options = options })
end
end
end
function SILE.require (dependency, pathprefix, deprecation_ack)
if pathprefix and not deprecation_ack then
local notice = string.format(
[[
Please don't use the path prefix mechanism; it was intended to provide
alternate paths to override core components but never worked well and is
causing portability problems. Just use Lua idiomatic module loading:
SILE.require("%s", "%s") → SILE.require("%s.%s")]],
dependency,
pathprefix,
pathprefix,
dependency
)
SU.deprecated("SILE.require", "SILE.require", "0.13.0", nil, notice)
end
dependency = dependency:gsub(".lua$", "")
local status, lib
if pathprefix then
status, lib = pcall(require, pl.stringx.join(".", { pathprefix, dependency }))
end
if not status then
local prefixederror = lib
status, lib = pcall(require, dependency)
if not status then
SU.error(
("Unable to find module '%s'%s"):format(
dependency,
SILE.traceback and ((pathprefix and "\n " .. prefixederror or "") .. "\n " .. lib) or ""
)
)
end
end
local class = SILE.documentState.documentClass
if not class and not deprecation_ack then
SU.warn(string.format(
[[
Use of SILE.require() is only supported in documents, packages, or class
init functions. It will not function fully before the class is instantiated.
Please just use the Lua require() function directly:
SILE.require("%s") → require("%s")]],
dependency,
dependency
))
end
if type(lib) == "table" and class then
if lib.type == "package" then
lib(class)
else
class:initPackage(lib)
end
end
return lib
end
function SILE.process (ast)
if not ast then
return
end
if SU.debugging("ast") then
SU.debugAST(ast, 0)
end
if type(ast) == "function" then
return ast()
end
for _, content in ipairs(ast) do
if type(content) == "string" then
SILE.typesetter:typeset(content)
elseif type(content) == "function" then
content()
elseif SILE.Commands[content.command] then
SILE.call(content.command, content.options, content)
elseif content.id == "content" or (not content.command and not content.id) then
local pId = SILE.traceStack:pushContent(content, "content")
SILE.process(content)
SILE.traceStack:pop(pId)
elseif type(content) ~= "nil" then
local pId = SILE.traceStack:pushContent(content)
SU.error("Unknown command " .. (tostring(content.command or content.id)))
SILE.traceStack:pop(pId)
end
end
end
local preloadedinputters = { "xml", "lua", "sil" }
local function detectFormat (doc, filename)
if #SILE.inputters == 0 then
for _, format in ipairs(preloadedinputters) do
local _ = SILE.inputters[format]
end
end
local contentDetectionOrder = {}
for _, inputter in pairs(SILE.inputters) do
if inputter.order then
table.insert(contentDetectionOrder, inputter)
end
end
table.sort(contentDetectionOrder, function (a, b)
return a.order < b.order
end)
local initialround = filename and 1 or 2
for round = initialround, 3 do
for _, inputter in ipairs(contentDetectionOrder) do
SU.debug("inputter", "Running content type detection round", round, "with", inputter._name)
if inputter.appropriate(round, filename, doc) then
return inputter._name
end
end
end
SU.error(("Unable to pick inputter to process input from '%s'"):format(filename))
end
function SILE.processString (doc, format, filename, options)
local cpf
if not filename then
cpf = SILE.currentlyProcessingFile
local caller = debug.getinfo(2, "Sl")
SILE.currentlyProcessingFile = caller.short_src .. ":" .. caller.currentline
end
local inputter
if
filename
and pl.path.normcase(pl.path.normpath(filename)) == pl.path.normcase(SILE.input.filenames[1])
and SILE.inputter
then
inputter = SILE.inputter
else
format = format or detectFormat(doc, filename)
if not SILE.quiet then
io.stderr:write(("<%s> as %s\n"):format(SILE.currentlyProcessingFile, format))
end
inputter = SILE.inputters[format](options)
if filename and pl.path.normcase(filename) == pl.path.normcase(SILE.input.filenames[1]:gsub("^-$", "STDIN")) then
SILE.inputter = inputter
end
end
local pId = SILE.traceStack:pushDocument(SILE.currentlyProcessingFile, doc)
inputter:process(doc)
SILE.traceStack:pop(pId)
if cpf then
SILE.currentlyProcessingFile = cpf
end
end
function SILE.processFile (filename, format, options)
local lfs = require("lfs")
local doc
if filename == "-" then
filename = "STDIN"
doc = io.stdin:read("*a")
else
filename = filename:gsub("\\", "/")
if not SILE.masterFilename then
SILE.masterFilename = pl.path.splitext(pl.path.normpath(filename))
end
if SILE.input.filenames[1] and not SILE.masterDir then
SILE.masterDir = pl.path.dirname(SILE.input.filenames[1])
end
if SILE.masterDir and SILE.masterDir:len() >= 1 then
_G.extendSilePath(SILE.masterDir)
_G.extendSilePathRocks(SILE.masterDir .. "/lua_modules")
end
filename = SILE.resolveFile(filename) or SU.error("Could not find file")
local mode = lfs.attributes(filename).mode
if mode ~= "file" and mode ~= "named pipe" then
SU.error(filename .. " isn't a file or named pipe, it's a " .. mode .. "!")
end
if SILE.makeDeps then
SILE.makeDeps:add(filename)
end
local file, err = io.open(filename)
if not file then
print("Could not open " .. filename .. ": " .. err)
return
end
doc = file:read("*a")
end
local cpf = SILE.currentlyProcessingFile
SILE.currentlyProcessingFile = filename
local pId = SILE.traceStack:pushDocument(filename, doc)
local ret = SILE.processString(doc, format, filename, options)
SILE.traceStack:pop(pId)
SILE.currentlyProcessingFile = cpf
return ret
end
function SILE.typesetNaturally (frame, func)
local saveTypesetter = SILE.typesetter
if SILE.typesetter.frame then
SILE.typesetter.frame:leave(SILE.typesetter)
end
SILE.typesetter = SILE.typesetters.base(frame)
SILE.settings:temporarily(func)
SILE.typesetter:leaveHmode()
SILE.typesetter:chuck()
SILE.typesetter.frame:leave(SILE.typesetter)
SILE.typesetter = saveTypesetter
if SILE.typesetter.frame then
SILE.typesetter.frame:enter(SILE.typesetter)
end
end
function SILE.resolveFile (filename, pathprefix)
local candidates = {}
if pathprefix then
candidates[#candidates + 1] = pl.path.join(pathprefix, "?")
end
candidates[#candidates + 1] = "?"
if SILE.masterDir then
for path in SU.gtoke(SILE.masterDir .. ";" .. tostring(os.getenv("SILE_PATH")), ";") do
if path.string and path.string ~= "nil" then
if pathprefix then
candidates[#candidates + 1] = pl.path.join(path.string, pathprefix, "?")
end
candidates[#candidates + 1] = pl.path.join(path.string, "?")
end
end
end
local path = table.concat(candidates, ";")
local resolved, err = package.searchpath(filename, path, "/")
if resolved then
if SILE.makeDeps then
SILE.makeDeps:add(resolved)
end
elseif SU.debugging("paths") then
SU.debug("paths", ("Unable to find file '%s': %s"):format(filename, err))
end
return resolved
end
function SILE.call (command, options, content)
options = options or {}
content = content or {}
if SILE.traceback and type(content) == "table" and not content.lno then
local caller = debug.getinfo(2, "Sl")
content.file, content.lno = caller.short_src, caller.currentline
end
local pId = SILE.traceStack:pushCommand(command, content, options)
if not SILE.Commands[command] then
SU.error("Unknown command " .. command)
end
local result = SILE.Commands[command](options, content)
SILE.traceStack:pop(pId)
return result
end
function SILE.registerCommand (name, func, help, pack, cheat)
local class = SILE.documentState.documentClass
if not cheat then
SU.deprecated(
"SILE.registerCommand",
"class:registerCommand",
"0.14.0",
"0.16.0",
[[Commands are being scoped to the document classes they are loaded into rather than being globals.]]
)
end
if not cheat or not class or class.type ~= "class" then
return SILE.classes.base.registerCommand(nil, name, func, help, pack)
end
return class:registerCommand(name, func, help, pack)
end
function SILE.setCommandDefaults (command, options)
local oldCommand = SILE.Commands[command]
SILE.Commands[command] = function (defaults, content)
for k, v in pairs(options) do
defaults[k] = defaults[k] or v
end
return oldCommand(defaults, content)
end
end
function SILE.registerUnit (unit, spec)
if SILE.types.unit[unit] then
SILE.types.unit[unit] = nil
end
SILE.types.unit[unit] = spec
end
function SILE.paperSizeParser (size)
SU.deprecated("SILE.paperSizeParser", "SILE.papersize", "0.15.0", "0.16.0")
return SILE.papersize(size)
end
function SILE.finish ()
SILE.documentState.documentClass:finish()
SILE.font.finish()
runEvals(SILE.input.evaluateAfters, "evaluate-after")
if SILE.makeDeps then
SILE.makeDeps:write()
end
if not SILE.quiet then
io.stderr:write("\n")
end
if SU.debugging("profile") then
ProFi:stop()
ProFi:writeReport(pl.path.splitext(SILE.input.filenames[1]) .. ".profile.txt")
end
if SU.debugging("versions") then
SILE.shaper:debugVersions()
end
end
SILE.traceStack = require("core.tracestack")()
SILE.settings = require("core.settings")()
require("core.hyphenator-liang")
require("core.languages")
SILE.linebreak = require("core.break")
require("core.frame")
SILE.font = require("core.font")
return SILE