Pandoc: HTML-to-Markdown--can I replace elements using templates or scripts?

风格不统一 提交于 2019-12-11 01:46:50

问题


I'm successfully converting HTML to Markdown, but elements such as <span class="cmd"> are preserved and appear in the MD result.

Is there a way, perhaps by using templates or Pandoc scripting, to replace the <span> element with <strong> or even with asterisks during the HTML-to-Markdown conversion?

For example:

I want to replace

<span class="cmd">This content must be bold</span>

with

<strong>This content must be bold</strong>

or

*This content must be bold*

Thanks very much.


回答1:


You could adapt this pandoc filter. Save this as cmd_italics.py and run pandoc myfile.html -o myfile.md -F cmd_italics.py

#!/usr/bin/env python

from pandocfilters import toJSONFilter, Strong


def cmd_italics(key, value, format, meta):
    if key == 'Span':
        [[ident, classes, kvs], contents] = value
        for c in classes:
            if c == "cmd":
                return Strong(contents)

if __name__ == "__main__":
    toJSONFilter(cmd_italics)

You will need the pandocfilter python library installed.



来源:https://stackoverflow.com/questions/34817960/pandoc-html-to-markdown-can-i-replace-elements-using-templates-or-scripts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!