Skip to content

Commit f0e299f

Browse files
Merge #503
503: Monkey patch sphinx SortIDs transform to sort our own IDs r=pietroalbini a=Veykril This fixes some anchoring inconsistencies and also makes link check correctly work for section id <-> other id collisions, see 3cf79ed which CI did not catch so far, now it will. cc ferrocene/ferrocene#699 which broke because of the collision Co-authored-by: Lukas Wirth <[email protected]>
2 parents 30b1ba3 + e91470f commit f0e299f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

exts/ferrocene_spec/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from . import definitions, informational, syntax_directive, std_role, paragraph_ids
55
from . import items_with_rubric
6+
from .utils import FlsSortIds
67
from sphinx.domains import Domain
78

89

@@ -37,6 +38,7 @@ def is_empty(data):
3738

3839
def setup(app):
3940
app.add_domain(SpecDomain)
41+
app.add_transform(FlsSortIds)
4042
definitions.setup(app)
4143
paragraph_ids.setup(app)
4244
informational.setup(app)

exts/ferrocene_spec/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-FileCopyrightText: The Ferrocene Developers
33

44
from docutils import nodes
5+
from sphinx import transforms
56

67

78
def section_id_and_anchor(section):
@@ -23,3 +24,25 @@ def section_id_and_anchor(section):
2324

2425
class NoSectionIdError(RuntimeError):
2526
pass
27+
28+
29+
# Sphinx by default sorts all ids of the form `id[0-9]+` to the end.
30+
# Our IDs are section name and fls_ id pairs, so in some cases this transform
31+
# will instead sort the section name to the back, but not always!
32+
# So we overwrite the transform instead so that our fls ids are sorted to the back.
33+
# In addition to that we normalize them, as sphinx turns the `_` in `fls_{id}`
34+
# into `fls-{id}` which can break the link check from working correctly.
35+
class FlsSortIds(transforms.SphinxTransform):
36+
# Run this step after sphinx sorted.
37+
default_priority = transforms.SortIds.default_priority + 1
38+
39+
def apply(self, **kwargs):
40+
from docutils import nodes
41+
42+
for node in self.document.findall(nodes.section):
43+
for n, id in enumerate(node["ids"]):
44+
if id.startswith("fls-"):
45+
node["ids"][n] = id[:3] + "_" + id[4:]
46+
# sort the fls id to the back
47+
if len(node["ids"]) > 1 and node["ids"][0].startswith("fls_"):
48+
node["ids"] = node["ids"][1:] + [node["ids"][0]]

0 commit comments

Comments
 (0)