On Thursday, October 5, 2023 at 6:19:10 AM UTC-4 Albert Krewinkel wrote:
jcr writes:
> Another approach is to use Lua's debug facilities to find the path to
> the script being executed, and add its directory to the Lua package
> path. This is the function I use:
>
> ```
> local function addMyDirToPath()
> local myFile = debug.getinfo(2, "S").source:sub(2)
> local myDir = myFile:match("(.*)/.*") or '.'
> package.path = ("%s/?.lua;%s"):format(myDir, package.path)
> end
> ```
That's elegant. Another way to get the value of `myDir` would be
local myDir = pandoc.path.directory(arg[0])
and should also work on all platforms. It relies on pandoc's built-in
path library and on a feature of the default lua interpreter, which
pandoc replicates for compatibility:
https://www.lua.org/manual/5.4/manual.html#7
I can't take credit for the elegance: that part comes from stackoverflow (https://stackoverflow.com/a/23535333). The answer below it there does, in fact, suggest arg[0], but the elegant solution got more votes. I think I'll change my function to use arg[0] as you suggest.