【问题标题】:How to use docutils to create HTML from reStructuredText?如何使用 docutils 从 reStructuredText 创建 HTML?
【发布时间】:2018-01-31 09:46:25
【问题描述】:

我已经编写了简单的程序来将 reStructuredText 转换为 html

from docutils.core import publish_string

input_string = ("Heading\n"
                "=======\n"
                "\n"
                "1. With 24 widgets pull a **long** one;\n"
                "2. with fewer, push a **wide** one.\n")

html = publish_string(input_string)
print(html)

但是输出是:

<document ids="heading" names="heading" source="<string>" title="Heading">
    <title>
        Heading
    <enumerated_list enumtype="arabic" prefix="" suffix=".">
        <list_item>
            <paragraph>
                With 24 widgets pull a 
                <strong>
                    long
                 one;
        <list_item>
            <paragraph>
                with fewer, push a 
                <strong>
                    wide
                 one.

这显然是在尝试,但我是否缺少参数?我是否需要指定所需的转换,例如读取器、写入器还是解析器?

当我使用命令行运行时,它可以完美运行

rst2html.py <input file> <output file>

【问题讨论】:

    标签: html restructuredtext docutils


    【解决方案1】:

    我想我已经在问题中找到了答案:(

    我需要 writer_name 参数

    from docutils.core import publish_string
    
    input_string = ("Heading\n"
                    "=======\n"
                    "\n"
                    "1. With 24 widgets pull a **long** one;\n"
                    "2. with fewer, push a **wide** one.\n")
    
    html = publish_string(input_string, writer_name='html')
    
    print(html)
    

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2011-07-29
      相关资源
      最近更新 更多