-
DescriptionHi all, I have a Lua script that makes use of local lfs = require("lfs") -- Load the Lua File System library -- TODO check documentation, does quarto support imports? now, that's not shipped as standard and needs to be imported with lua rocks luarocks install luafilesystem My question is Supporting informationThis is the extension script workflow: It is being used so that i can check for the existence of files in a directory with a specific extension. The exact code looks like this: -- Function to find files with specific extensions in a directory
local function find_files(directory, extensions)
local files = {}
for file in lfs.dir(directory) do
if file ~= "." and file ~= ".." then
local _, ext = file:match(".+(%..+)$")
if ext and extensions[ext:lower()] then
table.insert(files, file)
end
end
end
return files
end whereby you pass in some extensions, and it returns an array of files that match the extension. An alternative is to use ls command, but to my knowledge, that is only supported by unix like systems and i'm not sure it would work for windows (i'm on Ubuntu), something like this: -- Commented out, will it cause windows systems to crash?
-- Function to find files with specific extensions in a directory
local function find_files(directory, extensions)
local files = {}
local handle = io.popen('ls "' .. directory .. '"') -- Using 'ls' command, this is only available on unix systems TODO better approach required
if handle then
local output = handle:read("*a")
handle:close()
for file in output:gmatch("[^\r\n]+") do
local _, ext = file:match(".+(%..+)$")
if ext and extensions[ext:lower()] then
table.insert(files, file)
end
end
end
return files
end
``
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Ok after some research on ClI in windows, i see i can make it work without requiring third party tools, something like this. So maybe the question is obsolete for me, but maybe an answer might help other people who do require third party tools. The workaround does some checks and then sets the command accordingly -- Use platform-specific commands to list files in the directory
if package.config:sub(1,1) == '\\' then -- Windows
command = 'dir /b "' .. directory .. '"'
else -- Unix-like systems (Linux, macOS, etc.)
command = 'ls "' .. directory .. '"'
end
local handle = io.popen(command) |
Beta Was this translation helpful? Give feedback.
-
If the scripts depends on some external dependency, yes it will be necessary to have those available. Quarto will just run the Lua Script as part of the filter chain during pandoc conversion, or directly using the Lua provided by Pandoc. So only what is available in Lua API (https://quarto.org/docs/extensions/lua-api.html) will be available without more dependencies to install. |
Beta Was this translation helpful? Give feedback.
-
Hi! Just a follow up question. What if I ship the module together with the extension? (specifically, luafilesystem) I tried the following hack, which works outside of quarto, but I get an error in run as the quarto extension. local current_path = package.cpath
local module_folder = "./luafilesystem/?.so"
package.cpath = current_path .. ";" .. module_folder
require "lfs" |
Beta Was this translation helpful? Give feedback.
If the scripts depends on some external dependency, yes it will be necessary to have those available. Quarto will just run the Lua Script as part of the filter chain during pandoc conversion, or directly using the Lua provided by Pandoc.
So only what is available in Lua API (https://quarto.org/docs/extensions/lua-api.html) will be available without more dependencies to install.