Skip to content

Commit

Permalink
Add transform pass to sort fls_ ids to the back
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jul 2, 2024
1 parent 2f68cc3 commit 8c2ed89
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions exts/ferrocene_spec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

from . import definitions, informational, syntax_directive, std_role, paragraph_ids
from . import items_with_rubric
from .utils import FlsSortIds
from sphinx.domains import Domain

import sphinx

class SpecDomain(Domain):
name = "spec"
Expand Down Expand Up @@ -34,9 +35,9 @@ def is_empty(data):
"there is data in the domain, merge_domaindata should be implemented"
)


def setup(app):
app.add_domain(SpecDomain)
app.add_transform(FlsSortIds)
definitions.setup(app)
paragraph_ids.setup(app)
informational.setup(app)
Expand Down
23 changes: 22 additions & 1 deletion exts/ferrocene_spec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: The Ferrocene Developers

from docutils import nodes

from sphinx import transforms

def section_id_and_anchor(section):
if "names" in section:
Expand All @@ -23,3 +23,24 @@ def section_id_and_anchor(section):

class NoSectionIdError(RuntimeError):
pass

# Sphinx by default sorts all ids of the form `id[0-9]+` to the end.
# Our IDs are section name and fls_ id pairs, so in some cases this transform
# will instead sort the section name to the back, but not always!
# So we overwrite the transform instead so that our fls ids are sorted to the back.
# In addition to that we normalize them, as sphinx turns the `_` in `fls_{id}`
# into `fls-{id}` which can break the link check from working correctly.
class FlsSortIds(transforms.SphinxTransform):
# Run this step after sphinx sorted.
default_priority = transforms.SortIds.default_priority + 1

def apply(self, **kwargs):
from docutils import nodes

for node in self.document.findall(nodes.section):
for n, id in enumerate(node["ids"]):
if id.startswith("fls-"):
node["ids"][n] = id[:3] + "_" + id[4:]
# sort the fls id to the back
if len(node["ids"]) > 1 and node["ids"][0].startswith("fls_"):
node["ids"] = node["ids"][1:] + [node["ids"][0]]

0 comments on commit 8c2ed89

Please sign in to comment.