| 1 |
"""A custom Sphinx HTML Translator for Bootstrap layout.""" |
| 2 |
|
| 3 |
import types |
| 4 |
|
| 5 |
from sphinx.application import Sphinx |
| 6 |
from sphinx.ext.autosummary import autosummary_table |
| 7 |
from sphinx.util import logging |
| 8 |
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__) |
| 11 |
|
| 12 |
|
| 13 |
class BootstrapHTML5TranslatorMixin: |
| 14 |
"""Mixin HTML Translator for a Bootstrap-ified Sphinx layout. |
| 15 |
|
| 16 |
Only a couple of functions have been overridden to produce valid HTML to be |
| 17 |
directly styled with Bootstrap, and fulfill acessibility best practices. |
| 18 |
""" |
| 19 |
|
| 20 |
def __init__(self, *args, **kwds): |
| 21 |
super().__init__(*args, **kwds) |
| 22 |
self.settings.table_style = "table" |
| 23 |
|
| 24 |
def starttag(self, *args, **kwargs): |
| 25 |
"""Perform small modifications to tags. |
| 26 |
|
| 27 |
- ensure aria-level is set for any tag with heading role |
| 28 |
""" |
| 29 |
if kwargs.get("ROLE") == "heading" and "ARIA-LEVEL" not in kwargs: |
| 30 |
kwargs["ARIA-LEVEL"] = "2" |
| 31 |
|
| 32 |
return super().starttag(*args, **kwargs) |
| 33 |
|
| 34 |
def visit_table(self, node): |
| 35 |
""" |
| 36 |
Custom visit table method. |
| 37 |
|
| 38 |
Copy of sphinx source to *not* add 'docutils' and 'align-default' classes but |
| 39 |
add 'table' class. |
| 40 |
""" |
| 41 |
# init the attributes |
| 42 |
atts = {} |
| 43 |
|
| 44 |
self._table_row_indices.append(0) |
| 45 |
|
| 46 |
# get the classes |
| 47 |
classes = [cls.strip(" \t\n") for cls in self.settings.table_style.split(",")] |
| 48 |
|
| 49 |
# we're looking at the 'real_table', which is wrapped by an autosummary |
| 50 |
if isinstance(node.parent, autosummary_table): |
| 51 |
classes += ["autosummary"] |
| 52 |
|
| 53 |
# add the width if set in a style attribute |
| 54 |
if "width" in node: |
| 55 |
atts["style"] = f"width: {node['width']}" |
| 56 |
|
| 57 |
# add specific class if align is set |
| 58 |
if "align" in node: |
| 59 |
classes.append(f"table-{node['align']}") |
| 60 |
|
| 61 |
# put table within a scrollable container (for tables that are too wide) |
| 62 |
self.body.append('<div class="pst-scrollable-table-container">') |
| 63 |
|
| 64 |
tag = self.starttag(node, "table", CLASS=" ".join(classes), **atts) |
| 65 |
self.body.append(tag) |
| 66 |
|
| 67 |
def depart_table(self, node): |
| 68 |
""" |
| 69 |
Custom depart_table method to close the scrollable div we add in |
| 70 |
visit_table. |
| 71 |
""" |
| 72 |
super().depart_table(node) |
| 73 |
self.body.append("</div>\n") |
| 74 |
|
| 75 |
|
| 76 |
def setup_translators(app: Sphinx): |
| 77 |
""" |
| 78 |
Add bootstrap HTML functionality if we are using an HTML translator. |
| 79 |
|
| 80 |
This re-uses the pre-existing Sphinx translator and adds extra functionality |
| 81 |
defined in ``BootstrapHTML5TranslatorMixin``. |
| 82 |
This way we can retain the original translator's |
| 83 |
behavior and configuration, and _only_ add the extra bootstrap rules. |
| 84 |
If we don't detect an HTML-based translator, then we do nothing. |
| 85 |
""" |
| 86 |
# Do not add the mixin if the builder format is not HTML: |
| 87 |
if app.builder.format != "html": |
| 88 |
return |
| 89 |
|
| 90 |
if not app.registry.translators.items(): |
| 91 |
try: |
| 92 |
default_translator_class = app.builder.default_translator_class |
| 93 |
except AttributeError: |
| 94 |
# some builders, e.g. linkcheck, do not define 'default_translator_class' |
| 95 |
return |
| 96 |
|
| 97 |
translator = types.new_class( |
| 98 |
"BootstrapHTML5Translator", |
| 99 |
( |
| 100 |
BootstrapHTML5TranslatorMixin, |
| 101 |
default_translator_class, |
| 102 |
), |
| 103 |
{}, |
| 104 |
) |
| 105 |
app.set_translator(app.builder.name, translator, override=True) |
| 106 |
else: |
| 107 |
for name, klass in app.registry.translators.items(): |
| 108 |
translator = types.new_class( |
| 109 |
"BootstrapHTML5Translator", |
| 110 |
( |
| 111 |
BootstrapHTML5TranslatorMixin, |
| 112 |
klass, |
| 113 |
), |
| 114 |
{}, |
| 115 |
) |
| 116 |
app.set_translator(name, translator, override=True) |