11"""A custom Sphinx HTML Translator for Bootstrap layout
22"""
33from packaging .version import Version
4- from docutils import nodes
54
65import sphinx
76from 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