Lua - access metadata outside of Meta function #9373
Closed
dude-at-RA
started this conversation in
General
Replies: 3 comments 2 replies
-
For now, my workaround solution is to explicitly call ANY functions we have implemented in the filter in what I think is the default order (haven't gone to look at the pandoc source to verify): return {
{ Meta = Meta }, -- get meta vars first
{ Str = Str }, -- Inline elements
{ Inlines },
{ Para = Para, Div = Div, OrderedList = OrderedList, BulletList = BulletList}, -- Block Elements
{ Blocks = Blocks }
} This is okay as a temporary solution, but it requires upkeep as we add more functions - and I don't know how to handle a situation where the traversal might be dynamic. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Do you have one big filter that performs all the required transformations? If so, why not split it into multiple files, one for each task? I find it more maintainable.
That said, assuming that the metadata is in the first or only file you pass to pandoc as an argument, you can get its metadata anywhere in your filter with:
pandoc.read(io.open(PANDOC_STATE.input_files[1]):read('a')).meta
Indeed, you can have access to the whole document this way. An ugly, working hack.
A more robust way would be to store all your functions but Meta in a table of functions and to call first Meta, second Pandoc with the table set as the filter. Your file would look like this:
local function Meta(meta)
...
end
local my_filters = {
Str = function(str)
...
end,
Para = function(para)
...
end
}
local function Pandoc(doc)
return doc:walk(my_filters)
end
return { { Meta = Meta }, { Pandoc = Pandoc } }
This way, you will not have to think updating the return table as you change the functions in my_filters, and you don't have to bother with execution order.
|
Beta Was this translation helpful? Give feedback.
1 reply
-
For the final recommendation, does Pandoc reorder the execution of the
functions in my_filters based on the standard behavior? Or do I need to
put them in order in the declaration of my_filters to match the default
ordering (i.e. inlines before blocks, etc)?
Key-value tables are not ordered in Lua, so pandoc always calls the functions in the default order.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to enhance the lua variable substitution example and because of the complexity of our lua filters, I'd like to access the metadata in a different order than normal. Changing the order of execution for meta as described in the example
return {{ Meta = Meta, Str = Str}}
breaks our existing filters (as they are written assuming the standard pandoc order). AFAICT the only way to fix that is to explicitly list the functions in the order you want to execute, which means that any time we add a new filter for another type, we have to add that to our sequencing, which I'd like to avoid.So, big question:
Is there a way to access the metadata outside of the Meta function in lua?
I found issue #4957, which seems like it be close to what I wanted, but it looks like that approach doesn't work or has been removed, and it's not clear what the alternative is to it.
Thanks for the time!
Beta Was this translation helpful? Give feedback.
All reactions