2
2
# SPDX-FileCopyrightText: The Ferrocene Developers
3
3
4
4
from docutils import nodes
5
+ from sphinx import transforms
5
6
6
7
7
8
def section_id_and_anchor (section ):
@@ -23,3 +24,25 @@ def section_id_and_anchor(section):
23
24
24
25
class NoSectionIdError (RuntimeError ):
25
26
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