Search code examples
html-tablepython-sphinxline-breaksrestructuredtext

Preserve table cell line breaks in Sphinx processing of reStructuredText


I have a reStructuredText table with a row like this:

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

The cell when produced by Sphinx (HTML as example) is this:

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

What I would like to be produced is this (or similar), where a line break is forced at least before every new line:

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

Is there a way I can configure Sphinx to respect the lines in a reStructuredText table cell?

(For reference, here is the whole table as currently produced:)

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

Solution

  • Generally there are two easy ways to guarantee a line break or alignment in reST.

    1. Using Paragraphs, the following:

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

    Will give:

    <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. Using Line Blocks, the following:

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

    Will give:

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

    The resulting <div class="line"></div> will work like a paragraph and also keep alignment. This is guaranteed by the reST specification, so even if your output is not HTML there should be mechanisms in place to guarantee the result will be consistent.