Skip to content

Commit

Permalink
[gen][nfc] Remove traverse method now upstream mistletoe.utils.traver…
Browse files Browse the repository at this point in the history
…se is fixed

Now my fixes <miyuchina/mistletoe#157>
<miyuchina/mistletoe#158>
<miyuchina/mistletoe#159> are in a released
version, just use the upstream traverse implementation.
  • Loading branch information
asb committed Feb 27, 2023
1 parent bfc12cc commit 52989cf
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import threading
import typing
import urllib.parse
from collections import namedtuple
from contextlib import contextmanager
from dataclasses import dataclass
from http import HTTPStatus
Expand Down Expand Up @@ -266,31 +265,6 @@ def fm_get_opt(key: str, ty: type[T]) -> T | None:
)


TraverseResult = namedtuple("TraverseResult", ["node", "parent", "depth"])


# Copied and fixed from mistletoe.utils.
# TODO: Replace with call to lib version once a new release includes it.
def traverse(
source: mistletoe.token.Token,
klass: type | None = None,
depth: int | None = None,
include_source: bool = False,
) -> Iterator[TraverseResult]:
current_depth = 0
if include_source:
yield TraverseResult(source, None, current_depth)
next_children = [(source, c) for c in getattr(source, "children", [])]
while next_children and (depth is None or current_depth > depth):
current_depth += 1
new_children = []
for parent, child in next_children:
if klass is None or isinstance(child, klass):
yield TraverseResult(child, parent, current_depth)
new_children.extend([(child, c) for c in getattr(child, "children", [])])
next_children = new_children


def strip_anchor(target: str) -> str:
return target.split("#")[0]

Expand Down Expand Up @@ -371,7 +345,7 @@ def extract_and_remove_article_title(doc: mistletoe.Document) -> str:

def check_and_rewrite_links(doc: mistletoe.Document) -> None:
# Rewrite any /pages/foo.md links to appropriate permalinks.
for t in traverse(doc, klass=mistletoe.span_token.Link):
for t in mistletoe.utils.traverse(doc, klass=mistletoe.span_token.Link):
# TODO: check anchor links, including within the current document.
stripped_target = strip_anchor(t.node.target)
check_link_target(stripped_target, src_file)
Expand All @@ -387,7 +361,7 @@ def process_changelog(
changelog_heading = None
last_major_update = None
last_minor_update = None
for t in traverse(doc, klass=mistletoe.block_token.Heading):
for t in mistletoe.utils.traverse(doc, klass=mistletoe.block_token.Heading):
if changelog_heading:
err("changelog wasn't last heading")
# TODO: error if changelog heading isn't the last
Expand Down

0 comments on commit 52989cf

Please sign in to comment.