Question:
In the pandoc introduction to filters the following code is presented as an “example of a Lua filter”:Could anyone please let me know the following:
- Whether this is valid Lua?
- Where it is documented in the Lua docs?
Answer:
Yes, this is valid Lua. Lua files or strings are called “chunks” and are basically functions and may thus containreturn
statements like any other function. Their arguments are accessible through the vararg ...
.This is documented in the Lua 5.4 reference manual section on Chunks:
Lua handles a chunk as the body of an anonymous function with a variable number of arguments (see §3.4.11). As such, chunks can define local variables, receive arguments, and return values.
When starting a Lua file from the command line,
...
will be the command line arguments. When manually load
ing strings or loadfile
ing files, you get the chunk returned as a function and can decide what you want to pass.In fact it is considered good practice to
return
something from a require
d file rather than writing your library functions to the global namespace. return
is in that case equivalent to JS export
:foo.lua
:bar.lua
:If you have better answer, please add a comment about this, thank you!