diff --git a/scripts/sitegen/build.py b/scripts/sitegen/build.py index af3e2b5c1..937f8d231 100644 --- a/scripts/sitegen/build.py +++ b/scripts/sitegen/build.py @@ -52,7 +52,7 @@ from sitegen.dataexports import ( ) from sitegen.datapage import generate_data_page from sitegen.decorate import SEARCH_DEMOTED, front_matter, decorate_markdown_pages -from sitegen.descriptions import PAGE_DATA, PART_PAGES +from sitegen.descriptions import PAGE_DATA, PART_PAGES, TITLE_NOUNS, title_noun from sitegen.emulatorcards import ( ARCHIVE_RECIPES_PAGE, ROMSET_FAQ_PAGE, @@ -284,6 +284,8 @@ def _write_system_pages(run: _SiteRun) -> None: pages = system_pages(mfr, consoles, run.consumers, console_installs.get(slug)) for stem, (page, held) in pages.items(): PAGE_DATA[f"systems/{stem}.md"] = system_structured_data(stem, held) + TITLE_NOUNS[f"systems/{stem}.md"] = title_noun( + str(entry.get("name", "")) for entries in held.values() for entry in entries) if stem != slug: PART_PAGES.add(f"systems/{stem}.md") write_if_changed(str(docs / "systems" / f"{stem}.md"), page) diff --git a/scripts/sitegen/descriptions.py b/scripts/sitegen/descriptions.py index fb77a79e5..f49e5b96b 100644 --- a/scripts/sitegen/descriptions.py +++ b/scripts/sitegen/descriptions.py @@ -4,6 +4,8 @@ from __future__ import annotations import html import re +from collections import Counter +from collections.abc import Iterable from pathlib import Path from model import releaserecord @@ -47,13 +49,45 @@ def page_browser_title(relative: Path, title: str) -> str: has_files = PAGE_DATA.get(key, {}).get("hasPart") and key not in NOTHING_TO_FETCH # Lexaloffle, QEMU and ScummVM name a system page too. own = " emulator" if f"systems/{relative.name}" in PAGE_DATA else "" - return f"{title}{own} BIOS files" if has_files else f"{title} emulator" + return f"{title}{own} {TITLE_NOUNS.get(key, BIOS_FILES)}" if has_files else f"{title} emulator" if key.startswith("systems/"): + noun = TITLE_NOUNS.get(key, BIOS_FILES) maker, _, console = title.partition(": ") - return f"{console} BIOS files ({maker})" if console else f"{title} BIOS files" + return f"{console} {noun} ({maker})" if console else f"{title} {noun}" return title +BIOS_FILES = "BIOS files" + + +def file_kind(name: str) -> str: + """What a file is called by the people who look for it, read from its + name: a key set (prod.keys, aes_keys.txt, keys.bin), a firmware package + or flash image (PS3UPDAT.PUP, SystemUpdate_17559_USB.zip, dsi_nand.bin), + or a BIOS. Words are whole: keyboard.zip and keymap are no keys.""" + folded = Path(name).name.casefold() + words = set(re.split(r"[^a-z0-9]+", folded)) + if words & {"key", "keys"}: + return "keys" + if folded.endswith(".pup") or "update" in folded or words & {"firmware", "nand"}: + return "firmware" + return BIOS_FILES + + +def title_noun(names: Iterable[str]) -> str: + """The noun a page title gives its files: each kind holding at least a + quarter of the distinct names, keys first. A Switch emulator reads keys + and "eden keys" is the search; Vita3K reads firmware packages; one + rom.key beside nine Kickstarts leaves WinUAE's page "BIOS files". A + name pattern (.zip) is no file to fetch and counts for none.""" + distinct = {Path(name).name.casefold(): name for name in names if "<" not in name} + if not distinct: + return BIOS_FILES + counts = Counter(file_kind(name) for name in distinct.values()) + return said_as_list([kind for kind in ("keys", "firmware", BIOS_FILES) + if counts[kind] * 4 >= len(distinct)]) + + _LINK_TEXT = r"(?:`[^`]*`|\[[^\]\[]*\]|[^\]\[])" @@ -298,6 +332,9 @@ NOTHING_TO_FETCH: set[str] = set() # The first system, the file names, the count of required names and the # word for them ("BIOS" or "files") of each emulator page, for its snippet. EMULATOR_SNIPPETS: dict[str, tuple[str, list[str], int, str]] = {} +# The noun of the title of an emulator or system page, by page path +# relative to docs/ (title_noun): "BIOS files" when absent. +TITLE_NOUNS: dict[str, str] = {} # Pages that hold part of a page too long for one (a console of a split # manufacturer, a run of an emulator's cards), by path relative to docs/: # their H1 already says what they are. diff --git a/scripts/sitegen/structured.py b/scripts/sitegen/structured.py index 7e3c998ec..97541d50e 100644 --- a/scripts/sitegen/structured.py +++ b/scripts/sitegen/structured.py @@ -13,7 +13,7 @@ from sitegen import starlight as sitestarlight from sitegen.cards import emulator_file_anchors from sitegen.config import REPO_URL, SITE_NAME, SITE_URL from core.paths import REPO_ROOT -from sitegen.descriptions import EMULATOR_SNIPPETS, NOTHING_TO_FETCH, PAGE_DATA +from sitegen.descriptions import EMULATOR_SNIPPETS, NOTHING_TO_FETCH, PAGE_DATA, TITLE_NOUNS, title_noun from sitegen.emulatorcards import snippet_noun from sitegen.install import comes_with_app from sitegen.lookup import data_member_digests, sorted_hash_values @@ -316,6 +316,7 @@ def record_emulator_page(name: str, profile: dict) -> None: base = Path(str(f["name"])).name names.setdefault(base.casefold(), base) firmware = [f for f in fetched if f.get("category", "bios") in ("bios", "bios_zip")] + TITLE_NOUNS[key] = title_noun(str(f["name"]) for f in firmware) EMULATOR_SNIPPETS[key] = ( # One console names an emulator of five systems, not BizHawk of 40. system_name(first) if 0 < len(systems) <= 5 else "", diff --git a/tests/test_site_search.py b/tests/test_site_search.py index c9ddff1af..37b53f2d8 100644 --- a/tests/test_site_search.py +++ b/tests/test_site_search.py @@ -284,6 +284,47 @@ class Descriptions(unittest.TestCase): self.assertEqual(emulatorcards.snippet_noun(["gba_bios.bin", "gb_bios.bin"], 2, 2), "BIOS") self.assertEqual(emulatorcards.snippet_noun(["oot.z64", "oot-mq.z64", "font.bin"], 1, 3), "files") + def test_a_title_names_what_its_files_are(self) -> None: + """"eden keys" (1,125 impressions) and "ps vita firmware" landed on + pages titled "Eden BIOS files" and "Vita3K BIOS files".""" + noun = descriptions.title_noun + self.assertEqual(noun(["prod.keys", "title.keys", "console.keys", "key_retail.bin"]), "keys") + self.assertEqual(noun(["PSVUPDAT.PUP", "PSP2UPDAT.PUP", "font.pup"]), "firmware") + self.assertEqual(noun(["SystemUpdate_17559_USB.zip"]), "firmware") + self.assertEqual(noun(["aes_keys.txt", "boot9.bin", "shared_font.bin", "seeddb.bin"]), + "keys and BIOS files") + self.assertEqual(noun(["bios7.bin", "bios9.bin", "firmware.bin", "dsi_nand.bin"]), + "firmware and BIOS files") + self.assertEqual(noun(["gba_bios.bin", "gb_bios.bin"]), "BIOS files") + # One key beside Kickstarts, keyboards and keymaps name no keys. + self.assertEqual(noun([f"amiga-os-{n}.rom" for n in range(9)] + ["rom.key"]), "BIOS files") + self.assertEqual(noun(["archimedes_keyboard.zip", "keymap file", "at_keybc.zip"]), "BIOS files") + # A name pattern is no file to fetch. + self.assertEqual(noun(["prod.keys", ".zip", ".bin"]), "keys") + self.assertEqual(noun([]), "BIOS files") + + def test_titles_follow_the_profiles(self) -> None: + saved, nouns = dict(descriptions.PAGE_DATA), dict(descriptions.TITLE_NOUNS) + snippets, shipped = dict(descriptions.EMULATOR_SNIPPETS), set(descriptions.NOTHING_TO_FETCH) + self.addCleanup(lambda: ( + descriptions.PAGE_DATA.clear(), descriptions.PAGE_DATA.update(saved), + descriptions.TITLE_NOUNS.clear(), descriptions.TITLE_NOUNS.update(nouns), + descriptions.EMULATOR_SNIPPETS.clear(), descriptions.EMULATOR_SNIPPETS.update(snippets), + descriptions.NOTHING_TO_FETCH.clear(), descriptions.NOTHING_TO_FETCH.update(shipped))) + expected = {"eden": ("Eden", "Eden keys"), "vita3k": ("Vita3K", "Vita3K firmware"), + "rpcs3": ("RPCS3", "RPCS3 firmware"), "mgba": ("mGBA", "mGBA BIOS files"), + "citra": ("Citra", "Citra keys and BIOS files")} + for name, (title, browser) in expected.items(): + with open(REPO_ROOT / "emulators" / f"{name}.yml", encoding="utf-8") as handle: + structured.record_emulator_page(name, yaml.safe_load(handle)) + with self.subTest(emulator=name): + self.assertEqual(descriptions.page_browser_title(Path(f"emulators/{name}.md"), title), + browser) + descriptions.TITLE_NOUNS["systems/sony-playstation-vita.md"] = "firmware" + self.assertEqual(descriptions.page_browser_title( + Path("systems/sony-playstation-vita.md"), "Sony: PlayStation Vita"), + "PlayStation Vita firmware (Sony)") + def test_an_any_name_snippet_says_what_to_do(self) -> None: saved = dict(descriptions.PAGE_DATA) self.addCleanup(lambda: (descriptions.PAGE_DATA.clear(), descriptions.PAGE_DATA.update(saved)))