jgm / pandoc

Universal markup converter
https://pandoc.org
Other
33.21k stars 3.3k forks source link

Code blocks in rST tables incorrectly wrapped #4644

Open NickVolynkin opened 6 years ago

NickVolynkin commented 6 years ago

Code blocks in table cells, having lines of more than about 30 characters, get incorrectly wrapped when writing rST.

Take this source html:

<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  <tr>
    <td>Some text.</td>
    <td>
      <pre class="bash">pandoc -f html -t rst test.html -o result.rst</pre>
    </td>
  </tr>
</table>

Or the same in Pandoc native format:

[Table [] [AlignDefault,AlignDefault] [0.5,0.5]
 [[Plain [Str "Header",Space,Str "1"]]
 ,[Plain [Str "Header",Space,Str "2"]]]
 [[[Plain [Str "Some",Space,Str "text."]]
  ,[CodeBlock ("",["bash"],[]) "pandoc -f html -t rst test.html -o result.rst"]]]]

Pandoc writes this as the following rST:

+-----------------------------------+-----------------------------------+
| Header 1                          | Header 2                          |
+===================================+===================================+
| Some text.                        | .. code:: bash                    |
|                                   |                                   |
|                                   |     pandoc -f html -t rst test.ht |
|                                   | ml -o result.rst                  |
+-----------------------------------+-----------------------------------+

Note the wrapped line ml -o result.rst. It's indented less than the code, so it becomes plain text. But even if it had the same indentation, still it would be wrong to voluntarily wrap a line of code. This rST is read to the following native data:

[Table [] [AlignDefault,AlignDefault] [0.5,0.5]
 [[Plain [Str "Header",Space,Str "1"]]
 ,[Plain [Str "Header",Space,Str "2"]]]
 [[[Plain [Str "Some",Space,Str "text."]]
  ,[CodeBlock ("",["sourceCode","bash"],[]) "pandoc -f html -t rst test.ht"
   ,Plain [Str "ml",Space,Str "-o",Space,Str "result.rst"]]]]]

And then written to this HTML (I've indented it for readability):

<table>
  <colgroup>
    <col style="width: 50%"/>
    <col style="width: 50%"/>
  </colgroup>
  <thead>
  <tr class="header">
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  </thead>
  <tbody>
  <tr class="odd">
    <td>Some text.</td>
    <td>
      <div class="sourceCode" id="cb1">
        <pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1"
                                                                      data-line-number="1"><span
            class="ex">pandoc</span> -f html -t rst test.ht</a></code></pre>
      </div>
      ml -o result.rst
    </td>
  </tr>
  </tbody>
</table>

Just to make sure that wrapping lines of code is wrong syntax, here's how Sphinx renders the same rST table:

sphinx

This can be solved by writing native tables to list-tables in rST, as proposed in #4564. In list-tables, lines can be of any length and table cells can have content with complex formatting: code blocks, lists and even other tables.


pandoc -v
pandoc 2.1.3
Compiled with pandoc-types 1.17.4.2, texmath 0.10.1.1, skylighting 0.7.1
jgm commented 6 years ago

This is similar to #4572 and #4320. It's not clear that this is a bug.

[Table [] [AlignDefault,AlignDefault] [0.5,0.5]

specifies that the table has two columns, each 50% of the text width. Pandoc respects that. The only way to respect that constraint is to break the code block, which is simply too wide to fit in a 50% wide column.

You could fix this by changing the widths, e.g.

[Table [] [AlignDefault,AlignDefault] [0.2,0.8]

One possible solution to all of these problems would be to increase the widths of cells arbitrarily to fit the content without bad line breaks (that is proposed in #4320). Maybe that's the thing to do, but I'm not sure. Whether we make that change or not, something will be broken in the output. Either the code will be badly wrapped, or the table will extend beyond the specified column width. Either of these calls for manual intervention.

NickVolynkin commented 6 years ago

@jgm do the widths of columns in the ascii-drawn rST table represent anything in rST syntax? Looks like there's the widths parameter for that:

.. table:: Name
   :widths: 50, 50

   +----...
   | ...

If a table in native Pandoc format has column widths defined, shoud not they be used in the widths parameter, but not literally as widths of columns in rST source? I think so, because I believe that people usually read rST sources only when they edit those sources, and most readers use HTML or PDF outputs, produced by Pandoc or Sphinx. But maybe I'm wrong.

jgm commented 6 years ago

You might be right. We use the same function to create grid tables in rst and markdown. I had thought that the widths were significant in both, but it may be that they're only significant in (pandoc) markdown. Need to do some experiments I guess.

ALso, forgot to mention this before, but the tables behave differently if you select --wrap=none. Then you get:

+------------+--------------------------------------------------+
| Header 1   | Header 2                                         |
+============+==================================================+
| Some text. | .. code:: bash                                   |
|            |                                                  |
|            |    pandoc -f html -t rst test.html -o result.rst |
+------------+--------------------------------------------------+
NickVolynkin commented 6 years ago

@jgm should I raise another issue about column widths? I could add some examples there.

jgm commented 6 years ago

I've determined by experimenting with rst2html.py that the actual widths of cells in grid tables are significant in the way pandoc takes them to be; they affect the widths in the HTML tables produced. However, this can be overridden with the :widths: in an explicit .. table: directive.

rprots commented 6 years ago

https://rackerlabs.github.io/docs-rackspace/tools/table-conversion.html

danse commented 6 years ago

this is the same problem we tackled with #4320, right? Anything we want to improve on that solution? The idea of list tables is interesting and we can discuss it on the related issue

albert2126 commented 1 year ago

One more example that works fine:

.. list-table::
   :widths: 1 1
   :header-rows: 1

   *  -  Block
      -  Example

   *  -  Execution block
      -  .. code-block:: ruby

            config.vm.define "node-1" do |node|
               node.vm.provision "shell",
            end