Skip to content

Commit 92f4ba7

Browse files
committed
move FlsSortIds to its own module
1 parent 5170776 commit 92f4ba7

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

exts/ferrocene_spec/__init__.py

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

44
from . import definitions, informational, syntax_directive, std_role, paragraph_ids
5-
from . import items_with_rubric
6-
from .utils import FlsSortIds
5+
from . import items_with_rubric, sphinx_fixes
76
from sphinx.domains import Domain
87

98

@@ -38,11 +37,11 @@ def is_empty(data):
3837

3938
def setup(app):
4039
app.add_domain(SpecDomain)
41-
app.add_transform(FlsSortIds)
4240
definitions.setup(app)
4341
paragraph_ids.setup(app)
4442
informational.setup(app)
4543
items_with_rubric.setup(app)
44+
sphinx_fixes.setup(app)
4645

4746
app.add_config_value(
4847
name="spec_std_docs_url",

exts/ferrocene_spec/sphinx_fixes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-License-Identifier: MIT OR Apache-2.0
2+
# SPDX-FileCopyrightText: The Ferrocene Developers
3+
4+
from docutils import nodes
5+
from sphinx.transforms import SphinxTransform, SortIds
6+
7+
# Sphinx by default sorts all ids of the form `id[0-9]+` to the end.
8+
# Our IDs are section name and fls_ id pairs, so in some cases this transform
9+
# will instead sort the section name to the back, but not always!
10+
# So we overwrite the transform instead so that our fls ids are sorted to the back.
11+
# In addition to that we normalize them, as sphinx turns the `_` in `fls_{id}`
12+
# into `fls-{id}` which can break the link check from working correctly.
13+
class FerroceneSortIds(SphinxTransform):
14+
# Run this step after sphinx sorted.
15+
default_priority = SortIds.default_priority + 1
16+
17+
def apply(self, **kwargs):
18+
for node in self.document.findall(nodes.section):
19+
for n, id in enumerate(node["ids"]):
20+
if id.startswith("fls-"):
21+
node["ids"][n] = id[:3] + "_" + id[4:]
22+
# sort the fls id to the back
23+
if len(node["ids"]) > 1 and node["ids"][0].startswith("fls_"):
24+
node["ids"] = node["ids"][1:] + [node["ids"][0]]
25+
26+
27+
def setup(app):
28+
app.add_transform(FerroceneSortIds)

exts/ferrocene_spec/utils.py

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

44
from docutils import nodes
5-
from sphinx import transforms
65

76

87
def section_id_and_anchor(section):
@@ -24,25 +23,3 @@ def section_id_and_anchor(section):
2423

2524
class NoSectionIdError(RuntimeError):
2625
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)