diff --git a/docs/spec/tables/csv-table-auto-keyword.rst.txt b/docs/spec/tables/csv-table-auto-keyword.rst.txt new file mode 100644 index 0000000..03de3b2 --- /dev/null +++ b/docs/spec/tables/csv-table-auto-keyword.rst.txt @@ -0,0 +1,8 @@ +.. csv-table:: Frozen Delights! + :header: "Treat", "Quantity", "Description" + :widths: auto + + "Albatross", 2.99, "On a stick!" + "Crunchy Frog", 1.49, "If we took the bones out, + it wouldn't be crunchy, now would it?" + "Gannet Ripple", 1.99, "On a stick!" diff --git a/docs/spec/tables/csv-table-auto-keyword.typ.txt b/docs/spec/tables/csv-table-auto-keyword.typ.txt new file mode 100644 index 0000000..1ba092e --- /dev/null +++ b/docs/spec/tables/csv-table-auto-keyword.typ.txt @@ -0,0 +1,21 @@ +#figure([ + #table( + columns: 3, + table.header( + [Treat], + [Quantity], + [Description], + ), + [Albatross], + [2.99], + [On a stick!], + [Crunchy Frog], + [1.49], + [If we took the bones out, + it wouldn't be crunchy, now would it?], + [Gannet Ripple], + [1.99], + [On a stick!], + )], + caption: [Frozen Delights!], +) diff --git a/docs/spec/tables/csv-table-autolayout.rst.txt b/docs/spec/tables/csv-table-autolayout.rst.txt new file mode 100644 index 0000000..821b39a --- /dev/null +++ b/docs/spec/tables/csv-table-autolayout.rst.txt @@ -0,0 +1,7 @@ +.. csv-table:: Frozen Delights! + :header: "Treat", "Quantity", "Description" + + "Albatross", 2.99, "On a stick!" + "Crunchy Frog", 1.49, "If we took the bones out, + it wouldn't be crunchy, now would it?" + "Gannet Ripple", 1.99, "On a stick!" diff --git a/docs/spec/tables/csv-table-autolayout.typ.txt b/docs/spec/tables/csv-table-autolayout.typ.txt new file mode 100644 index 0000000..1ba092e --- /dev/null +++ b/docs/spec/tables/csv-table-autolayout.typ.txt @@ -0,0 +1,21 @@ +#figure([ + #table( + columns: 3, + table.header( + [Treat], + [Quantity], + [Description], + ), + [Albatross], + [2.99], + [On a stick!], + [Crunchy Frog], + [1.49], + [If we took the bones out, + it wouldn't be crunchy, now would it?], + [Gannet Ripple], + [1.99], + [On a stick!], + )], + caption: [Frozen Delights!], +) diff --git a/docs/spec/tables/grid-table-with-widths.rst.txt b/docs/spec/tables/grid-table-with-widths.rst.txt new file mode 100644 index 0000000..7fa8687 --- /dev/null +++ b/docs/spec/tables/grid-table-with-widths.rst.txt @@ -0,0 +1,14 @@ +.. table:: Truth table with grid-derived widths + :widths: grid + + +-------+-------+-----------+ + | A | B | A and B | + +=======+=======+===========+ + | False | False | False | + +-------+-------+-----------+ + | True | False | False | + +-------+-------+-----------+ + | False | True | False | + +-------+-------+-----------+ + | True | True | True | + +-------+-------+-----------+ diff --git a/docs/spec/tables/grid-table-with-widths.typ.txt b/docs/spec/tables/grid-table-with-widths.typ.txt new file mode 100644 index 0000000..1060a40 --- /dev/null +++ b/docs/spec/tables/grid-table-with-widths.typ.txt @@ -0,0 +1,23 @@ +#figure([ + #table( + columns: (7fr, 7fr, 11fr), + table.header( + [A], + [B], + [A and B], + ), + [False], + [False], + [False], + [True], + [False], + [False], + [False], + [True], + [False], + [True], + [True], + [True], + )], + caption: [Truth table with grid-derived widths], +) diff --git a/docs/spec/tables/grid-table.typ.txt b/docs/spec/tables/grid-table.typ.txt index 2f8eee0..1bca093 100644 --- a/docs/spec/tables/grid-table.typ.txt +++ b/docs/spec/tables/grid-table.typ.txt @@ -1,5 +1,5 @@ #table( - columns: (24fr, 12fr, 10fr, 10fr), + columns: 4, table.header( [Header row, column 1 (header rows optional)], diff --git a/docs/spec/tables/index.rst b/docs/spec/tables/index.rst index e62a899..6b6e8cf 100644 --- a/docs/spec/tables/index.rst +++ b/docs/spec/tables/index.rst @@ -56,6 +56,72 @@ List table .. literalinclude:: list-table.typ.txt :language: typst +List table with autolayout +-------------------------- + +.. tab-set-code:: + + .. literalinclude:: list-table-autolayout.rst.txt + :language: rst + + .. literalinclude:: list-table-autolayout.typ.txt + :language: typst + +CSV table with autolayout +------------------------- + +.. tab-set-code:: + + .. literalinclude:: csv-table-autolayout.rst.txt + :language: rst + + .. literalinclude:: csv-table-autolayout.typ.txt + :language: typst + +CSV table with "auto" keyword +----------------------------- + +.. tab-set-code:: + + .. literalinclude:: csv-table-auto-keyword.rst.txt + :language: rst + + .. literalinclude:: csv-table-auto-keyword.typ.txt + :language: typst + +List table with "auto" keyword +------------------------------ + +.. tab-set-code:: + + .. literalinclude:: list-table-auto-keyword.rst.txt + :language: rst + + .. literalinclude:: list-table-auto-keyword.typ.txt + :language: typst + +Simple table with explicit widths +--------------------------------- + +.. tab-set-code:: + + .. literalinclude:: simple-table-with-widths.rst.txt + :language: rst + + .. literalinclude:: simple-table-with-widths.typ.txt + :language: typst + +Grid table with "grid" widths keyword +------------------------------------- + +.. tab-set-code:: + + .. literalinclude:: grid-table-with-widths.rst.txt + :language: rst + + .. literalinclude:: grid-table-with-widths.typ.txt + :language: typst + References ========== diff --git a/docs/spec/tables/list-table-auto-keyword.rst.txt b/docs/spec/tables/list-table-auto-keyword.rst.txt new file mode 100644 index 0000000..5d9f610 --- /dev/null +++ b/docs/spec/tables/list-table-auto-keyword.rst.txt @@ -0,0 +1,17 @@ +.. list-table:: Frozen Delights! + :widths: auto + :header-rows: 1 + + * - Treat + - Quantity + - Description + * - Albatross + - 2.99 + - On a stick! + * - Crunchy Frog + - 1.49 + - If we took the bones out, it wouldn't be + crunchy, now would it? + * - Gannet Ripple + - 1.99 + - On a stick! diff --git a/docs/spec/tables/list-table-auto-keyword.typ.txt b/docs/spec/tables/list-table-auto-keyword.typ.txt new file mode 100644 index 0000000..7c66551 --- /dev/null +++ b/docs/spec/tables/list-table-auto-keyword.typ.txt @@ -0,0 +1,21 @@ +#figure([ + #table( + columns: 3, + table.header( + [Treat], + [Quantity], + [Description], + ), + [Albatross], + [2.99], + [On a stick!], + [Crunchy Frog], + [1.49], + [If we took the bones out, it wouldn't be + crunchy, now would it?], + [Gannet Ripple], + [1.99], + [On a stick!], + )], + caption: [Frozen Delights!], +) diff --git a/docs/spec/tables/list-table-autolayout.rst.txt b/docs/spec/tables/list-table-autolayout.rst.txt new file mode 100644 index 0000000..fbd4e3d --- /dev/null +++ b/docs/spec/tables/list-table-autolayout.rst.txt @@ -0,0 +1,16 @@ +.. list-table:: Frozen Delights! + :header-rows: 1 + + * - Treat + - Quantity + - Description + * - Albatross + - 2.99 + - On a stick! + * - Crunchy Frog + - 1.49 + - If we took the bones out, it wouldn't be + crunchy, now would it? + * - Gannet Ripple + - 1.99 + - On a stick! diff --git a/docs/spec/tables/list-table-autolayout.typ.txt b/docs/spec/tables/list-table-autolayout.typ.txt new file mode 100644 index 0000000..7c66551 --- /dev/null +++ b/docs/spec/tables/list-table-autolayout.typ.txt @@ -0,0 +1,21 @@ +#figure([ + #table( + columns: 3, + table.header( + [Treat], + [Quantity], + [Description], + ), + [Albatross], + [2.99], + [On a stick!], + [Crunchy Frog], + [1.49], + [If we took the bones out, it wouldn't be + crunchy, now would it?], + [Gannet Ripple], + [1.99], + [On a stick!], + )], + caption: [Frozen Delights!], +) diff --git a/docs/spec/tables/simple-table-with-widths.rst.txt b/docs/spec/tables/simple-table-with-widths.rst.txt new file mode 100644 index 0000000..b27495d --- /dev/null +++ b/docs/spec/tables/simple-table-with-widths.rst.txt @@ -0,0 +1,11 @@ +.. table:: Truth table with explicit widths + :widths: 10 10 15 + + ===== ===== ======= + A B A and B + ===== ===== ======= + False False False + True False False + False True False + True True True + ===== ===== ======= diff --git a/docs/spec/tables/simple-table-with-widths.typ.txt b/docs/spec/tables/simple-table-with-widths.typ.txt new file mode 100644 index 0000000..5722539 --- /dev/null +++ b/docs/spec/tables/simple-table-with-widths.typ.txt @@ -0,0 +1,23 @@ +#figure([ + #table( + columns: (10fr, 10fr, 15fr), + table.header( + [A], + [B], + [A and B], + ), + [False], + [False], + [False], + [True], + [False], + [False], + [False], + [True], + [False], + [True], + [True], + [True], + )], + caption: [Truth table with explicit widths], +) diff --git a/docs/spec/tables/simple-table.typ.txt b/docs/spec/tables/simple-table.typ.txt index af60978..c7dd537 100644 --- a/docs/spec/tables/simple-table.typ.txt +++ b/docs/spec/tables/simple-table.typ.txt @@ -1,5 +1,5 @@ #table( - columns: (5fr, 5fr, 7fr), + columns: 3, table.header( [A], [B], diff --git a/src/rst2typst/writer.py b/src/rst2typst/writer.py index 9db0a85..f21bf7c 100644 --- a/src/rst2typst/writer.py +++ b/src/rst2typst/writer.py @@ -540,11 +540,19 @@ def depart_table(self, node: nodes.table): self.body.append("\n") def visit_tgroup(self, node: nodes.tgroup): - cols = [ - f"{colspec['colwidth']}fr" if colspec != "auto" else "auto" - for colspec in node.findall(nodes.colspec) - ] - self.body.append(f"{self._hi.indent}columns: ({', '.join(cols)}),\n") + table_node = node.parent + colwidths_given = "colwidths-given" in table_node.get("classes", []) + + if colwidths_given: + # Use explicit fractional widths when the author specified them. + cols = [ + f"{colspec['colwidth']}fr" for colspec in node.findall(nodes.colspec) + ] + self.body.append(f"{self._hi.indent}columns: ({', '.join(cols)}),\n") + else: + # Typst defaults to columns: 1, so we must always emit the column + # count; the integer shorthand sizes all columns automatically. + self.body.append(f"{self._hi.indent}columns: {node['cols']},\n") def depart_tgroup(self, node: nodes.tgroup): pass