Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monkey patch sphinx SortIDs transform to sort our own IDs #503

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]]
Loading