可以将多个段落替换为reStructuredText表单元格吗?

时间:2019-02-18 03:56:24

标签: python-sphinx restructuredtext

我有一个reST格式的表,其中某些单元格包含长文本块(即多个段落或项目符号列表),并且它们会定期更新,例如:

+-------+-----------------------+
|Cat    | Chunk that is updated |
|       | periodically.         |
|       |                       |
|       | Line #2, #3, etc      |
+-------+-----------------------+
|Dog    | Substitution means    |
|       | table boilplat static |
|       |                       |
|       | Line #2, #3, etc      |
+-------+-----------------------+

为了避免每次更新都维护表样板,我首先尝试了substitution,但这仅适用于内联指令(如图像)和单行文本...不适用于多个段落:

.. |cellb1| replace:: Chunk that is updated periodically via copy-and-paste

.. |cellb3| replace:: Substitution means table boilerplate can remain static 

+-------+-----------------------+
|Cat    | |cellb1|              |
+-------+-----------------------+
|Dog    | |cellb3|              |
+-------+-----------------------+

第二种方法是.. include::指令。对于多个段落来说,这很好用,但是却以复杂性为代价(现在,内容已在多个外部文本文件中错位)。

+-------+-----------------------+
|Cat    | .. include:: xr1.txt  |
+-------+-----------------------+
|Dog    | .. include:: xr3.txt  |
+-------+-----------------------+

list-table无济于事,csv-table也无济于事(因为内容包括逗号和引号字符)。

.. csv-table:: Test csv-table with multi-paragraph
   :header: "a", "b", "c"

    Cat,"Chunk that is updated periodically via copy-and-paste.

    Line #2, #3, etc", "Kitten"
    Dog,"Substitution means table boilerplate can remain static.

    Line #2, #3, etc", "Puppy"

NB:附带csv-table的副标题出现,缩进和列数影响Sphinx解析器,因此内容被格式化为块引用或定义列表。 [编辑:请参见下面的答案-由于:header:行的缩进不一致]

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

以下示例适用于我。请注意,空格非常棘手。在缩进任何内容时,我总是使用4个空格,包括诸如:header:之类的选项,因为它可以更轻松地检测问题,并确保Sphinx正确解释了空格。在您的示例中,您需要做的就是在:header:之前再添加一个空格,总共4个空格,以使所有内容对齐。我还添加了一个枚举列表的示例。

.. csv-table:: Test csv-table with multi-paragraph
    :header: "a", "b", "c"

    Cat,"Chunk that is updated periodically via copy-and-paste.

    Line #2, #3, etc", "Kitten"
    Dog,"Substitution means table boilerplate can remain static.

    Line #2, #3, etc", "Puppy"
    Walrus,"#.  List Item 1
    #.  List Item 2
    #.  List Item 3", "Pup"