【问题标题】:Preserve table cell line breaks in Sphinx processing of reStructuredText在 reStructuredText 的 Sphinx 处理中保留表格单元格换行符
【发布时间】:2021-01-18 09:28:47
【问题描述】:

我有一个 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>

【问题讨论】:

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


    【解决方案1】:

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

    1.使用Paragraphs,如下:

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

    将给予:

    <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>
    

    2.使用Line Blocks,如下:

    +------+-------------------------------+
    | 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>
    

    生成的&lt;div class="line"&gt;&lt;/div&gt; 将像一个段落一样工作并且保持对齐。这是由 reST 规范保证的,因此即使您的输出不是 HTML,也应该有适当的机制来保证结果的一致性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-10
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 2021-10-15
      相关资源
      最近更新 更多