panflute icon indicating copy to clipboard operation
panflute copied to clipboard

replace_keyword not compatible with \bibliography

Open peterzjx opened this issue 5 years ago • 2 comments

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.

peterzjx avatar Jun 28 '19 18:06 peterzjx

Probably related to #101

peterzjx avatar Jun 28 '19 18:06 peterzjx

@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.

sergiocorreia avatar Nov 10 '20 02:11 sergiocorreia