3
3
import types
4
4
5
5
import sphinx
6
+ from docutils import nodes
6
7
from packaging .version import Version
7
8
from sphinx .application import Sphinx
8
9
from sphinx .ext .autosummary import autosummary_table
@@ -23,11 +24,35 @@ def __init__(self, *args, **kwds):
23
24
self .settings .table_style = "table"
24
25
25
26
def starttag (self , * args , ** kwargs ):
26
- """Ensure an aria-level is set for any heading role."""
27
+ """Perform small modifications to tags.
28
+
29
+ - ensure aria-level is set for any tag with heading role
30
+ - ensure <pre> tags have tabindex="0".
31
+ """
27
32
if kwargs .get ("ROLE" ) == "heading" and "ARIA-LEVEL" not in kwargs :
28
33
kwargs ["ARIA-LEVEL" ] = "2"
34
+
35
+ if "pre" in args :
36
+ kwargs ["tabindex" ] = "0"
37
+
29
38
return super ().starttag (* args , ** kwargs )
30
39
40
+ def visit_literal_block (self , node ):
41
+ """Modify literal blocks.
42
+
43
+ - add tabindex="0" to <pre> tags within the HTML tree of the literal
44
+ block
45
+ """
46
+ try :
47
+ super ().visit_literal_block (node )
48
+ except nodes .SkipNode :
49
+ # If the super method raises nodes.SkipNode, then we know it
50
+ # executed successfully and appended to self.body a string of HTML
51
+ # representing the code block, which we then modify.
52
+ html_string = self .body [- 1 ]
53
+ self .body [- 1 ] = html_string .replace ("<pre" , '<pre tabindex="0"' )
54
+ raise nodes .SkipNode
55
+
31
56
def visit_table (self , node ):
32
57
"""Custom visit table method.
33
58
0 commit comments