panflute
panflute copied to clipboard
replace_keyword not compatible with \bibliography
Here is a minimal working sample, for a latex document input (main.bib
doesn't have to present):
\begin{document}
foo
\bibliography{main}
\end{document}
with a finalize
filter calling replace_keyword
replacing with a block
def f(doc):
doc = doc.replace_keyword('foo', pf.Para())
def nan(elem, doc):
pass
def main(doc=None):
return pf.run_filters([nan], finalize=f, doc=doc)
The pre-finalized generated AST looks like
Pandoc (Meta {unMeta = fromList [("bibliography",MetaList [MetaInlines [Str "main.bib"]])]})
[Para [Str "foo"]]
The problem seems to originate from the code base panflute/tools.py
line 493
def replace_with_block(e, doc):
if hasattr(e, 'content') and len(e.content) == 1:
ee = e.content[0]
where in this case, e.content
could be a DictContainer('bibliography')
and fails to retreive e.content[0]
.
It seems necessary to ensure e.content
to be a ListContainer
but I'm not sure if any other type is also allowed here.
Probably related to #101
@peterzjx more thna a year late, but fixed the bug but avoiding searching DictContainers
Reopening though as a better approach might be to walk looking for Str(keyword) and then flagging the parent object for latter replacement. Because .walk()
breaks if we replace the parent object, an alternative might be to instead flag it (add it to a set) and then do a second round where we replace the parent objects we flagged.