简体   繁体   English

使用 Pandoc 从 HTML 转换为 Markdown 时为代码块添加正确的语法名称

[英]Add proper syntax name to code blocks when converting from HTML to Markdown with Pandoc

I need to convert some HTML to Markdown with Pandoc.我需要使用 Pandoc 将一些 HTML 转换为 Markdown。 All is fine except the code blocks in my document are not converted properly.一切都很好,只是我文档中的代码块没有正确转换。 I need them to appear in the resulting Markdown document as backtick-code blocks with syntax definition.我需要它们作为带有语法定义的反引号代码块出现在生成的 Markdown 文档中。

For example, if I have such source HTML:例如,如果我有这样的源 HTML:

<pre class="python"><code>
    def myfunc(param):
        '''Description of myfunc'''
        return do_something(param)
</code></pre>

I want Pandoc to convert it into:我希望 Pandoc 将其转换为:

```python
    def myfunc(param):
        '''Description of myfunc'''
        return do_something(param)
```

But what I am getting is:但我得到的是:

``` {.python}
    def myfunc(param):
        '''Description of myfunc'''
        return do_something(param)
```

It's almost there, but the syntax definition is in curly braces and with a dot, which is not recognised by my Markdown parser.它几乎就在那里,但语法定义在花括号和一个点中,我的 Markdown 解析器无法识别。 How can I get ```python instead of ``` {.python} when converting HTML to Markdown?将 HTML 转换为 Markdown 时,如何获得```python而不是``` {.python}

I have control over the source HTML, so I can change it the way needed.我可以控制源 HTML,因此我可以根据需要更改它。 If there's an option to insert "raw markdown" into the HTML which will be ignored by Pandoc, that would work for me too, I can embed those blocks into the source HTML the way I need, but I need to tell Pandoc not to touch them.如果有一个选项可以将“原始降价”插入到 HTML 中,这将被 Pandoc 忽略,这对我也有用,我可以按照我需要的方式将这些块嵌入到源 HTML 中,但我需要告诉 Pandoc 不要触摸他们。 But I can't find such option in the docs.但是我在文档中找不到这样的选项。

This behavior is governed by thefenced_code_attributes extension.此行为由fenced_code_attributes扩展控制。 It is enabled by default;默认开启; disabling it will give your desired output:禁用它会给出你想要的输出:

pandoc --to=markdown-fenced_code_attributes ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandoc:从markdown转换时添加onload等html事件 - pandoc: add html events such as onload when converting from markdown 使用Pandoc将HTML中的多行代码段转换为Markdown - Converting multiline code snippets in HTML to Markdown with pandoc 从 markdown 转换为 HTML 时,使用 Pandoc Lua 过滤器替换 HTML 标签 - Replace HTML tags using Pandoc Lua filter when converting from markdown to HTML Pandoc在转换为Markdown时会丢弃“未知” HTML元素 - Pandoc drops “unknown” HTML elements when converting to markdown 为什么pandoc在将html转换为markdown时会保留span和div标签? - Why pandoc keeps span and div tags when converting html to markdown? Pandoc:从 Markdown 转换为 HTML 时将字体系列更改为 sans - Pandoc: Change font family to sans while converting from Markdown to HTML 从 markdown 到 html 时包含一个带有 pandoc 的 javascript 文件 - Including a javascript file with pandoc when going from markdown to html Pandoc:将docx转换为markdown时有条件中断 - Pandoc: Conditional breaks when converting docx to markdown 使用Pandoc从HTML转换为Markdown时如何保留标识符 - How do I preserve identifiers while converting from HTML to Markdown with Pandoc 从 html 转换为 docx 格式时,Pandoc 不渲染 SVG 图像 - Pandoc is not rendering SVG images when converting from html to docx format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM