I was originally following the idea from Garrick Peschke
To import all .tex files inside a folder, but I want my Lua function to also search inside subfolders.
Unfortunately I can't make the Lua function work on subfolders. It is worth noting that TeX doesn't recognize this as an error, any .tex file will be properly processed while folders will not.
function inputAll(dir)
for content in lfs.dir(dir) do
if ("." ~= content) and (".." ~= content) then
fullpath = dir .."/".. content
contentType = lfs.attributes(fullpath, "mode")
if isTexFile(content, contentType) then
tex.sprint("\\input{" .. fullpath .. "}")
elseif ("directory" == contentType) then
return inputAll(fullpath)
end
end
end
end
function isTexFile(dirOrFolder, contentType)
return ("file" == contentType) and (string.find(dirOrFolder, ".+%.tex"))
end
Following the idea proposed by Skeen to use an inner function, the result is the same: it works on files but not on folders.
function inputAll(dir)
local function inner(m)
for content in lfs.dir(m) do
if ("." ~= content) and (".." ~= content) then
fullpath = m .."/".. content
contentType = lfs.attributes(fullpath, "mode")
if isTexFile(content, contentType) then
tex.sprint("\\input{" .. fullpath .. "}")
elseif ("directory" == contentType) then
return inner(fullpath);
end
end
end
end
inner(dir)
end
function isTexFile(dirOrFolder, contentType)
return ("file" == contentType) and (string.find(dirOrFolder, ".+%.tex"))
end
following are some system details:
Mac Os X: 10.13.6
TeX 3.141592653 (TeX Live 2022)
LuaTeX, Version 1.15.0 (TeX Live 2022)
|_Compiled with lua version 5.3.6
TeXShop: 4.44 (4.44)
Conclusion
As David Carlisle, pointed out, LuaTeX from TexLive 2022 is actually able to recurse when the latest instruction does not use return.
With TexLive 2018 it was not working with or without return. Unfortunately I haven't saved any evidence so you'll have to take my words on it.
It is still unclear to me if provided a suitable proper tail recursive algoritm LuaTex will work or not:
AFAIK Lua needs the return statement to execute tail recursion properly. LuaTex I don't really know.
Final words:
I've been messing around with lua recursion in some other tex works of mine, and it seems that using a suitable algorithm will and using only pure lua (not sure whether it's needed or not) will allow the use of return statement no problem.
By pure lua I mean not messing with tex table. E.g.: tex.sprint( ... )
I did not had enough time to understand all the lfs library code to understand if there is another way to get the content as list (without me preparing the data first, essentially creating it myself).
The following code should be enough to prove what I meant
function excludeDependencyRecursive(csvListOfGroupIdColonArtifactId, result, textDecorator)
if (csvListOfGroupIdColonArtifactId) then
local beginIndex, endIndex = string.find(csvListOfGroupIdColonArtifactId,",");
texio.write_nl( "term and log", "beginIndex : " .. (beginIndex or "nil") .. " endIndex : " .. (endIndex or "nil"))
local first = string.sub(csvListOfGroupIdColonArtifactId, 1, getEndIndex(endIndex, csvListOfGroupIdColonArtifactId))
--[[ first is always a valid string ]]
local groupId, artifactId = getGroupAndArtifact(first)
result = result .. textDecorator("<dependency>") .. "\\+ \\\\"
result = result .. textDecorator("<groupId>" .. groupId .. "</groupId>") .. "\\\\"
result = result .. textDecorator("<artifactId>" .. artifactId .. "</artifactId>") .. "\\\\"
result = result .. textDecorator("\\dots") .. "\\- \\\\"
result = result .. textDecorator("</dependency>") .. "\\\\"
--[[ lua's shortcircut for IF endIndex THEN string.sub ELSE nil ]]
local rest = endIndex and string.sub(csvListOfGroupIdColonArtifactId, 1+endIndex, #csvListOfGroupIdColonArtifactId)
texio.write_nl( "term and log", "first : " .. first .. " rest : " .. (rest or "nil"))
return excludeDependencyRecursive(rest, result, textDecorator)
end
return result
end
--[[ can throw an error if the groupAndArtifact is not a string of the form A:B ]]
function getGroupAndArtifact(groupAndArtifact)
--[[ both are local ]]
local beginIndex, endIndex = string.find(groupAndArtifact,":");
local first = string.sub(groupAndArtifact, 1, endIndex-1)
local rest = string.sub(groupAndArtifact, 1+endIndex, #groupAndArtifact)
return first, rest
end
function getEndIndex(index, string)
--[[ lua version of C if then else ( A ? B : C) ]]
return index and (index - 1) or #string
end
What you have seen is a not so elegant decorator which you can call with
excludeDependencyRecursive(csvListOfGroupIdColonArtifactId, "", orangeBackgroudDecorator)
where
greenBackgroudDecorator = function (x) return "\\colorbox[HTML]{".. colorGreen .."}{" .. x .. "}" end
For those wondering about \\, \+, \-, excludeDependencyRecursive's caller was using latex's tabbing environment
Thank you all very much for the help, I mean it.
stdin,stdout, andstderr, you're only left with 16 files. macOS almost certainly supports more, but LuaTeX may not have been compiled with a higher limit. You could perhaps request that they set a higher limit on the LuaTeX mailing list, but it would probably be easiest to remove the recursion. – Max Chernoff Aug 10 '22 at 00:31