cppmanlite 0.1.0__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cppmanlite
3
- Version: 0.1.0
3
+ Version: 0.1.6
4
4
  Summary: Lightweight serverless C++ documentation lookup — pure Python, no C deps
5
5
  License-Expression: MIT
6
6
  Project-URL: Homepage, https://dive4dec.github.io/cppmanlite/
@@ -0,0 +1,498 @@
1
+ """Core search and display logic for cppmanlite.
2
+
3
+ No external dependencies — pure stdlib. Fetches pages on-demand from
4
+ cppreference.com when not bundled locally.
5
+
6
+ Works in CPython, Jupyter, and Pyodide (browser). In Pyodide, network
7
+ fetches use the browser's Fetch API via pyodide.http instead of urllib.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import html
13
+ import json
14
+ import re
15
+ import textwrap
16
+ from pathlib import Path
17
+ from typing import Any
18
+
19
+ # --------------------------------------------------------------------------- #
20
+ # Environment detection
21
+ # --------------------------------------------------------------------------- #
22
+
23
+ def _detect_pyodide() -> bool:
24
+ """Return True if running under Pyodide."""
25
+ try:
26
+ import sys
27
+ return "pyodide" in sys.modules or "emscripten" in getattr(sys, "platform", "")
28
+ except Exception:
29
+ return False
30
+
31
+
32
+ _IS_PYODIDE = _detect_pyodide()
33
+
34
+
35
+ # --------------------------------------------------------------------------- #
36
+ # Network fetch — urllib in CPython, pyodide.http in Pyodide
37
+ # --------------------------------------------------------------------------- #
38
+
39
+ def _fetch_urllib(url: str, timeout: int) -> str:
40
+ import urllib.request
41
+ req = urllib.request.Request(url, headers={"User-Agent": "cppmanlite/0.1"})
42
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
43
+ return resp.read().decode("utf-8", errors="replace")
44
+
45
+
46
+ async def _fetch_pyodide(url: str) -> str:
47
+ """Fetch via pyodide.http.pyfetch (async)."""
48
+ from pyodide.http import pyfetch
49
+ resp = await pyfetch(url, headers={"User-Agent": "cppmanlite/0.1"})
50
+ return await resp.string()
51
+
52
+
53
+ # --------------------------------------------------------------------------- #
54
+ # Index management
55
+ # --------------------------------------------------------------------------- #
56
+
57
+ _INDEX: list[dict[str, str]] = []
58
+ _INDEX_PATH = Path(__file__).parent / "data" / "index.json"
59
+
60
+ # When running in Pyodide the bundled index.json ships inside the wheel;
61
+ # when running in CPython without the bundle, fetch from GitHub Pages.
62
+ _INDEX_FALLBACK_URL = "https://dive4dec.github.io/cppmanlite/index.json"
63
+
64
+ # cppreference page base URL (redirects /w/cpp/... → /cpp/...)
65
+ _PAGE_BASE = "https://en.cppreference.com/w"
66
+
67
+ # GitHub Pages mirror — used as fallback in Pyodide (browser CORS blocks
68
+ # direct fetches to en.cppreference.com which doesn't send CORS headers).
69
+ _PAGES_MIRROR = "https://dive4dec.github.io/cppmanlite"
70
+
71
+
72
+ def _load_index() -> list[dict[str, str]]:
73
+ """Load the search index, fetching it if necessary."""
74
+ global _INDEX
75
+ if _INDEX:
76
+ return _INDEX
77
+ if _INDEX_PATH.exists():
78
+ with open(_INDEX_PATH, encoding="utf-8") as f:
79
+ _INDEX = json.load(f)
80
+ else:
81
+ # Fetch from GitHub Pages (works in both CPython and Pyodide)
82
+ try:
83
+ _INDEX = json.loads(_fetch_url_sync(_INDEX_FALLBACK_URL))
84
+ except Exception:
85
+ _INDEX = []
86
+ return _INDEX
87
+
88
+
89
+ def _fetch_url_sync(url: str) -> str:
90
+ """Synchronous fetch for index loading — blocks in CPython, raises in Pyodide.
91
+
92
+ In Pyodide, index should be bundled in the wheel so this is never called.
93
+ If it is, we try asyncio.run as a fallback.
94
+ """
95
+ if _IS_PYODIDE:
96
+ import asyncio
97
+ return asyncio.run(_fetch_pyodide(url))
98
+ return _fetch_urllib(url, 15)
99
+
100
+
101
+ # --------------------------------------------------------------------------- #
102
+ # Search
103
+ # --------------------------------------------------------------------------- #
104
+
105
+
106
+ def search(query: str, limit: int = 20) -> list[dict[str, str]]:
107
+ """Search C++ documentation pages.
108
+
109
+ Args:
110
+ query: Search term (e.g. "vector", "std::sort", "shared_ptr").
111
+ limit: Maximum number of results.
112
+
113
+ Returns:
114
+ List of dicts with keys: title, url, snippet.
115
+ """
116
+ idx = _load_index()
117
+ if not idx:
118
+ return []
119
+ q = query.lower().strip()
120
+ # Normalise std:: prefix
121
+ q_norm = re.sub(r"^std::", "", q)
122
+ results = []
123
+ for entry in idx:
124
+ title = entry.get("title", "").lower()
125
+ url = entry.get("url", "").lower()
126
+ # Score: exact match > starts with > contains in title > contains in URL
127
+ score = 0
128
+ if title == q or title == q_norm:
129
+ score = 100
130
+ elif title.startswith(q) or title.startswith(q_norm):
131
+ score = 80
132
+ elif q in title or q_norm in title:
133
+ score = 60
134
+ elif q in url or q_norm in url:
135
+ score = 40
136
+ if score > 0:
137
+ results.append({**entry, "_score": score})
138
+ results.sort(key=lambda x: (-x["_score"], x.get("title", "")))
139
+ return [{k: v for k, v in r.items() if k != "_score"} for r in results[:limit]]
140
+
141
+
142
+ def list_pages(limit: int = 0) -> list[dict[str, str]]:
143
+ """List all indexed pages (for debugging/browsing)."""
144
+ idx = _load_index()
145
+ return idx if limit == 0 else idx[:limit]
146
+
147
+
148
+ # --------------------------------------------------------------------------- #
149
+ # Page fetching and rendering
150
+ # --------------------------------------------------------------------------- #
151
+
152
+ _CONTENT_RE = re.compile(
153
+ r'<div class="mw-content-ltr mw-parser-output"[^>]*>(.*?)(?:</div>\s*<!--|\Z)',
154
+ re.DOTALL,
155
+ )
156
+ _SCRIPT_RE = re.compile(r"<script[^>]*>.*?</script>", re.DOTALL)
157
+ _STYLE_RE = re.compile(r"<style[^>]*>.*?</style>", re.DOTALL)
158
+ _COMMENT_RE = re.compile(r"<!--.*?-->", re.DOTALL)
159
+ _EDIT_RE = re.compile(r'<span class="(?:mw-)?editsection[^"]*">.*?</span>', re.DOTALL)
160
+
161
+ # Strip cppreference navigation chrome (t-navbar has nested divs — match
162
+ # the outermost by greedy-matching to the closing </div> that is followed
163
+ # by a non-navbar block element or end-of-string).
164
+ _NAVBAR_RE = re.compile(
165
+ r'<div class="t-navbar"[^>]*>.*?(?:</div>\s*(?=<div|<h[1-6]|<table|<p|\Z))',
166
+ re.DOTALL,
167
+ )
168
+ _NV_TABLE_RE = re.compile(r'<table class="t-nv-begin"[^>]*>.*?</table>', re.DOTALL)
169
+
170
+
171
+ def _fetch_page_sync(url: str) -> str:
172
+ """Synchronous page fetch (CPython only)."""
173
+ full_url = f"{_PAGE_BASE}/{url}" if not url.startswith("http") else url
174
+ html_raw = _fetch_urllib(full_url, 15)
175
+ return _clean_page_html(html_raw)
176
+
177
+
178
+ async def _fetch_page_async(url: str) -> str:
179
+ """Async page fetch (Pyodide). Tries cppreference.com first, then
180
+ falls back to the GitHub Pages mirror (CORS-safe)."""
181
+ full_url = f"{_PAGE_BASE}/{url}" if not url.startswith("http") else url
182
+ try:
183
+ html_raw = await _fetch_pyodide(full_url)
184
+ return _clean_page_html(html_raw)
185
+ except Exception:
186
+ # CORS or network error — fall back to GitHub Pages mirror.
187
+ # Mirror pages are pre-stripped HTML (no #mw-content-text wrapper),
188
+ # so we clean them differently.
189
+ mirror_url = f"{_PAGES_MIRROR}/docs/{url}"
190
+ html_raw = await _fetch_pyodide(mirror_url)
191
+ return _clean_mirror_html(html_raw)
192
+
193
+
194
+ def _clean_page_html(html_raw: str) -> str:
195
+ """Extract and clean the main content from a cppreference page."""
196
+ m = _CONTENT_RE.search(html_raw)
197
+ if not m:
198
+ return "<p>Could not extract page content.</p>"
199
+ content = m.group(1)
200
+ # Clean up
201
+ content = _SCRIPT_RE.sub("", content)
202
+ content = _STYLE_RE.sub("", content)
203
+ content = _COMMENT_RE.sub("", content)
204
+ content = _EDIT_RE.sub("", content)
205
+ # Strip any residual [edit] markers (from &#91;edit&#93; entities)
206
+ content = re.sub(r"&#91;edit&#93;", "", content)
207
+ content = re.sub(r"\[edit\]", "", content)
208
+ # Strip cppreference navigation chrome (t-navbar, t-nv-begin tables)
209
+ content = _NAVBAR_RE.sub("", content)
210
+ content = _NV_TABLE_RE.sub("", content)
211
+ # Fix relative URLs
212
+ content = re.sub(r'href="/w/', 'href="https://en.cppreference.com/w/', content)
213
+ content = re.sub(r'src="/', 'src="https://en.cppreference.com/', content)
214
+ return content
215
+
216
+
217
+ def _clean_mirror_html(html_raw: str) -> str:
218
+ """Clean a pre-stripped page from the GitHub Pages mirror.
219
+
220
+ Mirror pages are raw HTML from the cppreference archive — they don't
221
+ have the #mw-content-text wrapper, but they do have t-navbar and
222
+ t-nv-begin tables that need stripping.
223
+ """
224
+ content = html_raw
225
+ content = _SCRIPT_RE.sub("", content)
226
+ content = _STYLE_RE.sub("", content)
227
+ content = _COMMENT_RE.sub("", content)
228
+ content = _EDIT_RE.sub("", content)
229
+ content = re.sub(r"&#91;edit&#93;", "", content)
230
+ content = re.sub(r"\[edit\]", "", content)
231
+ content = _NAVBAR_RE.sub("", content)
232
+ content = _NV_TABLE_RE.sub("", content)
233
+ # Fix relative URLs in archive pages (../../cpp/... → /w/cpp/...)
234
+ content = re.sub(
235
+ r'href="(\.\./)*([^"]+\.html)"',
236
+ lambda m: f'href="https://en.cppreference.com/w/{m.group(2)}"',
237
+ content,
238
+ )
239
+ return content
240
+
241
+
242
+ # --------------------------------------------------------------------------- #
243
+ # HTML → plain-text conversion (for terminal output)
244
+ # --------------------------------------------------------------------------- #
245
+
246
+ # Tags that should produce a line break
247
+ _BLOCK_TAGS = {"p", "div", "br", "tr", "li", "h1", "h2", "h3", "h4", "h5", "h6",
248
+ "hr", "table", "ul", "ol", "pre", "blockquote", "section"}
249
+
250
+
251
+ def _html_to_text(html_str: str, width: int = 80) -> str:
252
+ """Convert HTML to readable plain text with proper line breaks."""
253
+ # NB: strip HTML tags BEFORE decoding entities, otherwise
254
+ # &lt;class T&gt; becomes <class T> and gets eaten as a fake tag.
255
+
256
+ text = html_str
257
+
258
+ # Replace block-level tags with newlines (before stripping all tags)
259
+ for tag in _BLOCK_TAGS:
260
+ text = re.sub(rf"<{tag}[^>]*>", "\n", text, flags=re.IGNORECASE)
261
+ text = re.sub(rf"</{tag}>", "\n", text, flags=re.IGNORECASE)
262
+
263
+ # <td> / <th> → tab separator
264
+ text = re.sub(r"<t[dh][^>]*>", "\t", text, flags=re.IGNORECASE)
265
+ text = re.sub(r"</t[dh]>", "", text, flags=re.IGNORECASE)
266
+
267
+ # <code> / <tt> → backtick wrapping (strip the tag, keep content)
268
+ text = re.sub(r"<code[^>]*>", "`", text, flags=re.IGNORECASE)
269
+ text = re.sub(r"</code>", "`", text, flags=re.IGNORECASE)
270
+ text = re.sub(r"<tt[^>]*>", "`", text, flags=re.IGNORECASE)
271
+ text = re.sub(r"</tt>", "`", text, flags=re.IGNORECASE)
272
+
273
+ # <b> / <strong> → ** (bold marker)
274
+ text = re.sub(r"<b[^>]*>", "**", text, flags=re.IGNORECASE)
275
+ text = re.sub(r"</b>", "**", text, flags=re.IGNORECASE)
276
+ text = re.sub(r"<strong[^>]*>", "**", text, flags=re.IGNORECASE)
277
+ text = re.sub(r"</strong>", "**", text, flags=re.IGNORECASE)
278
+
279
+ # <i> / <em> → * (italic marker)
280
+ text = re.sub(r"<i[^>]*>", "*", text, flags=re.IGNORECASE)
281
+ text = re.sub(r"</i>", "*", text, flags=re.IGNORECASE)
282
+ text = re.sub(r"<em[^>]*>", "*", text, flags=re.IGNORECASE)
283
+ text = re.sub(r"</em>", "*", text, flags=re.IGNORECASE)
284
+
285
+ # Strip all remaining tags
286
+ text = re.sub(r"<[^>]+>", "", text)
287
+
288
+ # NOW decode entities (safe — no more HTML tags to confuse)
289
+ text = html.unescape(text)
290
+
291
+ # Process line by line
292
+ lines = text.split("\n")
293
+ result = []
294
+ for line in lines:
295
+ # Expand tabs to 4 spaces
296
+ line = line.expandtabs(4)
297
+ # Collapse multiple spaces (but preserve indentation)
298
+ stripped = line.lstrip()
299
+ indent = line[: len(line) - len(stripped)]
300
+ stripped = re.sub(r" +", " ", stripped).strip()
301
+ if stripped:
302
+ # Wrap long lines
303
+ wrapped = textwrap.fill(stripped, width=width,
304
+ initial_indent=indent,
305
+ subsequent_indent=indent + " ")
306
+ result.append(wrapped)
307
+ elif result and result[-1]: # preserve blank lines between content
308
+ result.append("")
309
+
310
+ # Remove leading/trailing blank lines
311
+ while result and not result[0]:
312
+ result.pop(0)
313
+ while result and not result[-1]:
314
+ result.pop()
315
+
316
+ return "\n".join(result)
317
+
318
+
319
+ # --------------------------------------------------------------------------- #
320
+ # Display
321
+ # --------------------------------------------------------------------------- #
322
+
323
+ def _is_jupyter() -> bool:
324
+ try:
325
+ from IPython.display import HTML, display # noqa: F401
326
+
327
+ get_ipython # type: ignore[name-defined]
328
+ return True
329
+ except Exception:
330
+ return False
331
+
332
+
333
+ def _format_search_html(results: list[dict[str, str]]) -> str:
334
+ rows = []
335
+ for r in results:
336
+ title = html.escape(r.get("title", ""))
337
+ url = html.escape(r.get("url", ""))
338
+ snippet = html.escape(r.get("snippet", ""))[:120]
339
+ rows.append(
340
+ f'<tr><td><a href="https://en.cppreference.com/w/{url}" '
341
+ f'target="_blank">{title}</a></td>'
342
+ f'<td><code>{snippet}</code></td></tr>'
343
+ )
344
+ return (
345
+ '<table style="font-size:14px;border-collapse:collapse">'
346
+ "<tr><th>Title</th><th>Path</th></tr>"
347
+ + "\n".join(rows)
348
+ + "</table>"
349
+ )
350
+
351
+
352
+ def _format_page_html(content: str, page_url: str = "") -> str:
353
+ """Format page content for Jupyter display with working links.
354
+
355
+ In a **trusted** notebook, Jupyter renders external https:// links
356
+ with target=\"_blank\" — clicking opens cppreference.com in a new tab.
357
+ In an untrusted notebook, the sanitizer strips external hrefs to '#'.
358
+
359
+ Links:
360
+ - Navigation links → absolute https://en.cppreference.com/w/... + target=\"_blank\"
361
+ - Edit links (<a href=\".../index.php?...action=edit\">) → unwrapped (text kept, <a> removed)
362
+ - Anchor links (href=\"#...\") → left as-is (in-page navigation)
363
+ """
364
+ from urllib.parse import urljoin
365
+
366
+ base_href = (
367
+ f"https://en.cppreference.com/w/{page_url}" if page_url
368
+ else "https://en.cppreference.com/w/"
369
+ )
370
+
371
+ # 1. Remove edit links entirely: <a ... href=".../index.php?...action=edit...">text</a> → text
372
+ content = re.sub(
373
+ r'<a [^>]*href="[^"]*index\.php[^"]*action=edit[^"]*"[^>]*>(.*?)</a>',
374
+ r'\1',
375
+ content,
376
+ flags=re.DOTALL,
377
+ )
378
+
379
+ # 2. Rewrite remaining hrefs
380
+ def _rewrite_href(m: re.Match) -> str:
381
+ href = m.group(1)
382
+
383
+ # Skip javascript: URLs — neutralize
384
+ if href.startswith("javascript:"):
385
+ return 'href="#" onclick="return false"'
386
+
387
+ # Skip anchors (in-page navigation) — keep as-is
388
+ if href.startswith("#"):
389
+ return f'href="{href}"'
390
+
391
+ # Resolve to absolute URL if needed
392
+ if not href.startswith("http://") and not href.startswith("https://"):
393
+ href = urljoin(base_href, href)
394
+
395
+ return f'href="{html.escape(href)}" target="_blank" rel="noopener"'
396
+
397
+ content = re.sub(r'href="([^"]*)"', _rewrite_href, content)
398
+
399
+ return (
400
+ '<div class="cppmanlite-content" '
401
+ 'style="max-height:600px;overflow:auto;'
402
+ 'border:1px solid #ddd;padding:16px;font-size:14px">'
403
+ + content
404
+ + '</div>'
405
+ + '<p style="font-size:11px;color:#888;margin-top:4px">'
406
+ + 'Links open cppreference.com in a new tab. '
407
+ + 'If links don\'t work, trust this notebook: '
408
+ + '<b>File → Trust Notebook</b>'
409
+ + '</p>'
410
+ )
411
+
412
+
413
+ # --------------------------------------------------------------------------- #
414
+ # Public API
415
+ # --------------------------------------------------------------------------- #
416
+
417
+
418
+ def man(query: str) -> Any:
419
+ """Display a C++ documentation page (like ``man`` for C++).
420
+
421
+ In Jupyter: renders HTML inline.
422
+ In terminal: prints formatted plain text.
423
+ In Pyodide: returns a coroutine (auto-awaited by the Pyodide REPL).
424
+
425
+ Args:
426
+ query: Page title or URL path (e.g. "std::vector" or "cpp/container/vector").
427
+ """
428
+ if _IS_PYODIDE:
429
+ return _man_async(query)
430
+ return _man_sync(query)
431
+
432
+
433
+ def _man_sync(query: str) -> None:
434
+ """Synchronous man() for CPython / terminal."""
435
+ results = search(query, limit=1)
436
+ if not results:
437
+ msg = f"No documentation found for '{query}'."
438
+ if _is_jupyter():
439
+ from IPython.display import HTML, display
440
+
441
+ display(HTML(f"<p>{html.escape(msg)}</p>"))
442
+ print(msg)
443
+ return
444
+ url = results[0]["url"]
445
+ title = results[0]["title"]
446
+ content = _fetch_page_sync(url)
447
+ if _is_jupyter():
448
+ from IPython.display import HTML, display
449
+
450
+ display(HTML(_format_page_html(content, page_url=url)))
451
+ else:
452
+ # Terminal: print formatted text
453
+ print(f"\n{'=' * 80}\n{title}\n{'=' * 80}\n")
454
+ print(_html_to_text(content, width=80))
455
+
456
+
457
+ async def _man_async(query: str) -> None:
458
+ """Async man() for Pyodide."""
459
+ results = search(query, limit=1)
460
+ if not results:
461
+ print(f"No documentation found for '{query}'.")
462
+ return
463
+ url = results[0]["url"]
464
+ title = results[0]["title"]
465
+ content = await _fetch_page_async(url)
466
+ if _is_jupyter():
467
+ from IPython.display import HTML, display
468
+
469
+ display(HTML(_format_page_html(content, page_url=url)))
470
+ else:
471
+ # Pyodide console / terminal
472
+ print(f"\n{'=' * 80}\n{title}\n{'=' * 80}\n")
473
+ print(_html_to_text(content, width=80))
474
+
475
+
476
+ def help(query: str) -> Any:
477
+ """Search C++ documentation (alias for :func:`search`).
478
+
479
+ Args:
480
+ query: Search term.
481
+ """
482
+ return search(query)
483
+
484
+
485
+ def refresh_index() -> int:
486
+ """Re-download the search index from GitHub Pages.
487
+
488
+ Returns the number of indexed pages.
489
+ """
490
+ global _INDEX
491
+ _INDEX = []
492
+ url = "https://dive4dec.github.io/cppmanlite/index.json"
493
+ _INDEX = json.loads(_fetch_url_sync(url))
494
+ return len(_INDEX)
495
+
496
+
497
+ # Re-export ``help`` under a safe alias to avoid shadowing builtin
498
+ help_query = help
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cppmanlite
3
- Version: 0.1.0
3
+ Version: 0.1.6
4
4
  Summary: Lightweight serverless C++ documentation lookup — pure Python, no C deps
5
5
  License-Expression: MIT
6
6
  Project-URL: Homepage, https://dive4dec.github.io/cppmanlite/
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cppmanlite"
7
- version = "0.1.0"
7
+ version = "0.1.6"
8
8
  description = "Lightweight serverless C++ documentation lookup — pure Python, no C deps"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -1,288 +0,0 @@
1
- """Core search and display logic for cppmanlite.
2
-
3
- No external dependencies — pure stdlib. Fetches pages on-demand from
4
- cppreference.com when not bundled locally.
5
-
6
- Works in CPython, Jupyter, and Pyodide (browser). In Pyodide, network
7
- fetches use the browser's Fetch API via pyodide.http instead of urllib.
8
- """
9
-
10
- from __future__ import annotations
11
-
12
- import html
13
- import json
14
- import re
15
- from pathlib import Path
16
- from typing import Any
17
-
18
- # --------------------------------------------------------------------------- #
19
- # Environment detection
20
- # --------------------------------------------------------------------------- #
21
-
22
- def _detect_pyodide() -> bool:
23
- """Return True if running under Pyodide."""
24
- try:
25
- import sys
26
- return "pyodide" in sys.modules or "pyodide" in getattr(sys, "platform", "")
27
- except Exception:
28
- return False
29
-
30
-
31
- _IS_PYODIDE = _detect_pyodide()
32
-
33
-
34
- # --------------------------------------------------------------------------- #
35
- # Network fetch — urllib in CPython, pyodide.http in Pyodide
36
- # --------------------------------------------------------------------------- #
37
-
38
- def _fetch_url(url: str, timeout: int = 15) -> str:
39
- """Fetch a URL and return text. Uses urllib (CPython) or pyfetch (Pyodide)."""
40
- if _IS_PYODIDE:
41
- return _fetch_pyodide(url)
42
- return _fetch_urllib(url, timeout)
43
-
44
-
45
- def _fetch_urllib(url: str, timeout: int) -> str:
46
- import urllib.request
47
- req = urllib.request.Request(url, headers={"User-Agent": "cppmanlite/0.1"})
48
- with urllib.request.urlopen(req, timeout=timeout) as resp:
49
- return resp.read().decode("utf-8", errors="replace")
50
-
51
-
52
- def _fetch_pyodide(url: str) -> str:
53
- """Fetch via pyodide.http.pyfetch (async under the hood, but Pyodide
54
- auto-awaits top-level coroutines)."""
55
- from pyodide.http import pyfetch
56
- resp = pyfetch(url, headers={"User-Agent": "cppmanlite/0.1"})
57
- return resp.string
58
-
59
-
60
- # --------------------------------------------------------------------------- #
61
- # Index management
62
- # --------------------------------------------------------------------------- #
63
-
64
- _INDEX: list[dict[str, str]] = []
65
- _INDEX_PATH = Path(__file__).parent / "data" / "index.json"
66
-
67
- # When running in Pyodide the bundled index.json ships inside the wheel;
68
- # when running in CPython without the bundle, fetch from GitHub Pages.
69
- _INDEX_FALLBACK_URL = "https://dive4dec.github.io/cppmanlite/index.json"
70
-
71
- # cppreference page base URL (redirects /w/cpp/... → /cpp/...)
72
- _PAGE_BASE = "https://en.cppreference.com/w"
73
-
74
-
75
- def _load_index() -> list[dict[str, str]]:
76
- """Load the search index, fetching it if necessary."""
77
- global _INDEX
78
- if _INDEX:
79
- return _INDEX
80
- if _INDEX_PATH.exists():
81
- with open(_INDEX_PATH, encoding="utf-8") as f:
82
- _INDEX = json.load(f)
83
- else:
84
- # Fetch from GitHub Pages (works in both CPython and Pyodide)
85
- try:
86
- _INDEX = json.loads(_fetch_url(_INDEX_FALLBACK_URL))
87
- except Exception:
88
- _INDEX = []
89
- return _INDEX
90
-
91
-
92
- # ---------------------------------------------------------------------------
93
- # Search
94
- # ---------------------------------------------------------------------------
95
-
96
-
97
- def search(query: str, limit: int = 20) -> list[dict[str, str]]:
98
- """Search C++ documentation pages.
99
-
100
- Args:
101
- query: Search term (e.g. "vector", "std::sort", "shared_ptr").
102
- limit: Maximum number of results.
103
-
104
- Returns:
105
- List of dicts with keys: title, url, snippet.
106
- """
107
- idx = _load_index()
108
- if not idx:
109
- return []
110
- q = query.lower().strip()
111
- # Normalise std:: prefix
112
- q_norm = re.sub(r"^std::", "", q)
113
- results = []
114
- for entry in idx:
115
- title = entry.get("title", "").lower()
116
- url = entry.get("url", "").lower()
117
- # Score: exact match > starts with > contains in title > contains in URL
118
- score = 0
119
- if title == q or title == q_norm:
120
- score = 100
121
- elif title.startswith(q) or title.startswith(q_norm):
122
- score = 80
123
- elif q in title or q_norm in title:
124
- score = 60
125
- elif q in url or q_norm in url:
126
- score = 40
127
- if score > 0:
128
- results.append({**entry, "_score": score})
129
- results.sort(key=lambda x: (-x["_score"], x.get("title", "")))
130
- return [{k: v for k, v in r.items() if k != "_score"} for r in results[:limit]]
131
-
132
-
133
- def list_pages(limit: int = 0) -> list[dict[str, str]]:
134
- """List all indexed pages (for debugging/browsing)."""
135
- idx = _load_index()
136
- return idx if limit == 0 else idx[:limit]
137
-
138
-
139
- # ---------------------------------------------------------------------------
140
- # Page fetching and rendering
141
- # ---------------------------------------------------------------------------
142
-
143
- _CONTENT_RE = re.compile(
144
- r'<div id="mw-content-text"[^>]*>(.*?)(?:</div>\s*<!--|\Z)',
145
- re.DOTALL,
146
- )
147
- _SCRIPT_RE = re.compile(r"<script[^>]*>.*?</script>", re.DOTALL)
148
- _STYLE_RE = re.compile(r"<style[^>]*>.*?</style>", re.DOTALL)
149
- _COMMENT_RE = re.compile(r"<!--.*?-->", re.DOTALL)
150
- _EDIT_RE = re.compile(r'<span class="mw-editsection">.*?</span>', re.DOTALL)
151
-
152
-
153
- _NAVBAR_RE = re.compile(
154
- r'<div class="t-navbar"[^>]*>.*?(?:</div>\s*(?=<div|<h[1-6]|<table|<p|\Z))',
155
- re.DOTALL,
156
- )
157
- _NV_TABLE_RE = re.compile(r'<table class="t-nv-begin"[^>]*>.*?</table>', re.DOTALL)
158
-
159
-
160
- def _fetch_page(url: str) -> str:
161
- """Fetch a cppreference page and extract the main content HTML."""
162
- full_url = f"{_PAGE_BASE}/{url}" if not url.startswith("http") else url
163
- html_raw = _fetch_url(full_url)
164
- # Extract #mw-content-text
165
- m = _CONTENT_RE.search(html_raw)
166
- if not m:
167
- return "<p>Could not extract page content.</p>"
168
- content = m.group(1)
169
- # Clean up
170
- content = _SCRIPT_RE.sub("", content)
171
- content = _STYLE_RE.sub("", content)
172
- content = _COMMENT_RE.sub("", content)
173
- content = _EDIT_RE.sub("", content)
174
- # Strip residual [edit] markers left by mw-editsection removal
175
- content = re.sub(r"\[edit\]", "", content)
176
- # Strip cppreference navigation chrome (t-navbar, t-nv-begin tables)
177
- content = _NAVBAR_RE.sub("", content)
178
- content = _NV_TABLE_RE.sub("", content)
179
- # Fix relative URLs
180
- content = re.sub(r'href="/w/', 'href="https://en.cppreference.com/w/', content)
181
- content = re.sub(r'src="/', 'src="https://en.cppreference.com/', content)
182
- return content
183
-
184
-
185
- # ---------------------------------------------------------------------------
186
- # Display
187
- # ---------------------------------------------------------------------------
188
-
189
-
190
- def _is_jupyter() -> bool:
191
- try:
192
- from IPython.display import HTML, display # noqa: F401
193
-
194
- get_ipython # type: ignore[name-defined]
195
- return True
196
- except Exception:
197
- return False
198
-
199
-
200
- def _format_search_html(results: list[dict[str, str]]) -> str:
201
- rows = []
202
- for r in results:
203
- title = html.escape(r.get("title", ""))
204
- url = html.escape(r.get("url", ""))
205
- snippet = html.escape(r.get("snippet", ""))[:120]
206
- rows.append(
207
- f'<tr><td><a href="https://en.cppreference.com/w/{url}" '
208
- f'target="_blank">{title}</a></td>'
209
- f'<td><code>{snippet}</code></td></tr>'
210
- )
211
- return (
212
- '<table style="font-size:14px;border-collapse:collapse">'
213
- "<tr><th>Title</th><th>Path</th></tr>"
214
- + "\n".join(rows)
215
- + "</table>"
216
- )
217
-
218
-
219
- def _format_page_html(content: str) -> str:
220
- return (
221
- '<div style="max-height:600px;overflow:auto;'
222
- 'border:1px solid #ddd;padding:16px;font-size:14px">'
223
- + content
224
- + "</div>"
225
- )
226
-
227
-
228
- # ---------------------------------------------------------------------------
229
- # Public API
230
- # ---------------------------------------------------------------------------
231
-
232
-
233
- def man(query: str) -> Any:
234
- """Display a C++ documentation page (like ``man`` for C++).
235
-
236
- In Jupyter: renders HTML inline.
237
- In terminal: prints plain text.
238
-
239
- Args:
240
- query: Page title or URL path (e.g. "std::vector" or "cpp/container/vector").
241
- """
242
- results = search(query, limit=1)
243
- if not results:
244
- msg = f"No documentation found for '{query}'."
245
- if _is_jupyter():
246
- from IPython.display import HTML, display
247
-
248
- display(HTML(f"<p>{html.escape(msg)}</p>"))
249
- print(msg)
250
- return
251
- url = results[0]["url"]
252
- content = _fetch_page(url)
253
- if _is_jupyter():
254
- from IPython.display import HTML, display
255
-
256
- display(HTML(_format_page_html(content)))
257
- else:
258
- # Strip HTML tags for terminal, then decode entities
259
- text = re.sub(r"<[^>]+>", "", content)
260
- text = html.unescape(text)
261
- text = re.sub(r"\[edit\]", "", text)
262
- text = re.sub(r"\s+", " ", text).strip()
263
- print(text[:4000])
264
-
265
-
266
- def help(query: str) -> Any:
267
- """Search C++ documentation (alias for :func:`search`).
268
-
269
- Args:
270
- query: Search term.
271
- """
272
- return search(query)
273
-
274
-
275
- def refresh_index() -> int:
276
- """Re-download the search index from GitHub Pages.
277
-
278
- Returns the number of indexed pages.
279
- """
280
- global _INDEX
281
- _INDEX = []
282
- url = "https://dive4dec.github.io/cppmanlite/index.json"
283
- _INDEX = json.loads(_fetch_url(url))
284
- return len(_INDEX)
285
-
286
-
287
- # Re-export ``help`` under a safe alias to avoid shadowing builtin
288
- help_query = help
File without changes
File without changes
File without changes