Is there a way to detect the output format in a JSON filter? #5491
Replies: 4 comments 3 replies
-
No, and that's one of the fundamental problems of a JSON filter - lack of access to the pandoc and quarto APIs. You could perhaps write two filters, one Lua filter that injects information into the metadata using the API, and then the JSON filter has access to it. |
Beta Was this translation helpful? Give feedback.
-
I guess this is a common case. Would y'all consider putting this into the JSON by default? Or providing a filter to do that? |
Beta Was this translation helpful? Give feedback.
-
Er - yes - I wasn't asking for every possible value from the API, just the |
Beta Was this translation helpful? Give feedback.
-
My current solution is the following little filter --[[
This filter adds metadata to the document.
This allows us to add useful information visible to Pandoc and
Quarto, but not available in a Python / Panflute filter.
See: https://quarto.org/docs/extensions/lua-api.html
]]
return {{
Meta = function (meta)
for _, fmt in ipairs({'html', 'latex',
'docx', 'markdown'}) do
if quarto.doc.is_format(fmt) then
out_format = fmt
break
end
end
meta['quarto-doc-params'] = {
output_directory = quarto.project.output_directory,
input_file = quarto.doc.input_file,
output_file = quarto.doc.output_file,
out_format = out_format
}
return meta
end
}} I'm sure it could be improved, but it has the desired effect, of filling in a new key in the JSON metadata like this: "quarto-doc-params": {
"input_file": "/Users/mb312/dev_trees/resamp-test/source/index.Rmd",
"out_format": "html",
"output_directory": "/Users/mb312/dev_trees/resamp-test/source/../r-book",
"output_file": "/Users/mb312/dev_trees/resamp-test/source/../r-book/index.html"
}, |
Beta Was this translation helpful? Give feedback.
-
I have a series of JSON filters in my book that pull out information from the documents, in fact to generate an
.ipynb
notebook from the pages.However, I don't want to execute these twice when outputing HTML and PDF documents with
quarto render .
.Is there a way for my JSON filter to know which output format it is currently rendering, so I can ensure the particular filter only runs for e.g. the HTML rendering? I see a Lua filter can ask e.g.
quarto.doc.is_format("latex")
, but is there an equivalent within a JSON filter?Beta Was this translation helpful? Give feedback.
All reactions