Skip to main content

Support for colspan and rowspan in RST tables for docutils and Sphinx

Project description

reStructuredText provides four syntaxes for tables:

  • Grid tables

  • Simple tables

  • List tables

  • CSV tables

However, only grid and simple tables support cells spanning multiple columns, and only grid tables support cells spanning multiple rows.

The rst-table-span package adds the :colspan:`C`, :rowspan:`R`, and :cellspan:`CxR` roles which add support for column and row span to all existing RST table types.

This project was inspired by the flat-table directive from the LinuxDoc project.

Example

To produce a table like this:

Header row, column 1 (header rows optional)

Header 2

Header 3

Header 4

body row 1, column 1

column 2

column 3

column 4

body row 2

Cells may span columns.

body row 3

Cells may span rows.

Corner

body row 4

You would normally need to write this:

+------------------------+------------+----------+----------+
| Header row, column 1   | Header 2   | Header 3 | Header 4 |
| (header rows optional) |            |          |          |
+========================+============+==========+==========+
| body row 1, column 1   | column 2   | column 3 | column 4 |
+------------------------+------------+----------+----------+
| body row 2             | Cells may span columns.          |
+------------------------+------------+---------------------+
| body row 3             | Cells may  | Corner              |
+------------------------+ span rows. |                     |
| body row 4             |            |                     |
+------------------------+------------+---------------------+

But with rst-table-span you can instead write it using a csv-table:

.. csv-table::
   :header-rows: 1

   "Header row, column 1 (header rows optional)", Header 2, Header 3, Header 4
   "body row 1, column 1", column 2, column 3, column 4
   body row 2, :colspan:`3` Cells may span columns.
   body row 3, :rowspan:`2` Cells may span rows., :cellspan:`2x2` Corner
   body row 4

Or a list-table:

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

   * - Header row, column 1 (header rows optional)
     - Header 2
     - Header 3
     - Header 4
   * - body row 1, column 1
     - column 2
     - column 3
     - column 4
   * - body row 2
     - :colspan:`3` Cells may span columns.
     -
     -
   * - body row 3
     - :rowspan:`2` Cells may span rows.
     - :cellspan:`2x2` Corner
     -
   * - body row 4
     -
     -
     -

Installation and Usage

Install the package using pip:

pip install rst-table-span

or add the rst-table-span package to your project or environment dependency list (eg. pyproject.toml, requirements.txt, or similar).

Sphinx

Also add the rst_table_span module to your list of extensions in your conf.py settings:

extensions = [
    "rst_table_span",
]

Docutils

Docutils doesn’t provide a declarative extension mechanism, as far as I know. If you invoke docutils programatically using its Publisher API, then a global function is provided to register the roles. Call it before setting up the publisher:

import sys
import docutils.__main__
import rst_table_span

rst_table_span.register_docutils_roles()
sys.exit(docutils.__main__.main())

For convenience, this package installs the above script as the rst-table-span-docutils executable. It behaves the same way as the standard docutils executable. It can then be used used like this:

rst-table-span-docutils demo.rst --output demo.html

License

This library is released under the MIT license (see the LICENSE file in the source code).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rst_table_span-1.0.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rst_table_span-1.0.0-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file rst_table_span-1.0.0.tar.gz.

File metadata

  • Download URL: rst_table_span-1.0.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for rst_table_span-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e671223cdec678f37ecc6e4d15e170a9e03a217f9eeb183547038884fedb6bea
MD5 6f334a07e547a3880723a71ef5477ddb
BLAKE2b-256 2419207542209353942def2bbe89a9b4a9ad51c807045cb8bdcd675bbf799ecc

See more details on using hashes here.

File details

Details for the file rst_table_span-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for rst_table_span-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5a7c895d26058c1bdcb55db3ea13c97832c9388523aa7755b44cf896850c8ae2
MD5 7541ff7f95a18121ed6b1620bf48d890
BLAKE2b-256 9ee05af59de1cc6af4e667fac62c56f29e4231adc14a4c057398eb22f66b04c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page