Can the relative paths for requiring Lua files be used in pre-render scripts too? #12893
-
DescriptionHey folks, I understand that, as of Quarto 1.4, I ought to be able to call a secondary Lua file using a relative path. I'm able to make that work with a filter, but I'm hoping to reuse that code with a pre-render script that my extension also contributes as metadata. To reproducequarto create project
quarto add jimjam-slam/test-quarto-lua-modules # the extension itself is called my-extension The extension has three Lua files:
The extension contributes the filter, as well as metadata that triggers the prerender script. The default file refers to the extension name under ---
title: "Hello"
filters:
- my-extension
--- If I comment out the last three lines of
But when the pre-render script is enabled, it doesn't seem to be able to find the module:
If I change he import in the prerender script to refer down through the extension folder then I'm fine: util = require("_extensions.jimjam-slam.my-extension.util")
But I don't want to hardcode the extension path, since it can be installed namespaced by org or not. I also tried this: util = require("_extensions.jimjam-slam.my-extension.util;_extensions.my-extension.util") But this only works if the path that matches the reprex's extension path is last (I was hoping it would try both after reading the Lua docs). Is there a way to reliably ensure that modules are found from pre-render scripts? I was also thinking about some sort of try-catch pattern, but this feels like overengineering! Quarto check
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Here's a bit of a brute force method that checks both possible extension paths (for the pre-render script only, not the filter). It works, though! -- try to load a module from multiple paths
-- modified version of https://stackoverflow.com/a/17878208/3246758
local function try_require(path_table)
for _, v in pairs(path_table) do
ok, err = pcall(require, v)
if ok then
return err
end
end
error("Could not find a module to load from selected paths")
end
util = try_require({
"_extensions.jimjam-slam.my-extension.util",
"_extensions.my-extension.util"
}) |
Beta Was this translation helpful? Give feedback.
-
I created #12913 to track. |
Beta Was this translation helpful? Give feedback.
I created #12913 to track.