-
-
Notifications
You must be signed in to change notification settings - Fork 611
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
Fix non-unique heading anchors on changelog doc page #1942
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 |
---|---|---|
|
@@ -3,12 +3,19 @@ | |
|
||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
from importlib.metadata import version as get_version | ||
from pathlib import Path | ||
|
||
from sphinx.application import Sphinx | ||
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. Obviously, this won't be necessary upon moving to a proper extension. |
||
from sphinx.util import logging | ||
from sphinx.util.console import bold | ||
|
||
# Add the docs/ directory to sys.path to be able to import utils | ||
docs_dir = os.path.dirname(os.path.dirname(__file__)) | ||
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. Plz use pathlib and only convert to str on insert. |
||
sys.path.insert(0, docs_dir) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# -- Path setup -------------------------------------------------------------- | ||
|
@@ -57,3 +64,9 @@ | |
] | ||
|
||
suppress_warnings = ["myst.xref_missing"] | ||
|
||
|
||
def setup(app: Sphinx) -> None: | ||
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. Don't add Also, it should return a dict. |
||
from docs.utils import TransformSectionIdToName | ||
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. This is not needed, especially upon moving to the extension. Just add the extension to the list above and Sphinx will load it through the standard means. |
||
|
||
app.add_transform(TransformSectionIdToName) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||||||||||||||||
from __future__ import annotations | ||||||||||||||||||||||
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. Move this to the |
||||||||||||||||||||||
|
||||||||||||||||||||||
import re | ||||||||||||||||||||||
from typing import Any | ||||||||||||||||||||||
|
||||||||||||||||||||||
from docutils import nodes | ||||||||||||||||||||||
from sphinx.transforms import SortIds, SphinxTransform | ||||||||||||||||||||||
|
||||||||||||||||||||||
numerical_id = re.compile(r"^id[0-9]+$") | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
class TransformSectionIdToName(SphinxTransform): # type: ignore[misc] | ||||||||||||||||||||||
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. What does this ignore? |
||||||||||||||||||||||
"""Transforms section ids from <id1>, <id2>, ... to id <section-name>.""" | ||||||||||||||||||||||
|
||||||||||||||||||||||
default_priority = SortIds.default_priority + 1 | ||||||||||||||||||||||
|
||||||||||||||||||||||
def apply(self, **kwargs: Any) -> Any: | ||||||||||||||||||||||
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. This returns none, not any. |
||||||||||||||||||||||
for node in self.document.findall(nodes.section): | ||||||||||||||||||||||
if not self._has_numerical_id(node): | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
self._transform_id_to_name(node) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def _has_numerical_id(self, node: nodes.section) -> bool: | ||||||||||||||||||||||
node_ids = node["ids"] | ||||||||||||||||||||||
if not node_ids: | ||||||||||||||||||||||
return False | ||||||||||||||||||||||
|
||||||||||||||||||||||
if len(node_ids) != 1: | ||||||||||||||||||||||
return False | ||||||||||||||||||||||
|
||||||||||||||||||||||
return bool(numerical_id.match(node_ids[0])) | ||||||||||||||||||||||
Comment on lines
+25
to
+31
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. This can be simplified:
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def _transform_id_to_name(self, node: nodes.section) -> None: | ||||||||||||||||||||||
node["ids"] = [nodes.make_id("id " + name) for name in node["names"]] | ||||||||||||||||||||||
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. String concatenation is slow.
Suggested change
|
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.
Don't add Python modules among the normal documents.