问题
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