Skip to content
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

replace_keyword not compatible with \bibliography #104

Open
peterzjx opened this issue Jun 28, 2019 · 2 comments
Open

replace_keyword not compatible with \bibliography #104

peterzjx opened this issue Jun 28, 2019 · 2 comments

Comments

@peterzjx
Copy link

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
Copy link
Author

Probably related to #101

@sergiocorreia sergiocorreia added question todo Stuff I want to update/fix/improve labels Dec 2, 2019
@sergiocorreia sergiocorreia reopened this Nov 10, 2020
@sergiocorreia
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants