Skip to content
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
28 changes: 22 additions & 6 deletions scripts/build_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
robots.txt naming the sitemap. Each page shows the package's current notes from notes_dir (default: the repository's notes/) and,
when it has a gap, an "Add a note" link. A `bundled` cell says which version Pyodide ships
(from the committed lock files, see pyodide_locks.py). Runs at publish time; the files are not
committed. The pages are plain HTML (no JavaScript; one JSON-LD data block) and reuse site/style.css, so
committed. The pages are plain HTML (no JavaScript; JSON-LD data blocks) and reuse site/style.css, so
search engines can find "does <package> have ARM wheels".
"""

Expand Down Expand Up @@ -277,6 +277,10 @@ def json_ld(name: str, version: str, url: str, description: str) -> str:
}
if version:
data["version"] = version
return json_ld_block(data)


def json_ld_block(data: dict) -> str:
text = json.dumps(data, ensure_ascii=False, indent=2)
text = text.replace("<", "\\u003c").replace(">", "\\u003e").replace("&", "\\u0026")
return f' <script type="application/ld+json">\n{text}\n </script>\n'
Expand All @@ -303,7 +307,7 @@ def gap_platforms(package: dict, targets: list[dict], statuses: tuple[str, ...]

def answer_html(
name: str, version: str, python: str, example: str, platforms: list[str], *, builds: bool
) -> str:
) -> tuple[str, str]:
"""The short answer for someone who arrived with the install's error ("Failed building wheel for NAME", or pip's
"No matching distribution found" where there's no sdist to build): why it happens, where it does for this package
(`platforms`, on a regular Python), the ways out, and a command for `python` on `example`, a platform where that
Expand Down Expand Up @@ -340,7 +344,7 @@ def answer_html(
else:
tool_intro = "To find every dependency like this before you install, on the platforms you ship to:"
command = f"uvx wheelgap requirements.txt -p {python} -t {example}"
return (
section = (
' <section class="notes" id="why-it-fails">\n'
" <h2>Why the install fails, and the fix</h2>\n"
f" <p>{why}</p>\n"
Expand All @@ -349,6 +353,18 @@ def answer_html(
f" <pre><code>{e(command)}</code></pre>\n"
" </section>\n"
)
error = "Failed building wheel for" if builds else "No matching distribution found for"
question = f"Why does pip install {name} fail with '{error} {name}'?"
faq = {
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [{
"@type": "Question",
"name": question,
"acceptedAnswer": {"@type": "Answer", "text": html.unescape(f"{why} {ways}")},
}],
}
return section, json_ld_block(faq)


def add_note_url(name: str, platforms: list[str], today: str, exists: bool) -> str:
Expand Down Expand Up @@ -728,7 +744,7 @@ def page(
else:
title = f"{name} wheel coverage: Linux, Alpine, macOS, Windows, ARM, Pyodide"
heading = f"{name} wheel coverage"
answer = ""
answer = faq = ""
if cells:
# The example command names a Python and platform that really has the page's error: the Python the summary
# answers for when it has one, else the newest that does, and that Python's first such platform.
Expand All @@ -738,7 +754,7 @@ def page(
example_python = preferred if preferred in pythons else max(pythons, key=install_results.python_key)
example_platform = next(pl for pl in PLATFORM_NAMES if (example_python, pl) in kind)
where = [pl for pl in PLATFORM_NAMES if any(p == pl for _, p, _ in cells)]
answer = answer_html(name, version, example_python, example_platform, where, builds=builds)
answer, faq = answer_html(name, version, example_python, example_platform, where, builds=builds)
campaign_links = []
for status, campaign in campaigns or []:
link = f'<a href="../../campaigns/{e(campaign["id"])}/">{e(campaign["title"])}</a>'
Expand Down Expand Up @@ -875,7 +891,7 @@ def cell(py: str, pl: str) -> str:
<p class="sub"><a href="../../data/packages/{e(slug)}.json">Package JSON</a> · <a href="../../#package={e(name)}">All {len(scan["packages"]):,} packages</a></p>
</main>
{build_campaigns.FOOTER}
{json_ld(name, version, url, summary)}</body>
{json_ld(name, version, url, summary)}{faq}</body>
</html>
"""

Expand Down
4 changes: 2 additions & 2 deletions tests/test_notes_on_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ def test_current_note_is_shown_escaped_with_backticks_as_code(self):
"&lt;script&gt;alert(1)&lt;/script&gt; &amp; go.</p>",
page,
)
# The only script tag is the JSON-LD data block (#100), and the note's text isn't in one.
self.assertEqual(page.count("<script"), 1)
# Only structured data blocks may open a script element.
self.assertEqual(page.count("<script"), page.count('<script type="application/ld+json">'))
self.assertIn('<script type="application/ld+json">', page)
self.assertNotIn("<script>", page)
self.assertIn('<a href="https://example.com/docs?a=1&amp;b=2">example.com/docs?a=1&amp;b=2</a>', page)
Expand Down
27 changes: 24 additions & 3 deletions tests/test_package_jsonld.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Package pages carry schema.org structured data for search engines (#100)."""

import html
import json
import re

Expand Down Expand Up @@ -28,7 +29,7 @@ def build(data):

def test_fields_and_valid_json():
page = build(scan())
(block,) = blocks(page)
block = blocks(page)[0]
data = json.loads(block)
assert data == {
"@context": "https://schema.org",
Expand All @@ -46,6 +47,26 @@ def test_fields_and_valid_json():
assert "numpy 2.3.1 has a prebuilt wheel for 1 of 2 platforms" in data["description"]


def test_faq_matches_the_visible_answer_for_each_install_error():
for status, error in [("source", "Failed building wheel for"), ("missing", "No matching distribution found for")]:
page = _page(["wheel", status], ["3.13 linux-x86_64", "3.13 win-arm64"], name="a&b</script>")
software, faq = [json.loads(block) for block in blocks(page)]
assert software["@type"] == "SoftwareSourceCode"
assert faq["@type"] == "FAQPage"
question, = faq["mainEntity"]
assert question["@type"] == "Question"
assert question["name"] == f"Why does pip install a&b</script> fail with '{error} a&b</script>'?"
section = page.split('id="why-it-fails">', 1)[1].split("</section>", 1)[0]
paragraphs = re.findall(r"<p>(.*?)</p>", section)[:2]
assert question["acceptedAnswer"] == {"@type": "Answer", "text": html.unescape(" ".join(paragraphs))}
assert all("<" not in block for block in blocks(page))


def test_no_faq_on_fully_covered_package():
page = _page(["wheel", "wheel"], ["3.13 linux-x86_64", "3.13 win-arm64"])
assert [json.loads(block)["@type"] for block in blocks(page)] == ["SoftwareSourceCode"]


def test_open_graph_and_twitter_card_tags():
page = build(scan())

Expand Down Expand Up @@ -75,8 +96,8 @@ def test_a_script_end_tag_in_the_name_cannot_end_the_block():
name = "evil</script><script>alert(1)</script>&x"
page = build(scan(name=name))
assert "</script><script>alert" not in page
assert page.count("</script>") == 1 # only the block's own end tag
(block,) = blocks(page)
assert page.count("</script>") == 2 # only the two blocks' own end tags
block = blocks(page)[0]
assert "<" not in block and ">" not in block and "&" not in block
data = json.loads(block)
assert data["name"] == name
Expand Down