File size: 6,359 Bytes
005e9fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Fetch each book's repository and render it to markdown with mdBook.

mdBook defines these books' syntax, so we let it do the rendering. That resolves
the `{{#include}}` and `{{#rustdoc_include}}` directives exactly as the published
site does, and its markdown backend keeps the original fence attributes and the
`#` prefix on hidden lines.

We build each book with a minimal `book.toml` of our own rather than the
repository's, because theirs pull in preprocessors and link-checking backends that
need extra binaries and do not affect the text we want.

Requires the `mdbook` binary on PATH: https://rust-lang.github.io/mdBook/

    uv run python -m ingest.fetch_sources
    uv run python -m ingest.fetch_sources --book nomicon
    uv run python -m ingest.fetch_sources --refetch
"""

import argparse
import io
import json
import re
import shutil
import subprocess
import sys
import tarfile
from datetime import datetime, timezone
from pathlib import Path

import requests

from rag.config import BOOKS, MANIFEST_PATH, SOURCES_DIR, BookSpec
from rag.types import ManifestBook, Page

TARBALL_URL = "https://codeload.github.com/{repo}/tar.gz/{sha}"
REQUEST_TIMEOUT = 120

# Only what mdBook needs to render the text. `create-missing` is off to prevent
# generating unwritten chapters that might be referenced in SUMMARY.md.
BOOK_TOML = """[book]
src = "src"

[build]
create-missing = false

[output.markdown]
"""

# mdBook table of contents entries are indented markdown links, and the nesting is
# the topic structure. Numbered chapters are bullets, but prefix and suffix
# chapters carry none, so the marker is optional. Part headings are `# Title`
# lines between entries.
TOC_LINK = re.compile(r"^\s*(?:[-*]\s*)?\[(?P<title>[^\]]*)\]\((?P<href>[^)]*)\)")
PART_HEADING = re.compile(r"^#\s+(?P<title>.+?)\s*$")


def download_repo(session: requests.Session, spec: BookSpec, target: Path) -> None:
    response = session.get(
        TARBALL_URL.format(repo=spec.repo, sha=spec.sha), timeout=REQUEST_TIMEOUT
    )
    response.raise_for_status()
    with tarfile.open(fileobj=io.BytesIO(response.content), mode="r:gz") as archive:
        root = archive.getnames()[0].split("/")[0]
        archive.extractall(target.parent, filter="data")
    if target.exists():
        shutil.rmtree(target)
    (target.parent / root).rename(target)


def render_markdown(repo_dir: Path, out_dir: Path) -> int:
    if shutil.which("mdbook") is None:
        raise RuntimeError(
            "mdbook not found on PATH. Install it from https://rust-lang.github.io/mdBook/"
        )
    (repo_dir / "book.toml").write_text(BOOK_TOML)
    result = subprocess.run(
        ["mdbook", "build", "-d", str(out_dir)], cwd=repo_dir, capture_output=True, text=True
    )
    if result.returncode != 0:
        raise RuntimeError(f"mdbook build failed for {repo_dir.name}:\n{result.stderr.strip()}")

    # The markdown backend renders chapters only. SUMMARY.md is its input, and it is what gives us titles, ordering and part headings.
    shutil.copyfile(repo_dir / "src" / "SUMMARY.md", out_dir / "SUMMARY.md")
    return len(list(out_dir.rglob("*.md")))


def parse_summary(text: str, book: str, spec: BookSpec) -> list[Page]:
    pages: list[Page] = []
    part: str | None = None
    seen: set[str] = set()

    for line in text.splitlines():
        heading = PART_HEADING.match(line)
        if heading:
            part = heading.group("title")
            continue

        match = TOC_LINK.match(line)
        if not match:
            continue
        href = match.group("href").strip()
        if not href or part in spec.exclude_parts:
            continue

        # nomicon writes 19 of its entries as "./vec/vec-drain.md"
        path = href.split("#")[0].removeprefix("./")
        if not path.endswith(".md") or path in seen or path in spec.exclude_paths:
            continue
        seen.add(path)

        pages.append(
            {
                "book": book,
                "path": path,
                "title": match.group("title").strip(),
                "part": part,
                "url": f"{spec.base_url}{path[: -len('.md')]}.html",
            }
        )
    return pages


def main(argv: list[str]) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--book", action="append", choices=sorted(BOOKS))
    parser.add_argument("--refetch", action="store_true", help="re-download and rebuild")
    args = parser.parse_args(argv[1:])

    fetched_at = datetime.now(timezone.utc).isoformat(timespec="seconds")
    books: dict[str, ManifestBook] = {}
    if MANIFEST_PATH.exists():
        books.update(json.loads(MANIFEST_PATH.read_text()).get("books", {}))

    session = requests.Session()
    session.headers["User-Agent"] = "rust-docs-rag ingestion (rust-lang docs, educational use)"

    for book in args.book or sorted(BOOKS):
        spec = BOOKS[book]
        out_dir, repo_dir = SOURCES_DIR / book, SOURCES_DIR / f"{book}.repo"
        print(f"{spec.title} ({spec.repo} @ {spec.sha})")

        if out_dir.exists() and not args.refetch:
            print("  cached")
        else:
            out_dir.parent.mkdir(parents=True, exist_ok=True)
            download_repo(session, spec, repo_dir)
            print(f"  rendered {render_markdown(repo_dir, out_dir)} markdown files")
            shutil.rmtree(repo_dir)

        pages = parse_summary((out_dir / "SUMMARY.md").read_text(encoding="utf-8"), book, spec)
        present = [p for p in pages if (out_dir / p["path"]).exists()]
        print(f"  {len(pages)} pages in SUMMARY.md, {len(pages) - len(present)} not rendered")

        books[book] = {
            "title": spec.title,
            "repo": spec.repo,
            "sha": spec.sha,
            "base_url": spec.base_url,
            "license": spec.license,
            "excluded_parts": list(spec.exclude_parts),
            "page_count": len(present),
            "pages": present,
        }

    MANIFEST_PATH.parent.mkdir(parents=True, exist_ok=True)
    manifest = {"fetched_at": fetched_at, "books": books}
    MANIFEST_PATH.write_text(json.dumps(manifest, indent=2) + "\n")
    total = sum(b["page_count"] for b in books.values())
    print(f"\n{total} pages across {len(books)} books -> {MANIFEST_PATH.name}")
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))