Skip to content

Commit

Permalink
Merge #503
Browse files Browse the repository at this point in the history
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 <lukastw97@gmail.com>
  • Loading branch information
bors-ferrocene[bot] and Veykril committed Jul 3, 2024
2 parents 30b1ba3 + e91470f commit f0e299f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions exts/ferrocene_spec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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


Expand Down Expand Up @@ -37,6 +38,7 @@ def is_empty(data):

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: 23 additions & 0 deletions exts/ferrocene_spec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: The Ferrocene Developers

from docutils import nodes
from sphinx import transforms


def section_id_and_anchor(section):
Expand All @@ -23,3 +24,25 @@ 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 f0e299f

Please sign in to comment.