diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index 7a943de33..a17e04fea 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -923,7 +923,7 @@ def _overwrite_pygments_css(app, exception=None): # ------------------------------------------------------------------------------ -# customize rendering of the links +# customize rendering of tag elements # ------------------------------------------------------------------------------ diff --git a/src/pydata_sphinx_theme/bootstrap_html_translator.py b/src/pydata_sphinx_theme/bootstrap_html_translator.py index 590afbe17..f6e43d0f1 100644 --- a/src/pydata_sphinx_theme/bootstrap_html_translator.py +++ b/src/pydata_sphinx_theme/bootstrap_html_translator.py @@ -6,6 +6,7 @@ from sphinx.writers.html5 import HTML5Translator from sphinx.util import logging from sphinx.ext.autosummary import autosummary_table +from docutils import nodes logger = logging.getLogger(__name__) @@ -22,12 +23,30 @@ def __init__(self, *args, **kwds): self.settings.table_style = "table" def starttag(self, *args, **kwargs): - """ensure an aria-level is set for any heading role""" + """ + Perform small modification on specific tags: + - ensure an aria-level is set for any heading role + - include the pst_atts in the final attributes + - include the pst_class in the final classes + """ + # update attributes using pst_atts + if hasattr(self, "pst_atts"): + for att, val in self.pst_atts.items(): + kwargs[att] = kwargs.pop(att, val) + + # update classes using pst_class + if hasattr(self, "pst_class"): + existing_classes = kwargs.get("CLASS", "").split(" ") + classes = list(set(existing_classes + self.pst_class)) + kwargs["CLASS"] = " ".join(classes) + + # ensure an aria-level is set for any heading role if kwargs.get("ROLE") == "heading" and "ARIA-LEVEL" not in kwargs: kwargs["ARIA-LEVEL"] = "2" + return super().starttag(*args, **kwargs) - def visit_table(self, node): + def visit_table(self, node: nodes.Element) -> None: """ copy of sphinx source to *not* add 'docutils' and 'align-default' classes but add 'table' class @@ -62,3 +81,12 @@ def visit_table(self, node): tag = self.starttag(node, "table", CLASS=" ".join(classes), **atts) self.body.append(tag) + + def visit_literal_block(self, node: nodes.Element) -> None: + """overwrite the literal-block element to make them focusable""" + + # add tabindex to the node + pst_atts = getattr(node, "pst_atts", {}) + self.pst_atts = {**pst_atts, "tabindex": "0"} + + return super().visit_literal_block(node)