在 reStructuredText 的 Sphinx 处理中保留表格单元格换行符

时间:2021-01-18 09:28:47

标签: html-table python-sphinx line-breaks restructuredtext

我有一个 reStructuredText 表,其中一行是这样的:

+------+-----------------------------+
| Mask | The bit mask:               |
|      | [bit 0] Description of bit0 |
|      | [bit 1] And bit1            |
+------+-----------------------------+

Sphinx 生成的单元格(以 HTML 为例)是这样的:

<td><p>The bit mask:
[bit 0] Description of bit0
[bit 1] And bit1</p></td>

我想要产生的是这个(或类似的),其中至少在每个新行之前强制换行:

<td><p>The bit mask:
<br>[bit 0] Description of bit0
<br>[bit 1] And bit1</p></td>

有没有办法配置 Sphinx 以尊重 reStructuredText 表格单元格中的行?

(作为参考,这里是当前制作的整个表格:)

<table class="docutils align-default">
   <colgroup>
      <col style="width: 17%" />
      <col style="width: 83%" />
   </colgroup>
   <tbody>
      <tr class="row-odd">
         <td>
            <p>Mask</p>
         </td>
         <td>
            <p>The bit mask:
               [bit 0] Description of bit0
               [bit 1] And bit1
            </p>
         </td>
      </tr>
   </tbody>
</table>

1 个答案:

答案 0 :(得分:2)

通常有两种简单的方法可以保证 reST 中的换行或对齐。

1. 使用 Paragraphs,如下:

let rp = require('request-promise')

function main(params) {
    if (params.actionA == 'joke') {
        const options = {
            uri: "http://api.icndb.com/jokes/random",
            json: true
        }
        return rp(options)
            .then(res => {
                return { response: res }
            })
    } else if (params.actionB == 'fact') {
        const options = {
            "method": "GET",
            "hostname": "healthruwords.p.rapidapi.com",
            "port": null,
            "uri": "/v1/quotes/?id=731&t=Wisdom&maxR=1&size=medium",
            "headers": {
                "x-rapidapi-host": "healthruwords.p.rapidapi.com",
                "x-rapidapi-key": params.apiKey,
                "useQueryString": true
            }
        }
        return rp(options)
            .then(res => {
                return { response: res }
            })
    }
} 

会给:

+------+-----------------------------+
| Mask | The bit mask:               |
|      |                             |
|      | [bit 0] Description of bit0 |
|      |                             |
|      | [bit 1] And bit1            |
|      |                             |
+------+-----------------------------+

2. 使用 Line Blocks,如下:

<table class="docutils align-default">
   <tbody>
      <tr class="row-odd">
         <td>
            <p>Mask</p>
         </td>
         <td>
            <p>The bit mask:</p>
            <p>[bit 0] Description of bit0</p>
            <p>[bit 1] And bit1</p>
         </td>
      </tr>
   </tbody>
</table>

会给:

+------+-------------------------------+
| Mask | | The bit mask:               |
|      | | [bit 0] Description of bit0 |
|      | | [bit 1] And bit1            |
+------+-------------------------------+

生成的 </table> <tbody> <tr class="row-odd"> <td> <p>Mask</p> </td> <td> <div class="line-block"> <div class="line">The bit mask:</div> <div class="line">[bit 0] Description of bit0</div> <div class="line">[bit 1] And bit1</div> </div> </td> </tr> </tbody> </table> 将像一个段落一样工作并保持对齐。这是由 reST 规范保证的,因此即使您的输出不是 HTML,也应该有适当的机制来保证结果的一致性。

相关问题