本文介绍了Pandoc:HTML到Markdown -我可以使用模板或脚本替换元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将HTML转换为Markdown,但诸如<span class="cmd">之类的元素已保留并出现在MD结果中.

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

在从HTML到Markdown的转换过程中,是否可以通过使用模板或Pandoc脚本将<span>元素替换为<strong>甚至使用星号?

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?

例如:

我要替换

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

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

*This content must be bold*

非常感谢.

推荐答案

您可以修改此pandoc过滤器.将其另存为cmd_italics.py并运行pandoc myfile.html -o myfile.md -F cmd_italics.py

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)

您将需要安装 pandocfilter python库.

You will need the pandocfilter python library installed.

这篇关于Pandoc:HTML到Markdown -我可以使用模板或脚本替换元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 09:51