public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* requiring other lua files from parent directories from within filters
@ 2023-10-03 16:17 ch_...-S0/GAf8tV78@public.gmane.org
       [not found] ` <ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: ch_...-S0/GAf8tV78@public.gmane.org @ 2023-10-03 16:17 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1396 bytes --]

I have the following file structure:

inner/script.lua
library/library.lua

I try to load the library from within the script

library = require("../library/library.lua")

but get an error when running inside inner: pandoc lua script.lua

Error running Lua:
script.lua:1: module '../library/library.lua' not found:
no field package.preload['../library/library.lua']
no file '/usr/local/share/lua/5.4////library/library/lua.lua'
no file '/usr/local/share/lua/5.4////library/library/lua/init.lua'
no file '/usr/local/lib/lua/5.4////library/library/lua.lua'
no file '/usr/local/lib/lua/5.4////library/library/lua/init.lua'
no file './///library/library/lua.lua'
no file './///library/library/lua/init.lua'
no file '/usr/local/lib/lua/5.4////library/library/lua.so'
no file '/usr/local/lib/lua/5.4/loadall.so'
no file './///library/library/lua.so'
no file '/usr/local/lib/lua/5.4/.so'
no file '/usr/local/lib/lua/5.4/loadall.so'
no file './.so'
stack traceback:
script.lua:1: in main chunk

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 2977 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: requiring other lua files from parent directories from within filters
       [not found] ` <ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2023-10-03 16:50   ` Bastien DUMONT
  2023-10-04 13:54     ` jcr
  0 siblings, 1 reply; 7+ messages in thread
From: Bastien DUMONT @ 2023-10-03 16:50 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

The argument of require() is not a path, but a string that is passed to a template. The template is composed of semi-colon separated strings where ? is stands for the argument of require() (after all . have been replaced with /). For instance, if the template is:

  ./?.lua;/usr/local/share/lua/5.4/?.lua

and you pass 'library.library' to it, Lua will search in ./library/library.lua and /usr/local/share/lua/5.4/library/library.lua

The path is controlled by environment variables. In your case, if all loaded files are in the ../library directory, I would advise to call your script with:

  LUA_PATH='../library/?.lua' pandoc-lua script.lua

and to write in inner/script.lua:

  library = require("library")

Of course, you can also set the path to '../?.lua;./?.lua' if you want to be able to load files from the current directory and to write explicitely the path to library.lua as library.library!

Le Tuesday 03 October 2023 à 09:17:04AM, ch_...-S0/GAf8tV78@public.gmane.org a écrit :
> I have the following file structure:
> 
> inner/script.lua
> library/library.lua
> 
> I try to load the library from within the script
> 
> library = require("../library/library.lua")
> 
> but get an error when running inside inner: pandoc lua script.lua
> 
> Error running Lua:
> script.lua:1: module '../library/library.lua' not found:
> no field package.preload['../library/library.lua']
> no file '/usr/local/share/lua/5.4////library/library/lua.lua'
> no file '/usr/local/share/lua/5.4////library/library/lua/init.lua'
> no file '/usr/local/lib/lua/5.4////library/library/lua.lua'
> no file '/usr/local/lib/lua/5.4////library/library/lua/init.lua'
> no file './///library/library/lua.lua'
> no file './///library/library/lua/init.lua'
> no file '/usr/local/lib/lua/5.4////library/library/lua.so'
> no file '/usr/local/lib/lua/5.4/loadall.so'
> no file './///library/library/lua.so'
> no file '/usr/local/lib/lua/5.4/.so'
> no file '/usr/local/lib/lua/5.4/loadall.so'
> no file './.so'
> stack traceback:
> script.lua:1: in main chunk
> 
> --
> You received this message because you are subscribed to the Google Groups
> "pandoc-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to [1]pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit [2]https://groups.google.com/d/msgid/
> pandoc-discuss/ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n%40googlegroups.com.
> 
> References:
> 
> [1] mailto:pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> [2] https://groups.google.com/d/msgid/pandoc-discuss/ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n%40googlegroups.com?utm_medium=email&utm_source=footer

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/ZRxGUn7zsP-pRL0O%40localhost.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: requiring other lua files from parent directories from within filters
  2023-10-03 16:50   ` Bastien DUMONT
@ 2023-10-04 13:54     ` jcr
       [not found]       ` <d94cef59-9869-4376-9d36-b38cab195025n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: jcr @ 2023-10-04 13:54 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 3930 bytes --]

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
```
To adapt this to your directory structure, just change the format string on 
the third line of the function to ` "%s/../library/?.lua;%s"`.
On Tuesday, October 3, 2023 at 12:50:39 PM UTC-4 Bastien DUMONT wrote:

