Skip to content

Commit

Permalink
Tree-walk to find all the links and associate them with sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
tabatkins committed Jan 25, 2025
1 parent 1adf34d commit 2e3e72d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
15 changes: 8 additions & 7 deletions bikeshed/dfnpanels/dfnpanels.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
def addDfnPanels(doc: t.SpecT, dfns: list[t.ElementT]) -> None:
# Constructs "dfn panels" which show all the local references to a term
# Gather all the <a href>s together
allRefs: OrderedDict[str, list[t.ElementT]] = OrderedDict()
for a in h.findAll("a", doc):
linksFromTargetId: OrderedDict[str, list[t.ElementT]] = OrderedDict()
sectionNameFromLink = h.collectLinksWithSectionNames(doc)
for a in sectionNameFromLink:
href = a.get("href")
if href is None:
continue
if not href.startswith("#"):
continue
allRefs.setdefault(href[1:], []).append(a)
linksFromTargetId.setdefault(href[1:], []).append(a)
panelsJSON = doc.extraJC.addDfnPanels()
for dfn in dfns:
id = dfn.get("id")
Expand All @@ -26,10 +27,10 @@ def addDfnPanels(doc: t.SpecT, dfns: list[t.ElementT]) -> None:
# Something went wrong, bail.
continue
refsFromSection: OrderedDict[str, list[t.ElementT]] = OrderedDict()
for link in allRefs.get(id, []):
section = h.sectionName(doc, link)
if section is not None:
refsFromSection.setdefault(section, []).append(link)
for link in linksFromTargetId.get(id, []):
sectionName = sectionNameFromLink.get(link)
if sectionName is not None:
refsFromSection.setdefault(sectionName, []).append(link)
h.addClass(doc, dfn, "dfn-paneled")
sectionsJson = []
for text, els in refsFromSection.items():
Expand Down
1 change: 1 addition & 0 deletions bikeshed/h/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
closestAttr,
collectAutolinks,
collectIds,
collectLinksWithSectionNames,
collectSyntaxHighlightables,
createElement,
dedupIDs,
Expand Down
27 changes: 25 additions & 2 deletions bikeshed/h/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,31 @@ def sectionName(doc: t.SpecT, el: t.ElementT) -> str | None:
return textContent(h)


def collectLinksWithSectionNames(
doc: t.SpecT,
root: t.ElementT | None = None,
links: dict[t.ElementT, str | None] | None = None,
name: str | None = "Unnamed section",
) -> dict[t.ElementT, str | None]:
# Tree-walk to collect all links, and compute their section names
# along the way.
if links is None:
links = {}
if root is None:
root = doc.body
for child in childElements(root):
if tagName(child) == "a":
links[child] = name
continue
if tagName(child) in ("h1", "h2", "h3", "h4", "h5", "h6"):
if hasClass(doc, child, "no-ref"):
name = None
else:
name = textContent(child)
collectLinksWithSectionNames(doc, child, links, name)
return links


def scopingElements(startEl: t.ElementT, tags: list[str]) -> t.Generator[t.ElementT, None, None]:
# Elements that could form a "scope" for the startEl
# Ancestors, and preceding siblings of ancestors.
Expand Down Expand Up @@ -524,8 +549,6 @@ def previousElements(startEl: t.ElementT, tag: str | None = None, *tags: str) ->


def childElements(parentEl: t.ElementT, oddNodes: bool = False) -> t.Generator[t.ElementT, None, None]:
if len(parentEl) == 0:
return
tag = None if oddNodes else "*"
yield from parentEl.iterchildren(tag)

Expand Down

0 comments on commit 2e3e72d

Please sign in to comment.