sergiocorreia / panflute

An Pythonic alternative to John MacFarlane's pandocfilters, with extra helper functions
http://scorreia.com/software/panflute/
BSD 3-Clause "New" or "Revised" License
500 stars 59 forks source link

Issue with handling `{=comment}` syntax in Markdown files #247

Open TomBener opened 6 months ago

TomBener commented 6 months ago

I've encountered an issue when using the {=comment} syntax in Markdown files processed with the Panflute.

Here's the command I'm using:

pandoc -C test.md -o test.html --filter sort-cnbib.py

The sort-cnbib.py script uses panflute to sort Chinese bibliography entries by their Pinyin representation:

sort-cnbib.py ```py import panflute as pf from pypinyin import pinyin, Style def contains_chinese(text): return any('\u4e00' <= char <= '\u9fff' for char in text) def special_pinyin(text): # 多音字的姓氏拼音 surname_map = { '葛': 'ge3', '阚': 'kan4', '任': 'ren2', '单': 'shan4', '解': 'xie4', '燕': 'yan1', '尉': 'yu4', '乐': 'yue4', '曾': 'zeng1', '查': 'zha1', } if contains_chinese(text): name = text.split(",")[0] if "," in text else text surname = name[0] return surname_map.get(surname, "".join([i[0] for i in pinyin(name, style=Style.TONE3)])) else: return None def prepare(doc): doc.chinese_entries = [] doc.non_chinese_entries = [] def action(elem, doc): if isinstance(elem, pf.Div) and "references" in elem.classes: for e in elem.content: if isinstance(e, pf.Div) and "csl-entry" in e.classes: entry_text = pf.stringify(e) if contains_chinese(entry_text): doc.chinese_entries.append(e) else: doc.non_chinese_entries.append(e) elem.content = [] def finalize(doc): doc.chinese_entries.sort(key=lambda x: special_pinyin(pf.stringify(x))) # Replace the Div container content with sorted entries for elem in doc.content: if isinstance(elem, pf.Div) and "references" in elem.classes: # Sort the Chinese bibliography entries by Pinyin and append them to the end of the non-Chinese entries # Swap the entries below can change the order of Chinese and non-Chinese bibliography entries elem.content = doc.non_chinese_entries + doc.chinese_entries break def main(doc=None): return pf.run_filter(action, prepare=prepare, finalize=finalize, doc=doc) if __name__ == '__main__': main() ```

When I include a {=comment} block in my Markdown file:

```{=comment}
This is a comment block.

 I get the following error:

TypeError: element str not in group {'vimwiki', 'docbook', 'openxml', 'odt', 'markdown', 'opml', 'html', 'twiki', 'native', 'tikiwiki', 'rst', 'jats', 'tex', 'org', 'gfm', 'ipynb', 'muse', 'fb2', 'context', 'latex', 't2t', 'man', 'icml', 'markdown_phpextra', 'markdown_mmd', 'markdown_strict', 'epub', 'textile', 'haddock', 'markdown_github', 'creole', 'noteref', 'dokuwiki', 'rtf', 'commonmark', 'json', 'docx', 'opendocument', 'mediawiki'}



If I remove the `{=comment}` block, the command runs without any issues.

It seems like Panflute is having trouble handling the `{=comment}` syntax. I would like to use this syntax for comments in my Markdown files, so it would be great if Panflute could support it.

Related: Three methods to add a [comment](https://fosstodon.org/@pandoc/109529170364166808) to Pandoc Markdown texts.

Thank you for your help!
NMarkgraf commented 3 months ago

In elements.py line 1249f you should add "comment" in the list. This should fix the ={comment} issue.