Skip to content

Commit

Permalink
refactor(core): Move masterDir and masterFilename generation to requi…
Browse files Browse the repository at this point in the history
…re less intervention
  • Loading branch information
alerque committed Apr 27, 2023
1 parent 8d1eef6 commit c07e7f6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Expand Up @@ -26,6 +26,7 @@ globals = {
"luautf8",
"pl",
"fluent",
"extendSilePath",
"SYSTEM_SILE_PATH",
"SHARED_LIB_EXT"
}
Expand Down
1 change: 1 addition & 0 deletions .luarc.json
Expand Up @@ -7,6 +7,7 @@
"luautf8",
"pl",
"fluent",
"extendSilePath",
"SYSTEM_SILE_PATH",
"SHARED_LIB_EXT"
],
Expand Down
5 changes: 0 additions & 5 deletions core/cli.lua
Expand Up @@ -47,11 +47,6 @@ cli.parseArguments = function ()
opts.INPUT = "-"
end
SILE.input.filename = opts.INPUT
-- Turn slashes around in the event we get passed a path from a Windows shell
local filename = opts.INPUT:gsub("\\", "/")
-- Strip extension
SILE.masterFilename = string.match(filename, "(.+)%..-$") or filename
SILE.masterDir = SILE.masterFilename:match("(.-)[^%/]+$")
end
if opts.backend then
SILE.backend = opts.backend
Expand Down
4 changes: 3 additions & 1 deletion core/pathsetup.lua.in
Expand Up @@ -68,4 +68,6 @@ else
extendPaths(".")
end

return executable, extendPaths
_G.extendSilePath = extendPaths

return executable
14 changes: 13 additions & 1 deletion core/sile.lua
Expand Up @@ -306,6 +306,18 @@ function SILE.processFile (filename, format, options)
filename = "STDIN"
doc = io.stdin:read("*a")
else
-- Turn slashes around in the event we get passed a path from a Windows shell
filename = filename:gsub("\\", "/")
if not SILE.masterFilename then
-- Strip extension
SILE.masterFilename = string.match(filename, "(.+)%..-$") or filename
end
if SILE.masterFilename and not SILE.masterDir then
SILE.masterDir = SILE.masterFilename:match("(.-)[^%/]+$")
end
if SILE.masterDir and SILE.masterDir:len() >= 1 then
extendSilePath(SILE.masterDir)
end
filename = SILE.resolveFile(filename)
if not filename then
SU.error("Could not find file")
Expand Down Expand Up @@ -356,7 +368,7 @@ function SILE.resolveFile (filename, pathprefix)
candidates[#candidates+1] = "?"
-- Iterate through the directory of the master file, the SILE_PATH variable, and the current directory
-- Check for prefixed paths first, then the plain path in that fails
if SILE.masterFilename then
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
Expand Down
16 changes: 9 additions & 7 deletions sile.in
Expand Up @@ -6,9 +6,15 @@ SHARED_LIB_EXT = "@SHARED_LIB_EXT@"

-- At this point at run time we may or may not have anything useful in package.path,
-- so require() isn't something we can rely on.
local status, executable, extendPaths = pcall(dofile, "@SILE_PATH@/core/pathsetup.lua")
local status, executable = pcall(dofile, "@SILE_PATH@/core/pathsetup.lua")
if not status then
executable, extendPaths = dofile("./core/pathsetup.lua")
executable = dofile("./core/pathsetup.lua")
end

-- On FreeBSD, Lua's debug module doesn't give us info above the top of the stack.
-- Since our detection is currently burried in a module it doesn't tell us the executable.
if executable ~= "=[C]" then
executable = debug.getinfo(1, "S").source
end

-- This global is set here and *also* in the core library, since this
Expand All @@ -34,11 +40,7 @@ end

SILE.init()

if SILE.masterFilename then

if SILE.masterDir:len() >= 1 then
extendPaths(SILE.masterDir)
end
if SILE.input.filename and SILE.input.filename:len() > 0 then

if SU.debugging("profile") then
ProFi:start()
Expand Down

0 comments on commit c07e7f6

Please sign in to comment.