> The argument of require() is not a path, but a string that is passed to a 
> template. The template is composed of semi-colon separated strings where ? 
> is stands for the argument of require() (after all . have been replaced 
> with /). For instance, if the template is:
>
> ./?.lua;/usr/local/share/lua/5.4/?.lua
>
> and you pass 'library.library' to it, Lua will search in 
> ./library/library.lua and /usr/local/share/lua/5.4/library/library.lua
>
> The path is controlled by environment variables. In your case, if all 
> loaded files are in the ../library directory, I would advise to call your 
> script with:
>
> LUA_PATH='../library/?.lua' pandoc-lua script.lua
>
> and to write in inner/script.lua:
>
> library = require("library")
>
> Of course, you can also set the path to '../?.lua;./?.lua' if you want to 
> be able to load files from the current directory and to write explicitely 
> the path to library.lua as library.library!
>
> Le Tuesday 03 October 2023 à 09:17:04AM, ch_...-S0/GAf8tV78@public.gmane.org a écrit :
> > I have the following file structure:
> > 
> > inner/script.lua
> > library/library.lua
> > 
> > I try to load the library from within the script
> > 
> > library = require("../library/library.lua")
> > 
> > but get an error when running inside inner: pandoc lua script.lua
> > 
> > Error running Lua:
> > script.lua:1: module '../library/library.lua' not found:
> > no field package.preload['../library/library.lua']
> > no file '/usr/local/share/lua/5.4////library/library/lua.lua'
> > no file '/usr/local/share/lua/5.4////library/library/lua/init.lua'
> > no file '/usr/local/lib/lua/5.4////library/library/lua.lua'
> > no file '/usr/local/lib/lua/5.4////library/library/lua/init.lua'
> > no file './///library/library/lua.lua'
> > no file './///library/library/lua/init.lua'
> > no file '/usr/local/lib/lua/5.4////library/library/lua.so'
> > no file '/usr/local/lib/lua/5.4/loadall.so'
> > no file './///library/library/lua.so'
> > no file '/usr/local/lib/lua/5.4/.so'
> > no file '/usr/local/lib/lua/5.4/loadall.so'
> > no file './.so'
> > stack traceback:
> > script.lua:1: in main chunk
> > 
> > --
> > You received this message because you are subscribed to the Google Groups
> > "pandoc-discuss" group.
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email
> > to [1]pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To view this discussion on the web visit [2]
> https://groups.google.com/d/msgid/
> > pandoc-discuss/ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n%40googlegroups.com.
> > 
> > References:
> > 
> > [1] mailto:pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> > [2] 
> https://groups.google.com/d/msgid/pandoc-discuss/ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n%40googlegroups.com?utm_medium=email&utm_source=footer
>
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/d94cef59-9869-4376-9d36-b38cab195025n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 5946 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: requiring other lua files from parent directories from within filters
       [not found]       ` <d94cef59-9869-4376-9d36-b38cab195025n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2023-10-05 10:11         ` Albert Krewinkel
       [not found]           ` <875y3lnzho.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Albert Krewinkel @ 2023-10-05 10:11 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


jcr <ffi.appdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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


-- 
Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: requiring other lua files from parent directories from within filters
       [not found]           ` <875y3lnzho.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
@ 2023-10-05 14:49             ` jcr
  2023-10-05 15:00             ` jcr
  1 sibling, 0 replies; 7+ messages in thread
From: jcr @ 2023-10-05 14:49 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1560 bytes --]


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.

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/9c7ac85d-ae26-41b2-941e-088bad98c353n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 2277 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: requiring other lua files from parent directories from within filters
       [not found]           ` <875y3lnzho.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  2023-10-05 14:49             ` jcr
@ 2023-10-05 15:00             ` jcr
       [not found]               ` <82dabe2b-28c9-4ea3-a299-36a8c01b31b6n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  1 sibling, 1 reply; 7+ messages in thread
From: jcr @ 2023-10-05 15:00 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1041 bytes --]


On Thursday, October 5, 2023 at 6:19:10 AM UTC-4 Albert Krewinkel wrote:


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 find with pandoc 3.1.8 that while `arg` is defined for a script executed 
with `pandoc lua`, it is nil for a filter executed with `pandoc -L`. So I'm 
back to my borrowed elegant solution.

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/82dabe2b-28c9-4ea3-a299-36a8c01b31b6n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 1660 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: requiring other lua files from parent directories from within filters
       [not found]               ` <82dabe2b-28c9-4ea3-a299-36a8c01b31b6n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2023-10-05 20:34                 ` Albert Krewinkel
  0 siblings, 0 replies; 7+ messages in thread
From: Albert Krewinkel @ 2023-10-05 20:34 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


jcr <ffi.appdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> On Thursday, October 5, 2023 at 6:19:10 AM UTC-4 Albert Krewinkel wrote:
>
>  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 find with pandoc 3.1.8 that while `arg` is defined for a script executed with `pandoc lua`, it is nil for a filter executed with `pandoc
> -L`. So I'm back to my borrowed elegant solution.

Alternatively, adding this to the top of the file would ensure that the
relevant values are set:

    local arg = arg or {[0] = PANDOC_SCRIPT_FILE}



-- 
Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/871qe8olcu.fsf%40zeitkraut.de.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-10-05 20:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-03 16:17 requiring other lua files from parent directories from within filters ch_...-S0/GAf8tV78@public.gmane.org
     [not found] ` <ee1ceb10-3318-48d0-ae1a-0ca538a3cdc9n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-10-03 16:50   ` Bastien DUMONT
2023-10-04 13:54     ` jcr
     [not found]       ` <d94cef59-9869-4376-9d36-b38cab195025n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-10-05 10:11         ` Albert Krewinkel
     [not found]           ` <875y3lnzho.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2023-10-05 14:49             ` jcr
2023-10-05 15:00             ` jcr
     [not found]               ` <82dabe2b-28c9-4ea3-a299-36a8c01b31b6n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-10-05 20:34                 ` Albert Krewinkel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).