Skip to content

Commit 88d30a0

Browse files
authored
FIX: respect the align and width parameter of table directives (#968)
* fix: respect the width instructions * fix: respect the align parameter * default to center
1 parent 4fb84f8 commit 88d30a0

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/pydata_sphinx_theme/assets/styles/content/_tables.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ table {
1010
overflow: auto;
1111

1212
@include scrollbar-style();
13+
14+
// default to table-center
15+
margin-left: auto;
16+
margin-right: auto;
17+
18+
&.table-right {
19+
margin-right: 0;
20+
}
21+
22+
&.table-left {
23+
margin-left: 0;
24+
}
1325
}
1426

1527
// customize table caption from bootstrap

src/pydata_sphinx_theme/bootstrap_html_translator.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""A custom Sphinx HTML Translator for Bootstrap layout
22
"""
33
from packaging.version import Version
4-
from docutils import nodes
54

65
import sphinx
76
from sphinx.writers.html5 import HTML5Translator
@@ -29,9 +28,13 @@ def starttag(self, *args, **kwargs):
2928
return super().starttag(*args, **kwargs)
3029

3130
def visit_table(self, node):
32-
# type: (nodes.Element) -> None
33-
# copy of sphinx source to *not* add 'docutils' and 'align-default' classes
34-
# but add 'table' class
31+
"""
32+
copy of sphinx source to *not* add 'docutils' and 'align-default' classes
33+
but add 'table' class
34+
"""
35+
36+
# init the attributes
37+
atts = {}
3538

3639
# generate_targets_for_table is deprecated in 4.0
3740
if Version(sphinx.__version__) < Version("4.0"):
@@ -42,14 +45,20 @@ def visit_table(self, node):
4245
else:
4346
self._table_row_indices.append(0)
4447

48+
# get the classes
4549
classes = [cls.strip(" \t\n") for cls in self.settings.table_style.split(",")]
4650

4751
# we're looking at the 'real_table', which is wrapped by an autosummary
4852
if isinstance(node.parent, autosummary_table):
4953
classes += ["autosummary"]
5054

51-
# classes.insert(0, "docutils") # compat
52-
# if 'align' in node:
53-
# classes.append('align-%s' % node['align'])
54-
tag = self.starttag(node, "table", CLASS=" ".join(classes))
55+
# add the width if set in a style attribute
56+
if "width" in node:
57+
atts["style"] = f'width: {node["width"]}'
58+
59+
# add specific class if align is set
60+
if "align" in node:
61+
classes.append(f'table-{node["align"]}')
62+
63+
tag = self.starttag(node, "table", CLASS=" ".join(classes), **atts)
5564
self.body.append(tag)

0 commit comments

Comments
 (0)