| 1 |
% Copyright (C) 2023-2025 Kale Ewasiuk |
| 2 |
% |
| 3 |
% Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
% of this software and associated documentation files (the "Software"), to deal |
| 5 |
% in the Software without restriction, including without limitation the rights |
| 6 |
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
% copies of the Software, and to permit persons to whom the Software is |
| 8 |
% furnished to do so, subject to the following conditions: |
| 9 |
% |
| 10 |
% The above copyright notice and this permission notice shall be included in |
| 11 |
% all copies or substantial portions of the Software. |
| 12 |
% |
| 13 |
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 14 |
% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 15 |
% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 16 |
% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 |
% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 18 |
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 21 |
% OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
|
| 23 |
|
| 24 |
\ProvidesPackage{addtoluatexpath}[2025-01-11] |
| 25 |
|
| 26 |
\RequirePackage{luacode} |
| 27 |
|
| 28 |
\providecommand{\input@path}{} % initialize input@path if not defined yet |
| 29 |
|
| 30 |
\begin{luacode*} |
| 31 |
addtoluatexpath = {} |
| 32 |
|
| 33 |
addtoluatexpath.paths = {"."} -- table containing paths added by this package |
| 34 |
|
| 35 |
function addtoluatexpath.find_file(f) -- find a path in list of addtoluatexpath.paths |
| 36 |
for i, k in pairs(addtoluatexpath.paths) do |
| 37 |
local fp = kpse.find_file(k ..'/'.. f) |
| 38 |
if (fp) then |
| 39 |
return fp |
| 40 |
end |
| 41 |
end -- if nothing returned, issue a package error |
| 42 |
texio.write_nl('addtoluatexpath searched for file: '..f) |
| 43 |
texio.write_nl('addtoluatexpath searched paths were: '..token.get_macro('input@path')) |
| 44 |
tex.sprint('\\PackageError{addtoluatexpath}{a file was not found, see log.}{}') |
| 45 |
tex.sprint('\\stop') |
| 46 |
end |
| 47 |
|
| 48 |
function addtoluatexpath.main(atlp_raw) -- add to path from raw string |
| 49 |
local penlight = require'penlight' |
| 50 |
|
| 51 |
local atlp_tbl = require'luakeys'().parse(atlp_raw, {naked_as_value=true}) -- paths as table |
| 52 |
|
| 53 |
local atlp_no_lua = atlp_tbl['nolua'] or false -- check and set nolua=true |
| 54 |
local atlp_no_tex = atlp_tbl['notex'] or false -- check and set notex=true |
| 55 |
local atlp_no_error = atlp_tbl['noerror'] or false -- check and set noerror=true |
| 56 |
|
| 57 |
atlp_tbl = penlight.List(atlp_tbl) -- convert to pl.List for easy manipulation |
| 58 |
|
| 59 |
for __, p in ipairs(atlp_tbl) do |
| 60 |
if p:find('*') == nil then -- add paths without *, and continue the loop after |
| 61 |
if not atlp_no_lua then package.path = package.path .. ';'..p..'/?.lua;' end |
| 62 |
if not atlp_no_tex then token.set_macro('input@path', token.get_macro('input@path')..'{'..p..'/}', 'global') end |
| 63 |
if (not atlp_no_error) and (not penlight.path.exists(p)) then |
| 64 |
tex.sprint('\\PackageError{addtoluatexpath}{directory "'..p..'" was not found}{}') |
| 65 |
end |
| 66 |
addtoluatexpath.paths[#addtoluatexpath.paths + 1] = p -- append the added path to global list |
| 67 |
else |
| 68 |
local p, c = p:gsub('*','') -- if * added, include subdirectories |
| 69 |
local atlp_subdirs = penlight.List(penlight.dir.getdirectories(p)) |
| 70 |
-- troubleshooting --texio.write_nl(penlight.pretty.write(atlp_subdirs)) |
| 71 |
if c == 2 then |
| 72 |
atlp_subdirs = atlp_subdirs:map(function(s) return s ..'/**' end) -- add ** to subdirs for recursive inclusion |
| 73 |
end |
| 74 |
atlp_tbl:append(p) -- make sure p (current path without *) is still added! |
| 75 |
atlp_tbl:extend(atlp_subdirs) -- extend path to include additional subdirs; the for loop is lengthened |
| 76 |
end |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
|
| 81 |
function addtoluatexpath.prt_paths() |
| 82 |
texio.write_nl('Lua Paths >>> \n'..package.path:gsub(';','\n')) |
| 83 |
texio.write_nl('\nTeX Paths >>> \n'..token.get_macro('input@path'):gsub('}{','\n')) |
| 84 |
end |
| 85 |
|
| 86 |
---- ---- ---- add paths passed as package options |
| 87 |
addtoluatexpath.main(token.get_macro('@raw@[email protected]')) |
| 88 |
---- ---- ---- |
| 89 |
|
| 90 |
\end{luacode*} |
| 91 |
|
| 92 |
\NewDocumentCommand{\addtoluatexpath}{m}{\luadirect{addtoluatexpath.main(\luastring{#1})}} % a command |
| 93 |
|
| 94 |
\AtEndOfPackage{\let\@unprocessedoptions\relax} |