quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.6k stars 294 forks source link

Using meta shortcode in a grid table creates an extra column #10069

Open sguyader opened 4 weeks ago

sguyader commented 4 weeks ago

When I define the date-modified metadata in yaml header of quarto document, and use the {{< meta date-modified >}} in a grid table, an extra column is created to the right. It doesn't happen in regular markdown tables. It happens in several output formats I've tried (odt, docx, html, pdf).

Code to reproduce:

---
title: "Untitled"
date-modified: today
format: odt
---

This grid table with the date in plain text shows 2 columns as defined:

+-------------------+----------------------------+
| **Date created**  | 2021-07-12                 |
+-------------------+----------------------------+
| **Date modified** | 2024-06-19                 |
+-------------------+----------------------------+
: {tbl-colwidths="\[50,50\]"}

This grid table using the date-modified shortcode shows 3 columns instead of 2:

+-------------------+----------------------------+
| **Date created**  | 2021-07-12                 |
+-------------------+----------------------------+
| **Date modified** | {{< meta date-modified >}} |
+-------------------+----------------------------+
: {tbl-colwidths="\[50,50\]"}
sguyader commented 4 weeks ago

I forgot to mention that in RStudio visual editor, the table is displayed with 2 columns as expected, it's only during export that the table ends up with 3 columns.

cderv commented 4 weeks ago

@cscheid I believe this is due to how our internal reader works. This is the text that will be parsed by pandoc.read() after we have dealt with shortcode

+-------------------+----------------------------+
| **Date created**  | 2021-07-12                 |
+-------------------+----------------------------+
| **Date modified** | [[]{.quarto-shortcode__-param data-is-shortcode="1" data-value="meta" data-raw="meta"} []{.quarto-shortcode__-param data-is-shortcode="1" data-value="date-modified" data-raw="date-modified"} ]{.quarto-shortcode__ data-is-shortcode="1" data-raw="{{< meta date-modified >}}"} |
+-------------------+----------------------------+
: {tbl-colwidths="\[50,50\]"}

I believe the | and + alignement matters. With our replacement, they are no more aligned, and so grid tables is seen with 3 columns.

Simpler example with bar pandoc leading to three colgroups

❯ quarto pandoc --to html
+-------------------+----------------------------+
| **Date created**  | 2021-07-12                 |
+-------------------+----------------------------+
| **Date modified** | Very Very Very Very Very Long Content |
+-------------------+----------------------------+
^Z
<table style="width:83%;">
<colgroup>
<col style="width: 27%" />
<col style="width: 40%" />
<col style="width: 15%" />
</colgroup>
<tbody>
<tr class="odd">
<td><strong>Date created</strong></td>
<td>2021-07-12</td>
<td></td>
</tr>
<tr class="even">
<td><strong>Date modified</strong></td>
<td colspan="2">Very Very Very Very Very Long Content</td>
</tr>
</tbody>
</table>

So using shortcode in grid table is currently limited I believe

cscheid commented 4 weeks ago

Yes, unfortunately shortcodes are fundamentally broken with grid tables, and that's a bug we won't be able to easily fix in the near term.

We should add something to the documentation about it.

sguyader commented 3 weeks ago

Okay I understand. The workaround I found in my case is to set the width of the third column to zero, so at least it's not visible in the output document. For instance:

---
title: "Untitled"
date-modified: today
format: odt
---

+-------------------+----------------------------+
| **Date created**  | 2021-07-12                 |
+-------------------+----------------------------+
| **Date modified** | {{< meta date-modified >}} |
+-------------------+----------------------------+
: {tbl-colwidths="\[50,50,0\]"}
cscheid commented 3 weeks ago

@sguyader If you're comfortable writing filters, one (admittedly ugly) workaround you can use is something like this:

---
title: "Untitled"
date-modified: today
format: odt
filters:
  - transfer_placeholder.lua
---

+-------------------+-------------------------------+
| **Date created**  | 2021-07-12                    |
+-------------------+-------------------------------+
| **Date modified** | []{#placeholder-span-dest}    |
+-------------------+-------------------------------+
: {tbl-colwidths="\[50,50,0\]"}

[{{< meta date-modified >}}]{#placeholder-span-source}

And your transfer_placeholder.lua filter would have two steps:

It's a lot of work, but if you absolutely need the table to be formatted properly, this should get you unblocked.

sguyader commented 3 weeks ago

And your transfer_placeholder.lua filter would have two steps:

* the first step would find the `placeholder-span-source` span that contains the content of the shortcode you're expanding, store it in some local state

* the second step would find the `placeholder-span-dest` span and place the stored state there.

It's a lot of work, but if you absolutely need the table to be formatted properly, this should get you unblocked.

Thanks for the idea, but I don't know LUA so I'm afraid it's beyond my capabilities.