|
1 | 1 | """A custom Sphinx HTML Translator for Bootstrap layout.""" |
2 | 2 |
|
3 | 3 | import types |
4 | | -from functools import partial |
5 | 4 |
|
6 | 5 | import sphinx |
7 | | -from docutils.nodes import Element |
8 | 6 | from packaging.version import Version |
9 | 7 | from sphinx.application import Sphinx |
10 | 8 | from sphinx.ext.autosummary import autosummary_table |
11 | 9 | from sphinx.util import logging |
12 | 10 |
|
13 | | -from .utils import traverse_or_findall |
14 | | - |
15 | 11 | logger = logging.getLogger(__name__) |
16 | 12 |
|
17 | 13 |
|
@@ -63,92 +59,6 @@ def visit_table(self, node): |
63 | 59 | tag = self.starttag(node, "table", CLASS=" ".join(classes), **atts) |
64 | 60 | self.body.append(tag) |
65 | 61 |
|
66 | | - # NOTE: `visit_section`, `visit_desc_signature` & `visit_reference` are extended |
67 | | - # here to resolve #1026 & #1207. There is an open issue with Sphinx to address this: |
68 | | - # https://github.com/sphinx-doc/sphinx/issues/11208 |
69 | | - # If the issue is resolved within Sphinx, these methods can be removed. |
70 | | - |
71 | | - def visit_section(self, node): |
72 | | - """Handle section nodes to replace dots with underscores. |
73 | | -
|
74 | | - This will modify the ``id`` of HTML ``<section>`` tags, where Python modules |
75 | | - are documented. Replacing dots with underscores allows the tags to be recognized |
76 | | - as navigation targets by ScrollSpy. |
77 | | - """ |
78 | | - if "ids" in node: |
79 | | - node["ids"] = [id_.replace(".", "_") for id_ in node["ids"]] |
80 | | - super().visit_section(node) |
81 | | - |
82 | | - def visit_desc_signature(self, node): |
83 | | - """Handle function & method signature nodes to replace dots with underscores. |
84 | | -
|
85 | | - This will modify the ``id`` attribute of HTML ``<dt>`` & ``<dd>`` tags, where |
86 | | - Python functions are documented. Replacing dots with underscores allows the tags |
87 | | - to be recognized as navigation targets by ScrollSpy. |
88 | | - """ |
89 | | - if "ids" in node: |
90 | | - ids = node["ids"] |
91 | | - for i, id_ in enumerate(ids): |
92 | | - ids[i] = id_.replace(".", "_") |
93 | | - super().visit_desc_signature(node) |
94 | | - |
95 | | - def visit_reference(self, node): |
96 | | - """Handle reference nodes to replace dots with underscores. |
97 | | -
|
98 | | - This will modify the ``href`` attribute of any internal HTML ``<a>`` tags, e.g. |
99 | | - the sidebar navigation links. |
100 | | - """ |
101 | | - try: |
102 | | - # We are only interested in internal anchor references |
103 | | - internal, anchorname = node["internal"], node["anchorname"] |
104 | | - if internal and anchorname.startswith("#") and "." in anchorname: |
105 | | - # Get the root node of the current document |
106 | | - document = self.builder.env.get_doctree(self.builder.current_docname) |
107 | | - |
108 | | - # Get the target anchor ID |
109 | | - target_id = anchorname.lstrip("#") |
110 | | - sanitized_id = target_id.replace(".", "_") |
111 | | - # Update the node `href` |
112 | | - node["refuri"] = node["anchorname"] = "#" + sanitized_id |
113 | | - |
114 | | - # Define a search condition to find the target node by ID |
115 | | - def find_target(search_id, node): |
116 | | - return ( |
117 | | - isinstance(node, Element) |
118 | | - and ("ids" in node) |
119 | | - and (search_id in node["ids"]) |
120 | | - ) |
121 | | - |
122 | | - # NOTE: Replacing with underscores creates the possibility for |
123 | | - # conflicting references. Here we check for these and warn the |
124 | | - # user if any are found. |
125 | | - if any( |
126 | | - traverse_or_findall( |
127 | | - document, condition=partial(find_target, sanitized_id) |
128 | | - ) |
129 | | - ): |
130 | | - logger.warning( |
131 | | - f'Sanitized reference "{sanitized_id}" for "{target_id}" ' |
132 | | - "conflicts with an existing reference!" |
133 | | - ) |
134 | | - |
135 | | - # Find nodes with the given ID (there should only be one) |
136 | | - targets = traverse_or_findall( |
137 | | - document, condition=partial(find_target, target_id) |
138 | | - ) |
139 | | - # Replace dots with underscores in the target node ID |
140 | | - for target in targets: |
141 | | - # NOTE: By itself, modifying the target `ids` here seems to be |
142 | | - # insufficient, however it helps ensure that the reference `refuri` |
143 | | - # and target `ids` remain consistent during the build process |
144 | | - target["ids"] = [ |
145 | | - sanitized_id if id_ == target_id else id_ |
146 | | - for id_ in target["ids"] |
147 | | - ] |
148 | | - except KeyError: |
149 | | - pass |
150 | | - super().visit_reference(node) |
151 | | - |
152 | 62 |
|
153 | 63 | def setup_translators(app: Sphinx): |
154 | 64 | """Add bootstrap HTML functionality if we are using an HTML translator. |
|
0 commit comments