wikitext.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # SPDX-License-Identifier: Apache-2.0
  2. """Read a wiki page without its generated blocks (issue #417, #441, #521).
  3. Since issue #521 the page files carry no navigation block: the publish step adds the links to the page's track,
  4. its previous and next page and related pages. This helper still strips any such block (a branch from before the
  5. change may carry one) and the contents block that stays in the long pages. Navigation is not lesson content, so a test that
  6. counts the links a page makes, names pages it must NOT link, or looks at the first lines of a page reads the page
  7. through this helper and sees what the author wrote. Since issue #441 the longest pages also carry a generated
  8. contents block (links to the page's own headings); it is stripped too. The stripping itself is
  9. gen_wiki_nav.strip_generated, tested in tests/test_wiki_page_nav.py and tests/test_wiki_page_contents.py.
  10. """
  11. import re
  12. import sys
  13. from pathlib import Path
  14. sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))
  15. import gen_wiki_nav
  16. def page_text(path: Path) -> str:
  17. """The text of a wiki page file with the generated navigation blocks and the contents block removed."""
  18. return gen_wiki_nav.strip_generated(Path(path).read_text(encoding="utf-8"))
  19. def nav_text(wiki: Path) -> str:
  20. """The navigation manifest, docs/wiki/_nav/, as one text in the shape the single file used to have: the index head,
  21. then `pages:` with ` <group id>:` and its indented `- {page: ...}` lines in group order, then links, readers, goals."""
  22. nav = Path(wiki) / "_nav"
  23. index = (nav / "index.yaml").read_text(encoding="utf-8")
  24. ids = re.findall(r"^ - \{id: ([a-z0-9_-]+),", index, re.MULTILINE)
  25. head, sep, tail = index.partition("\nlinks:\n")
  26. out = [head, "pages:"]
  27. for gid in ids:
  28. out.append(f" {gid}:")
  29. file = nav / f"{gid}.yaml"
  30. if file.is_file():
  31. out += [f" {line}" for line in file.read_text(encoding="utf-8").splitlines() if line.startswith("- ")]
  32. return "\n".join(out) + "\n" + sep + tail
  33. def glossary_pages(wiki: Path) -> list[Path]:
  34. """The Glossary index, Glossary.md, then its letter pages, Glossary-<letters>.md, in name order (issue #562)."""
  35. wiki = Path(wiki)
  36. return [wiki / "Glossary.md", *sorted(wiki.glob("Glossary-*.md"))]
  37. def glossary_text(wiki: Path, *, strip: bool = False) -> str:
  38. """The whole Glossary as one text: the index and every letter page joined, so a test sees every term row."""
  39. read = page_text if strip else (lambda p: p.read_text(encoding="utf-8"))
  40. return "\n".join(read(p) for p in glossary_pages(wiki))
  41. # File names of every glossary page, for tests that skip the glossary when they count the pages that link a lesson.
  42. GLOSSARY_FILES = frozenset(p.name for p in glossary_pages(Path(__file__).resolve().parent.parent / "docs" / "wiki"))