| 1 |
=========================================================================== |
| 2 |
:mod:`cloud_sptheme.ext.table_styling` - adds directives for styling tables |
| 3 |
=========================================================================== |
| 4 |
|
| 5 |
.. module:: cloud_sptheme.ext.table_styling |
| 6 |
:synopsis: adds directives for styling tables |
| 7 |
|
| 8 |
.. versionchanged:: 1.9 |
| 9 |
|
| 10 |
Added ``:header-alignment:`` directive. |
| 11 |
|
| 12 |
Overview |
| 13 |
======== |
| 14 |
This Sphinx extension replaces the default ``.. table::`` directive |
| 15 |
with a custom one, that supports a number of extra options for controlling |
| 16 |
table layout on a per-column basis. |
| 17 |
|
| 18 |
For example, the following snippet specifies relative widths for the |
| 19 |
three columns, changes the text alignment for each column, |
| 20 |
disables text-wrapping for the third column, and inserts |
| 21 |
dividers between the columns:: |
| 22 |
|
| 23 |
.. table:: Optional Caption |
| 24 |
:widths: 3 2 1 |
| 25 |
:column-alignment: left center right |
| 26 |
:column-wrapping: true true false |
| 27 |
:column-dividers: none single double single |
| 28 |
|
| 29 |
=========== =========== =========== |
| 30 |
Width 50% Width 33% Width 16% |
| 31 |
=========== =========== =========== |
| 32 |
Line 1 This text This text |
| 33 |
should wrap will always |
| 34 |
onto be one line. |
| 35 |
multiple |
| 36 |
lines. |
| 37 |
Line 2 Centered. Right-Aligned. |
| 38 |
Line 3 Centered Right-Aligned |
| 39 |
Again. Again. |
| 40 |
=========== =========== =========== |
| 41 |
|
| 42 |
This will then result in the following table: |
| 43 |
|
| 44 |
.. table:: Optional Caption |
| 45 |
:widths: 3 2 1 |
| 46 |
:column-alignment: left center right |
| 47 |
:column-wrapping: yes yes no |
| 48 |
:column-dividers: none single double single |
| 49 |
|
| 50 |
=========== =========== =========== |
| 51 |
Width 50% Width 33% Width 16% |
| 52 |
=========== =========== =========== |
| 53 |
Line 1 This text This text |
| 54 |
should wrap will always |
| 55 |
onto be one line. |
| 56 |
multiple |
| 57 |
lines. |
| 58 |
Line 2 Centered. Right-Aligned. |
| 59 |
Line 3 Centered Right-Aligned |
| 60 |
Again. Again. |
| 61 |
=========== =========== =========== |
| 62 |
|
| 63 |
Directive Options |
| 64 |
================= |
| 65 |
The enhanced ``.. table::`` directive supports the following options: |
| 66 |
|
| 67 |
``:widths:`` |
| 68 |
Sets proportional column widths |
| 69 |
|
| 70 |
This should be a space-separated list of positive integers, |
| 71 |
one for every column. The columns widths will be allocated |
| 72 |
proportionally (e.g. ``1 2 3`` for a 3-column table means the columns |
| 73 |
will use 16%, 33%, and 50% of the total width, respectively). |
| 74 |
|
| 75 |
``:column-alignment:`` |
| 76 |
Sets per-column text alignment |
| 77 |
|
| 78 |
This should be a comma/space-separated list of the following |
| 79 |
strings: ``left``, ``right``, ``center``, ``justify``. |
| 80 |
These are interpreted one per column. Extra values are ignored; |
| 81 |
if not enough values are provided, the remaining columns |
| 82 |
will default to ``left``. |
| 83 |
|
| 84 |
Alternately this can be a single word containing |
| 85 |
just the first letters: e.g. ``lrcj`` would be interpreted |
| 86 |
the same as ``left right center justify``. |
| 87 |
|
| 88 |
``:header-alignment:`` |
| 89 |
Sets per-column text alignment *for header rows*. |
| 90 |
|
| 91 |
This has the same format as ``:column-alignment:``. |
| 92 |
If specified, all headers rows will use these alignment values |
| 93 |
instead of the default column alignments. |
| 94 |
|
| 95 |
``:column-wrapping:`` |
| 96 |
Sets per-column text wrapping |
| 97 |
|
| 98 |
This should be a comma/space-separated list of |
| 99 |
either ``yes``/``true`` or ``no``/``false``. If ``yes`` (the default), |
| 100 |
words will wrap around if there is not enough horizontal space. |
| 101 |
If ``no``, whitespace-wrapping will be disabled for that column. |
| 102 |
Extra values are ignored; |
| 103 |
if not enough values are provided, the remaining columns |
| 104 |
will default to ``true``. |
| 105 |
|
| 106 |
Alternately this can be a single word containing |
| 107 |
just the first letters: e.g. ``ttf`` is the same as ``true true false``. |
| 108 |
|
| 109 |
``:column-dividers:`` |
| 110 |
Add dividers between columns |
| 111 |
|
| 112 |
This lets you specify custom vertical dividers between columns |
| 113 |
(ala what ``.. tabularcolumns::`` allows under latex). |
| 114 |
|
| 115 |
This should be a comma/space-separated list of |
| 116 |
``none``, ``single``, or ``double``; based on the type of divider |
| 117 |
you want. There should be one of these for the left side of the table, |
| 118 |
for between each column, and for the right side of the table |
| 119 |
(e.g. a 4 entries for a 3-column table). Extra values are ignored; |
| 120 |
if not enough values are provided, the remaining columns will default to ``none``. |
| 121 |
|
| 122 |
Alternately this can be a single word containing |
| 123 |
just the number: e.g. ``0121`` is the same as ``none single double single``. |
| 124 |
|
| 125 |
``:column-classes:`` |
| 126 |
Add per-column css classes. |
| 127 |
|
| 128 |
This lets you specify css classes that will be assigned to each |
| 129 |
column, much like ``.. rst-class::``. This should either |
| 130 |
be a space-separated list containing one class per column, |
| 131 |
or a comma-separated list containing multiple classes for each column, |
| 132 |
separated by spaces. Extra values are ignored; |
| 133 |
if there are not enough values, or there are blank entries, those columns |
| 134 |
will not be assigned any custom classes. |
| 135 |
|
| 136 |
For example, ``a b, c , , d`` |
| 137 |
would assign the classes ``a b`` to column 1, ``c`` to column 2, |
| 138 |
no custom classes for column 3, and ``d`` to column 4. |
| 139 |
|
| 140 |
``:header-columns:`` |
| 141 |
Specify number of "stub" columns |
| 142 |
|
| 143 |
Should be a non-negative integer specifying the number of |
| 144 |
columns (starting with the left side) that should be treated |
| 145 |
as "stub" or "header" columns, and should be styled accordingly. |
| 146 |
|
| 147 |
Configuration |
| 148 |
============= |
| 149 |
This extension reads the following ``conf.py`` options: |
| 150 |
|
| 151 |
``table_style_default_align`` |
| 152 |
|
| 153 |
Optionally add default value for ``:align:`` directive. |
| 154 |
Sphinx 2 changed it's default to "centered" tables, |
| 155 |
setting this directive to "left" will restore the original behavior. |
| 156 |
|
| 157 |
.. versionadded:: 1.10.0 |
| 158 |
|
| 159 |
``table_styling_embed_css`` |
| 160 |
|
| 161 |
Controls whether or not the custom css file should be included. |
| 162 |
Defaults to ``True``, set to ``False`` to disable. |
| 163 |
|
| 164 |
``table_styling_class`` |
| 165 |
|
| 166 |
By default, all HTML tables styled by this extension will |
| 167 |
have the css class ``"styled-table"`` added, to help with themeing support. |
| 168 |
Use this option to override with your own theme's preferred css class. |
| 169 |
|
| 170 |
Internals |
| 171 |
========= |
| 172 |
.. note:: |
| 173 |
|
| 174 |
This extension gets the job done by adding |
| 175 |
custom css classes to every cell in the generated html table. |
| 176 |
It then inserts a custom css file which provides styling |
| 177 |
implementing relevant parts of the above directives. |
| 178 |
While this extension is primarily tested with :mod:`!cloud_sptheme`, |
| 179 |
it should work with most Sphinx themes, any conflicts that occur |
| 180 |
are probably bugs. |
| 181 |
|
| 182 |
.. todo:: make this autogenerate a matching ``.. tabularcolumns`` directive for latex. |
| 183 |
.. todo:: allow ``:widths:`` to support ``em``, ``in``, ``%`` |