-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make code-block focusable #1104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 @@ | |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the same as:
? If so, I feel like that is a bit easier to understand. But maybe I am mis-reading these lines There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not exactly equivalent. here what I do is : In yours you always replace it even if it's already in kwargs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, check the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad you are right. I'll have a look to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do find setdefault to be easier to quickly understand. Personally I find the "if" statement to be the least "idiosyncratic" and most straightforward to understand, but setdefault works too. |
||
|
||
# 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 @@ | |
|
||
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", {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so pst_atts are chunks of data we are adding to the object on our own in order to re-use them later? And I guess we can't just modify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah wait so pst is not short for "pydata sphinx theme"? These are in-built sphinx things? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are very right "pst" stands for pydata-sphinx-theme which guarantee that no other lib should interfere with these values. It's the cool thing about Python I can add as many member as I want even after init. |
||
self.pst_atts = {**pst_atts, "tabindex": "0"} | ||
|
||
return super().visit_literal_block(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a quick note of why atts and class are added / what they do?