如何使用docutils从reStructuredText创建HTML?

时间:2018-01-31 09:46:26

标签: html restructuredtext docutils

我编写了简单的程序来将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>

1 个答案:

答案 0 :(得分:0)

我想我在问题中得到了答案:(

我需要 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)