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

Handle anchors in doc_url() correctly. #15812

Merged
merged 2 commits into from
Jun 13, 2022
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
7 changes: 5 additions & 2 deletions build-support/bin/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@

logger = logging.getLogger(__name__)

DOC_URL_RE = re.compile(r"https://www.pantsbuild.org/v(\d+\.[^/]+)/docs/(?P<slug>[a-zA-Z0-9_-]+)")
DOC_URL_RE = re.compile(
r"https://www.pantsbuild.org/v(\d+\.[^/]+)/docs/(?P<slug>[a-zA-Z0-9_-]+)(?P<anchor>#[a-zA-Z0-9_-]+)?"
)


def main() -> None:
Expand Down Expand Up @@ -119,10 +121,11 @@ def _rewrite_url(self, mo: re.Match) -> str:
# The docsite injects the version automatically at markdown rendering time, so we
# must not also do so, or it will be doubled, and the resulting links will be broken.
slug = mo.group("slug")
anchor = mo.group("anchor") or ""
title = self._slug_to_title.get(slug)
if not title:
raise ValueError(f"Found empty or no title for {mo.group(0)}")
return f"[{title}](doc:{slug})"
return f"[{title}](doc:{slug}{anchor})"

def rewrite(self, s: str) -> str:
return DOC_URL_RE.sub(self._rewrite_url, s)
Expand Down
12 changes: 8 additions & 4 deletions build-support/bin/generate_docs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ def test_gather_value_strs():
assert set(value_strs_iter(help_info)) == {"foo", "bar", "baz", "qux", "quux"}


@pytest.mark.parametrize("slug", ["foo-bar", "baz3", "qux"])
def test_slug_for_url(slug: str) -> None:
assert get_doc_slug(doc_url(slug)) == slug
@pytest.mark.parametrize("arg", ["foo-bar", "baz3", "qux#anchor"])
def test_slug_for_url(arg: str) -> None:
expected_slug = arg.split("#")[0]
assert get_doc_slug(doc_url(arg)) == expected_slug


def test_slug_for_url_error() -> None:
Expand Down Expand Up @@ -66,4 +67,7 @@ def test_doc_url_rewriter():
}
)
assert dur.rewrite(f"See {doc_url('foo')} for details.") == "See [Foo](doc:foo) for details."
assert dur.rewrite(f"Check out {doc_url('bar')}.") == "Check out [Welcome to Bar!](doc:bar)."
assert (
dur.rewrite(f"Check out {doc_url('bar#anchor')}.")
== "Check out [Welcome to Bar!](doc:bar#anchor)."
)