-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lua function pandoc.read
seems to ignore ReaderOptions.extensions
#10593
Comments
@tarleb would know what is happening here. |
This is indeed ignored, and I seem to remember a discussion about this behavior, but forgot the exact context. I'll try to find a link. The main issue is that we assume that the format specifier is the authoritative source of reader extensions, so the ReaderOptions field gets overridden. |
It seems like I was wrong, I can't find any discussion. The current behavior basically happened because the reader options parameter was added later, and the only obvious, backwards-compatible method was to introduce it with the current behavior. Related: #9587 |
So putting |
I can confirm that local doc = pandoc.read('# Header {.class key=val}', 'markdown-auto_identifiers', reader_options) has the desired effect. I have written some filters where I convert parts of the AST back to pandoc's Markdown, perform some string manipulations, and then convert it back to an AST, like so (I would assume that this is a fairly common theme): local writer_opts = pandoc.WriterOptions({extensions = PANDOC_READER_OPTIONS.extensions})
local md = pandoc.write(pandoc.Pandoc(blocks), 'markdown', writer_opts)
-- Manipulate md.
-- Then convert it back to an AST:
local ast_fragment = pandoc.read(md, 'markdown', PANDOC_READER_OPTIONS).blocks To do this accurately (so that one ends up with the same markdown as in the source file, or at least as close as possible), one needs to consider the markdown extensions that were applied when the document was parsed, as is done in the code above. Of course, this doesn't work if those extensions are ignored by the May I therefore propose the following alternative behaviour (which relies on the
Please let me know what you think. |
Another option might be that |
This should give the desired effect: local extensions = PANDOC_READER_OPTIONS.extensions
local flavored_format = { format = 'markdown', extensions = extensions }
local md = pandoc.write(pandoc.Pandoc(blocks), flavored_format)
-- Manipulate md.
-- Then convert it back to an AST:
local ast_fragment = pandoc.read(md, flavored_format).blocks I agree that the current situation is not really satisfying. Some kind of warning would be good. |
I'm not happy with either of the options for when to issue a warning, as all of them might lead to warnings in perfectly fine code. For now I'm just going to document the current behavior a bit better. @jgm, if you have any preferences for how these options should interact, then I'd be most happy to implement that. |
Perhaps a function that takes a format and a list of extensions and outputs something like |
Thank you for that Albert, I didn't realise that you could pass a table instead of the format string as the second argument - I should've done a better job reading the documentation! @jgm, that addresses the use case I presented above, so I don't think there's a need for the kind of function you have described. Regarding the behaviour of
I think this behaviour would be closest to what the user expects these functions to do. |
Partially off topic: I wanted to see if it's possible/easy to write a Lua function to stringify a format with extensions, and here's the result: local function flavored_format_spec (format_name, format_extensions_list)
local supported_exts = pandoc.format.extensions(format_name)
local supported_extensions_list = pandoc.List:new(pairs(supported_exts))
supported_extensions_list:sort() -- sort list so we get deterministic results
local results = {format_name}
for _, ext in ipairs(supported_extensions_list) do
if supported_exts[ext] ~= format_extensions_list:includes(ext) then
results[#results + 1] = (supported_exts[ext] and '-' or '+') .. ext
end
end
return table.concat(results)
end |
@tarleb why is it |
The input is a format name and the list of enabled extensions, e.g. |
Explain the problem.
When running the following Lua filter (
test.lua
)with the following command (where
test.md
is any document)pandoc --from=markdown --to=native test.md --lua-filter=test.lua > /dev/null
, the following text is printed to
stderr
:It seems as if removing the
auto_identifiers
extension fromreader_options.extensions
has no effect and the header is automatically assigned an Id regardless. The expectedstderr
output isPandoc version?
pandoc 3.6.2
Features: +server +lua
Scripting engine: Lua 5.4
OS: Linux
The text was updated successfully, but these errors were encountered: