afterpython 0.4.9__tar.gz → 0.5.1__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.
- {afterpython-0.4.9 → afterpython-0.5.1}/PKG-INFO +1 -1
- {afterpython-0.4.9 → afterpython-0.5.1}/pyproject.toml +1 -1
- {afterpython-0.4.9 → afterpython-0.5.1}/pyproject.toml.orig +1 -1
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/__init__.py +2 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/build_pdoc.py +2 -1
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/jupyter_notebook.py +64 -17
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/marimo_content.py +30 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/metadata.py +3 -0
- afterpython-0.5.1/src/afterpython/builders/seo.py +218 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/build.py +7 -2
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/sync.py +5 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/afterpython-template.toml +3 -1
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/tools/_afterpython.py +2 -2
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/tools/myst.py +3 -6
- {afterpython-0.4.9 → afterpython-0.5.1}/README.md +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/__init__.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/_io/toml.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/_io/yaml.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/_paths.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/_typing.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/comments_json.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/content_json.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/faq_json.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/index_md.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/llms_txt.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/marimo_notebook.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/markdown.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/site_data.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/builders/url_md.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/__init__.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/add.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/bump.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/changelog.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/check.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/clean.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/commit_msg.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/dev.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/format.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/init.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/init_branch_rules.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/install.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/lint.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/lock.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/prek.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/preview.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/release.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/remove.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/serve.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/start.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/commands/update.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/cli/main.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/const.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/main.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/pcu.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/py.typed +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/ci-workflow-template.yml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/cliff-template.toml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/dependabot-template.yml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/deploy-workflow-template.yml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pdoc/custom.css +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pdoc/frame.html.jinja2 +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pdoc/module.html.jinja2 +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pdoc/syntax-highlighting.css +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pdoc/theme.css +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pre-commit-config-template.yaml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/release-workflow-template.yml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/ruff-template.toml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/ty-template.toml +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/tools/_git.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/tools/branch_rules.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/tools/github_actions.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/tools/prek.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/tools/pyproject.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/utils.py +0 -0
- {afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/version.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: afterpython
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Summary: CLI tool to streamline Python package maintenance and generate a dedicated project website
|
|
5
5
|
Keywords: package-maintenance,project-website,static-site-generator,developer-tools,documentation,cli-tool,ci/cd,mystmd,semantic-release,python-packaging
|
|
6
6
|
Author: Stephen Yau
|
|
@@ -12,6 +12,7 @@ from afterpython.builders.marimo_content import build_marimo_content
|
|
|
12
12
|
from afterpython.builders.marimo_notebook import build_marimo_readme
|
|
13
13
|
from afterpython.builders.markdown import build_markdown
|
|
14
14
|
from afterpython.builders.metadata import build_metadata
|
|
15
|
+
from afterpython.builders.seo import build_seo
|
|
15
16
|
from afterpython.builders.url_md import build_url_md
|
|
16
17
|
|
|
17
18
|
__all__ = (
|
|
@@ -25,6 +26,7 @@ __all__ = (
|
|
|
25
26
|
"build_markdown",
|
|
26
27
|
"build_metadata",
|
|
27
28
|
"build_pdoc",
|
|
29
|
+
"build_seo",
|
|
28
30
|
"build_url_md",
|
|
29
31
|
"create_placeholder_index_md_files",
|
|
30
32
|
"delete_placeholder_index_md_files",
|
|
@@ -66,5 +66,6 @@ def build_pdoc(base_path: str) -> None:
|
|
|
66
66
|
if result.returncode != 0:
|
|
67
67
|
raise click.ClickException(
|
|
68
68
|
f"pdoc failed with exit code {result.returncode}. "
|
|
69
|
-
|
|
69
|
+
"To skip API reference generation, set `api_reference = false` "
|
|
70
|
+
"under [website] in afterpython.toml."
|
|
70
71
|
)
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import json
|
|
2
|
-
import
|
|
2
|
+
import re
|
|
3
3
|
from pathlib import Path
|
|
4
4
|
|
|
5
5
|
import click
|
|
6
|
+
from ruamel.yaml import YAML
|
|
7
|
+
from ruamel.yaml.error import YAMLError
|
|
6
8
|
|
|
7
9
|
import afterpython as ap
|
|
8
10
|
from afterpython.builders.marimo_notebook import _create_molab_url, _get_molab_badge
|
|
@@ -178,6 +180,43 @@ def _add_thumbnail_to_notebook(
|
|
|
178
180
|
return True
|
|
179
181
|
|
|
180
182
|
|
|
183
|
+
def _wants_molab_badge(notebook: dict) -> bool:
|
|
184
|
+
"""True if the notebook's frontmatter sets `molab_badge: true`, or a
|
|
185
|
+
generated notebook already had it moved into its metadata."""
|
|
186
|
+
if notebook.get("metadata", {}).get("afterpython", {}).get("molab_badge"):
|
|
187
|
+
return True
|
|
188
|
+
content = _read_frontmatter(notebook)
|
|
189
|
+
if content is None:
|
|
190
|
+
return False
|
|
191
|
+
match = re.match(r"^---\s*\n(.*?)\n---", content, re.DOTALL)
|
|
192
|
+
if not match:
|
|
193
|
+
return False
|
|
194
|
+
try:
|
|
195
|
+
data = YAML(typ="safe").load(match.group(1))
|
|
196
|
+
except YAMLError:
|
|
197
|
+
return False
|
|
198
|
+
return isinstance(data, dict) and data.get("molab_badge") is True
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _remove_frontmatter_key(notebook: dict, key: str):
|
|
202
|
+
content = _read_frontmatter(notebook)
|
|
203
|
+
if content is None:
|
|
204
|
+
return
|
|
205
|
+
lines = [line for line in content.split("\n") if not line.startswith(f"{key}:")]
|
|
206
|
+
_write_frontmatter(notebook, "\n".join(lines))
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _remove_molab_badge_from_notebook(notebook: dict) -> bool:
|
|
210
|
+
"""Remove a badge cell left by an earlier build, e.g. after `molab_badge`
|
|
211
|
+
was turned off in a committed notebook. Returns True if one was removed."""
|
|
212
|
+
cells = notebook["cells"]
|
|
213
|
+
kept = [cell for cell in cells if cell.get("id") != "molab-badge-cell"]
|
|
214
|
+
if len(kept) == len(cells):
|
|
215
|
+
return False
|
|
216
|
+
notebook["cells"] = kept
|
|
217
|
+
return True
|
|
218
|
+
|
|
219
|
+
|
|
181
220
|
def _add_molab_badge_to_notebook(notebook: dict, github_url: str, content_path: Path):
|
|
182
221
|
"""Add a molab badge markdown cell to a single notebook.
|
|
183
222
|
|
|
@@ -224,20 +263,9 @@ def build_jupyter_notebooks():
|
|
|
224
263
|
"""Build Jupyter notebooks for all content types"""
|
|
225
264
|
from afterpython.tools._afterpython import get_default_thumbnail
|
|
226
265
|
|
|
227
|
-
#
|
|
228
|
-
add_molab_badge = os.getenv("AP_MOLAB_BADGE", "0") == "1"
|
|
229
|
-
|
|
230
|
-
# Get GitHub URL once if needed for molab badges
|
|
266
|
+
# read pyproject.toml only once a notebook wants a badge
|
|
231
267
|
github_url = None
|
|
232
|
-
|
|
233
|
-
metadata = read_metadata()
|
|
234
|
-
if "repository" in metadata.urls:
|
|
235
|
-
github_url = metadata.urls["repository"]
|
|
236
|
-
else:
|
|
237
|
-
click.echo(
|
|
238
|
-
"⚠ Repository URL not found in [project.urls] in pyproject.toml, skipping molab badge"
|
|
239
|
-
)
|
|
240
|
-
add_molab_badge = False
|
|
268
|
+
github_url_read = False
|
|
241
269
|
|
|
242
270
|
for content_type in CONTENT_TYPES:
|
|
243
271
|
click.echo(f"Building Jupyter notebooks for {content_type}/...")
|
|
@@ -269,9 +297,18 @@ def build_jupyter_notebooks():
|
|
|
269
297
|
# Track if notebook was modified
|
|
270
298
|
modified = False
|
|
271
299
|
|
|
272
|
-
# Add molab badge if
|
|
273
|
-
#
|
|
274
|
-
|
|
300
|
+
# Add a molab badge if the notebook opts in with `molab_badge: true`
|
|
301
|
+
# in its frontmatter. A notebook generated from a marimo .py isn't
|
|
302
|
+
# committed, so the badge must open the .py instead.
|
|
303
|
+
wants_badge = _wants_molab_badge(notebook)
|
|
304
|
+
if wants_badge and not github_url_read:
|
|
305
|
+
github_url = read_metadata().urls.get("repository")
|
|
306
|
+
github_url_read = True
|
|
307
|
+
if wants_badge and not github_url:
|
|
308
|
+
click.echo(
|
|
309
|
+
f"⚠ Repository URL not found in [project.urls] in pyproject.toml, skipping molab badge in {content_path}"
|
|
310
|
+
)
|
|
311
|
+
if wants_badge and github_url:
|
|
275
312
|
generated_from = (
|
|
276
313
|
notebook.get("metadata", {})
|
|
277
314
|
.get("afterpython", {})
|
|
@@ -284,6 +321,16 @@ def build_jupyter_notebooks():
|
|
|
284
321
|
)
|
|
285
322
|
_add_molab_badge_to_notebook(notebook, github_url, molab_path)
|
|
286
323
|
modified = True
|
|
324
|
+
# MyST warns about unknown frontmatter keys. A generated notebook is
|
|
325
|
+
# a build output, so move the key into its metadata, where a cached
|
|
326
|
+
# rebuild still finds it; a hand-written notebook is the source and
|
|
327
|
+
# must keep it.
|
|
328
|
+
afterpython_metadata = notebook.get("metadata", {}).get("afterpython")
|
|
329
|
+
if wants_badge and afterpython_metadata is not None:
|
|
330
|
+
afterpython_metadata["molab_badge"] = True
|
|
331
|
+
_remove_frontmatter_key(notebook, "molab_badge")
|
|
332
|
+
elif _remove_molab_badge_from_notebook(notebook):
|
|
333
|
+
modified = True
|
|
287
334
|
|
|
288
335
|
# Add thumbnail to frontmatter
|
|
289
336
|
if content_type != "doc" and _add_thumbnail_to_notebook(
|
|
@@ -333,12 +333,42 @@ def _rewrite_cell_ids(notebook: dict, prefix: str):
|
|
|
333
333
|
cell["id"] = cell_id
|
|
334
334
|
|
|
335
335
|
|
|
336
|
+
def _markdown_output_to_markdown_cell(notebook: dict):
|
|
337
|
+
"""A static `mo.md("...")` cell exports as a markdown cell, but a dynamic one
|
|
338
|
+
(e.g. an f-string) stays a code cell with a `text/markdown` output, and MyST
|
|
339
|
+
renders that output as raw text instead of markdown. When the code is hidden,
|
|
340
|
+
only the output is shown anyway, so turn the cell into a markdown cell with
|
|
341
|
+
the output as its source."""
|
|
342
|
+
for index, cell in enumerate(notebook["cells"]):
|
|
343
|
+
if cell.get("cell_type") != "code":
|
|
344
|
+
continue
|
|
345
|
+
hide_code = (
|
|
346
|
+
cell.get("metadata", {})
|
|
347
|
+
.get("marimo", {})
|
|
348
|
+
.get("config", {})
|
|
349
|
+
.get("hide_code", False)
|
|
350
|
+
)
|
|
351
|
+
outputs = cell.get("outputs", [])
|
|
352
|
+
if not hide_code or len(outputs) != 1:
|
|
353
|
+
continue
|
|
354
|
+
data = outputs[0].get("data", {})
|
|
355
|
+
if "text/markdown" not in data or set(data) - {"text/markdown", "text/plain"}:
|
|
356
|
+
continue
|
|
357
|
+
notebook["cells"][index] = {
|
|
358
|
+
"cell_type": "markdown",
|
|
359
|
+
"id": cell.get("id", ""),
|
|
360
|
+
"metadata": {},
|
|
361
|
+
"source": _as_lines(data["text/markdown"]),
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
|
|
336
365
|
def _postprocess(
|
|
337
366
|
notebook: dict, source: Path, content_path: Path, marimo_version: str
|
|
338
367
|
) -> dict:
|
|
339
368
|
for cell in notebook["cells"]:
|
|
340
369
|
cell["source"] = _as_lines(cell.get("source", ""))
|
|
341
370
|
_check_no_errors(notebook, source)
|
|
371
|
+
_markdown_output_to_markdown_cell(notebook)
|
|
342
372
|
_check_frontmatter_first(notebook, source)
|
|
343
373
|
_warn_heading_not_first(notebook, source)
|
|
344
374
|
# outputs come from the sandbox, `myst build --execute` must not redo them
|
|
@@ -55,6 +55,9 @@ def build_metadata():
|
|
|
55
55
|
|
|
56
56
|
afterpython = read_afterpython()
|
|
57
57
|
website = _from_tomlkit(afterpython.get("website", {}))
|
|
58
|
+
# homepage <title>, e.g. "PyTrade - Python Trading Resources & Tutorials";
|
|
59
|
+
# empty means the frontend falls back to its own default
|
|
60
|
+
metadata_json["site_title"] = str(website.get("title", "")).strip()
|
|
58
61
|
metadata_json["announcement"] = str(website.get("announcement", "")).strip()
|
|
59
62
|
metadata_json["readme_py"] = _resolve_readme_py(website)
|
|
60
63
|
# Logos are served from the site root in _website/ (assets copied from
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""Post-process the final static site (afterpython/_website/build/) for SEO.
|
|
2
|
+
|
|
3
|
+
Runs after `pnpm build`, on the HTML that actually gets deployed, so it covers
|
|
4
|
+
both the SvelteKit pages and the MyST content pages without touching either
|
|
5
|
+
generator:
|
|
6
|
+
|
|
7
|
+
- one canonical URL per page: `x/index.html` -> `{url}/x/`, `x.html` -> `{url}/x`,
|
|
8
|
+
matching how GitHub Pages serves them (no redirect, no duplicate)
|
|
9
|
+
- `<link rel="canonical">` injected into every indexable page
|
|
10
|
+
- internal links rewritten to that canonical form, so `/doc/quickstart` (301 on
|
|
11
|
+
GitHub Pages) becomes `/doc/quickstart/`
|
|
12
|
+
- `noindex` on the SPA fallback (200.html) and 404.html
|
|
13
|
+
- sitemap.xml listing every indexable page, plus a robots.txt pointing to it
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import html
|
|
19
|
+
import posixpath
|
|
20
|
+
import re
|
|
21
|
+
from functools import partial
|
|
22
|
+
from pathlib import Path, PurePosixPath
|
|
23
|
+
from xml.sax.saxutils import escape
|
|
24
|
+
|
|
25
|
+
import click
|
|
26
|
+
|
|
27
|
+
import afterpython as ap
|
|
28
|
+
|
|
29
|
+
# pages that must not be indexed or listed in the sitemap
|
|
30
|
+
NOINDEX_FILES = {"200.html", "404.html"}
|
|
31
|
+
|
|
32
|
+
_HEAD_OPEN = re.compile(r"<head[^>]*>", re.IGNORECASE)
|
|
33
|
+
_CANONICAL = re.compile(r"<link[^>]*rel=[\"']?canonical", re.IGNORECASE)
|
|
34
|
+
_ROBOTS_NOINDEX = re.compile(
|
|
35
|
+
r"<meta[^>]*name=[\"']?robots[\"']?[^>]*noindex", re.IGNORECASE
|
|
36
|
+
)
|
|
37
|
+
# SvelteKit's prerendered redirect, e.g. blog/x.html -> blog/x/ (old post links)
|
|
38
|
+
_META_REFRESH = re.compile(r"<meta[^>]*http-equiv=[\"']?refresh", re.IGNORECASE)
|
|
39
|
+
# href on any tag, skipping external (scheme:) and fragment-only links
|
|
40
|
+
_HREF = re.compile(
|
|
41
|
+
r"""(\shref=)(["'])(?![a-zA-Z][\w+.-]*:|#)([^"']+)\2""", re.IGNORECASE
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _canonical_path(rel: PurePosixPath) -> str:
|
|
46
|
+
"""URL path (without base path) under which GitHub Pages serves `rel` with a 200."""
|
|
47
|
+
if rel.name == "index.html":
|
|
48
|
+
parent = rel.parent.as_posix()
|
|
49
|
+
return "/" if parent == "." else f"/{parent}/"
|
|
50
|
+
return "/" + rel.with_suffix("").as_posix()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _is_redirect(text: str) -> bool:
|
|
54
|
+
head = text.split("</head>", 1)[0]
|
|
55
|
+
return bool(_META_REFRESH.search(head))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _canonical_map(build_dir: Path) -> dict[str, str]:
|
|
59
|
+
"""Map every URL path a page answers to (with and without the trailing
|
|
60
|
+
slash) to that page's canonical path."""
|
|
61
|
+
# redirect stubs aren't pages: they never win over x/index.html
|
|
62
|
+
canonicals = {
|
|
63
|
+
_canonical_path(PurePosixPath(f.relative_to(build_dir).as_posix()))
|
|
64
|
+
for f in build_dir.rglob("*.html")
|
|
65
|
+
if not _is_redirect(f.read_text(encoding="utf-8"))
|
|
66
|
+
}
|
|
67
|
+
mapping: dict[str, str] = {}
|
|
68
|
+
for canonical in sorted(canonicals):
|
|
69
|
+
bare = canonical.rstrip("/") or "/"
|
|
70
|
+
# `x.html` wins over `x/index.html` when both exist: it's what
|
|
71
|
+
# SvelteKit prerenders for routes like /faq and /blog
|
|
72
|
+
if canonical != "/" and canonical.endswith("/") and bare in canonicals:
|
|
73
|
+
continue
|
|
74
|
+
mapping[bare] = canonical
|
|
75
|
+
mapping[bare.rstrip("/") + "/"] = canonical
|
|
76
|
+
return mapping
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _rewrite_href(
|
|
80
|
+
match: re.Match,
|
|
81
|
+
page_dir: str,
|
|
82
|
+
base_path: str,
|
|
83
|
+
canonical_by_path: dict[str, str],
|
|
84
|
+
) -> str:
|
|
85
|
+
"""Fix the trailing slash of an internal link so it doesn't redirect.
|
|
86
|
+
|
|
87
|
+
Only the slash changes, so absolute (`/doc/x`) and relative (`./doc`, how
|
|
88
|
+
SvelteKit writes links) hrefs both keep their form.
|
|
89
|
+
"""
|
|
90
|
+
prefix, quote, url = match.group(1), match.group(2), match.group(3)
|
|
91
|
+
path, sep, rest = _split_url(url)
|
|
92
|
+
if not path or path.startswith("//"):
|
|
93
|
+
return match.group(0)
|
|
94
|
+
if path.startswith("/"):
|
|
95
|
+
if base_path:
|
|
96
|
+
if path != base_path and not path.startswith(base_path + "/"):
|
|
97
|
+
return match.group(0)
|
|
98
|
+
local = path[len(base_path) :] or "/"
|
|
99
|
+
else:
|
|
100
|
+
local = path
|
|
101
|
+
else:
|
|
102
|
+
local = posixpath.normpath(posixpath.join(page_dir, path))
|
|
103
|
+
if path.endswith("/") and not local.endswith("/"):
|
|
104
|
+
local += "/"
|
|
105
|
+
canonical = canonical_by_path.get(html.unescape(local))
|
|
106
|
+
if canonical is None or canonical == local:
|
|
107
|
+
return match.group(0)
|
|
108
|
+
new_path = path + "/" if canonical.endswith("/") else path.rstrip("/")
|
|
109
|
+
return f"{prefix}{quote}{new_path}{sep}{rest}{quote}"
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _split_url(url: str) -> tuple[str, str, str]:
|
|
113
|
+
"""Split '/a/b?x#y' into ('/a/b', '?', 'x#y')."""
|
|
114
|
+
for i, ch in enumerate(url):
|
|
115
|
+
if ch in "?#":
|
|
116
|
+
return url[:i], ch, url[i + 1 :]
|
|
117
|
+
return url, "", ""
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _read_website_url() -> str:
|
|
121
|
+
from afterpython._io.toml import _from_tomlkit
|
|
122
|
+
from afterpython.tools._afterpython import read_afterpython
|
|
123
|
+
|
|
124
|
+
website = _from_tomlkit(read_afterpython().get("website", {}))
|
|
125
|
+
return str(website.get("url", "")).strip().rstrip("/")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def build_seo(base_path: str = ""):
|
|
129
|
+
"""Add canonical tags, fix internal links and write sitemap.xml/robots.txt.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
base_path: BASE_PATH the site was built with ("" for a custom domain,
|
|
133
|
+
"/repo" for a default GitHub Pages URL); internal links carry it.
|
|
134
|
+
"""
|
|
135
|
+
build_dir = ap.paths.website_path / "build"
|
|
136
|
+
if not build_dir.exists():
|
|
137
|
+
return
|
|
138
|
+
website_url = _read_website_url()
|
|
139
|
+
if not website_url:
|
|
140
|
+
click.echo(
|
|
141
|
+
"Warning: [website].url is not set in afterpython.toml, "
|
|
142
|
+
"skipping canonical tags and sitemap.xml"
|
|
143
|
+
)
|
|
144
|
+
return
|
|
145
|
+
# website.url already includes the base path for github.io sites
|
|
146
|
+
site_root = website_url
|
|
147
|
+
if base_path and website_url.endswith(base_path):
|
|
148
|
+
site_root = website_url[: -len(base_path)]
|
|
149
|
+
|
|
150
|
+
click.echo("Adding canonical tags and building sitemap.xml...")
|
|
151
|
+
canonical_by_path = _canonical_map(build_dir)
|
|
152
|
+
|
|
153
|
+
sitemap_urls: list[str] = []
|
|
154
|
+
for file in sorted(build_dir.rglob("*.html")):
|
|
155
|
+
rel = PurePosixPath(file.relative_to(build_dir).as_posix())
|
|
156
|
+
text = file.read_text(encoding="utf-8")
|
|
157
|
+
original = text
|
|
158
|
+
if _is_redirect(text):
|
|
159
|
+
continue
|
|
160
|
+
noindex = rel.as_posix() in NOINDEX_FILES or bool(_ROBOTS_NOINDEX.search(text))
|
|
161
|
+
|
|
162
|
+
page_path = _canonical_path(rel)
|
|
163
|
+
# directory relative links resolve against: /x/ for x/index.html, / for x.html
|
|
164
|
+
page_dir = (
|
|
165
|
+
page_path if page_path.endswith("/") else posixpath.dirname(page_path)
|
|
166
|
+
)
|
|
167
|
+
text = _HREF.sub(
|
|
168
|
+
partial(
|
|
169
|
+
_rewrite_href,
|
|
170
|
+
page_dir=page_dir,
|
|
171
|
+
base_path=base_path,
|
|
172
|
+
canonical_by_path=canonical_by_path,
|
|
173
|
+
),
|
|
174
|
+
text,
|
|
175
|
+
)
|
|
176
|
+
# a shadowed x/index.html points at its x.html twin
|
|
177
|
+
canonical_url = (
|
|
178
|
+
site_root + base_path + canonical_by_path.get(page_path, page_path)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
head = _HEAD_OPEN.search(text)
|
|
182
|
+
if head:
|
|
183
|
+
if rel.as_posix() in NOINDEX_FILES and not _ROBOTS_NOINDEX.search(text):
|
|
184
|
+
tag = '<meta name="robots" content="noindex">'
|
|
185
|
+
text = text[: head.end()] + tag + text[head.end() :]
|
|
186
|
+
elif not noindex and not _CANONICAL.search(text):
|
|
187
|
+
tag = f'<link rel="canonical" href="{html.escape(canonical_url)}">'
|
|
188
|
+
text = text[: head.end()] + tag + text[head.end() :]
|
|
189
|
+
|
|
190
|
+
if not noindex and head:
|
|
191
|
+
sitemap_urls.append(canonical_url)
|
|
192
|
+
if text != original:
|
|
193
|
+
file.write_text(text, encoding="utf-8")
|
|
194
|
+
|
|
195
|
+
_write_sitemap(build_dir, sitemap_urls)
|
|
196
|
+
_write_robots_txt(build_dir, f"{site_root}{base_path}/sitemap.xml")
|
|
197
|
+
click.echo(f"✓ sitemap.xml lists {len(sitemap_urls)} pages")
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _write_sitemap(build_dir: Path, urls: list[str]):
|
|
201
|
+
lines = ['<?xml version="1.0" encoding="UTF-8"?>']
|
|
202
|
+
lines.append('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
|
|
203
|
+
lines.extend(f" <url><loc>{escape(url)}</loc></url>" for url in sorted(set(urls)))
|
|
204
|
+
lines.append("</urlset>")
|
|
205
|
+
(build_dir / "sitemap.xml").write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _write_robots_txt(build_dir: Path, sitemap_url: str):
|
|
209
|
+
"""Write robots.txt, or add the Sitemap line to a user-provided one."""
|
|
210
|
+
robots = build_dir / "robots.txt"
|
|
211
|
+
line = f"Sitemap: {sitemap_url}"
|
|
212
|
+
if robots.exists():
|
|
213
|
+
text = robots.read_text(encoding="utf-8")
|
|
214
|
+
if "sitemap:" in text.lower():
|
|
215
|
+
return
|
|
216
|
+
robots.write_text(text.rstrip("\n") + f"\n\n{line}\n", encoding="utf-8")
|
|
217
|
+
else:
|
|
218
|
+
robots.write_text(f"User-agent: *\nAllow: /\n\n{line}\n", encoding="utf-8")
|
|
@@ -26,6 +26,7 @@ from afterpython.builders import (
|
|
|
26
26
|
build_markdown,
|
|
27
27
|
build_metadata,
|
|
28
28
|
build_pdoc,
|
|
29
|
+
build_seo,
|
|
29
30
|
build_url_md,
|
|
30
31
|
create_placeholder_index_md_files,
|
|
31
32
|
delete_placeholder_index_md_files,
|
|
@@ -103,8 +104,9 @@ def prebuild(dev_build: bool = False, build_readme_py: bool = True):
|
|
|
103
104
|
content_build_paths = [
|
|
104
105
|
website_static_path / content_type for content_type in CONTENT_TYPES
|
|
105
106
|
]
|
|
106
|
-
# Also wipe the API reference static dir so a
|
|
107
|
-
# leave stale pages from a prior run
|
|
107
|
+
# Also wipe the API reference static dir so a build with
|
|
108
|
+
# `api_reference = false` doesn't leave stale pages from a prior run
|
|
109
|
+
# live on the deployed site.
|
|
108
110
|
api_static_path = website_static_path / "api_reference"
|
|
109
111
|
for path in [
|
|
110
112
|
build_path,
|
|
@@ -339,3 +341,6 @@ def build(ctx: click.Context, execute: bool):
|
|
|
339
341
|
)
|
|
340
342
|
if result.returncode != 0:
|
|
341
343
|
raise Exit(result.returncode)
|
|
344
|
+
|
|
345
|
+
# on the final HTML, so it covers both SvelteKit and MyST pages
|
|
346
|
+
build_seo(base_path=base_path)
|
|
@@ -67,6 +67,10 @@ def sync():
|
|
|
67
67
|
company_name = str(_from_tomlkit(afterpython.get("company", {})).get("name", ""))
|
|
68
68
|
company_url = str(_from_tomlkit(afterpython.get("company", {})).get("url", ""))
|
|
69
69
|
website_url = str(_from_tomlkit(afterpython.get("website", {})).get("url", ""))
|
|
70
|
+
# empty string disables analytics: MyST only emits gtag for a truthy id
|
|
71
|
+
google_analytics_id = str(
|
|
72
|
+
_from_tomlkit(afterpython.get("website", {})).get("google_analytics_id", "")
|
|
73
|
+
).strip()
|
|
70
74
|
try:
|
|
71
75
|
website_favicon = normalize_static_path(
|
|
72
76
|
str(_from_tomlkit(afterpython.get("website", {})).get("favicon", ""))
|
|
@@ -151,6 +155,7 @@ def sync():
|
|
|
151
155
|
else "",
|
|
152
156
|
"logo_text": project_name,
|
|
153
157
|
"logo_url": website_url,
|
|
158
|
+
"analytics_google": google_analytics_id,
|
|
154
159
|
},
|
|
155
160
|
# FIXME: disable nav bar for now until myst fixes the issue with the nav bar
|
|
156
161
|
# the current issue is they prepend the BASE_URL to the nav bar links even they are external
|
|
@@ -5,10 +5,12 @@ url = "" # used in myst.yml's venue.url
|
|
|
5
5
|
|
|
6
6
|
[website]
|
|
7
7
|
url = ""
|
|
8
|
+
title = "" # homepage <title>, put your main search keywords here, e.g. "pytrade - Python Trading Resources & Tutorials"
|
|
9
|
+
google_analytics_id = "" # e.g. "G-XXXXXXXXXX"; empty disables Google Analytics on doc/blog/... pages
|
|
8
10
|
favicon = "favicon.ico"
|
|
9
11
|
logo = "logo.svg"
|
|
10
12
|
logo_dark = "logo.svg"
|
|
11
|
-
thumbnail = "thumbnail.png" # thumbnail for the website,
|
|
13
|
+
thumbnail = "thumbnail.png" # thumbnail for the website itself, not used for content; set [website.<content_type>].thumbnail for a default per content type
|
|
12
14
|
announcement = ""
|
|
13
15
|
readme_py = "wasm" # marimo html export format: "wasm" or "static"
|
|
14
16
|
execute_readme_py = false # execute README.py cells at build time and embed outputs as a preview (wasm mode only)
|
|
@@ -24,7 +24,6 @@ def get_default_thumbnail(
|
|
|
24
24
|
|
|
25
25
|
Reads default thumbnail configuration from afterpython.toml:
|
|
26
26
|
- Content type specific thumbnail: [website.{content_type}.thumbnail]
|
|
27
|
-
- Website-wide default thumbnail: [website.thumbnail]
|
|
28
27
|
- Falls back to empty string if no default is configured
|
|
29
28
|
|
|
30
29
|
Args:
|
|
@@ -39,6 +38,7 @@ def get_default_thumbnail(
|
|
|
39
38
|
|
|
40
39
|
afterpython = read_afterpython()
|
|
41
40
|
try:
|
|
41
|
+
# DEPRECATED: [website].thumbnail is not a default for content types
|
|
42
42
|
# website_default_thumbnail = normalize_static_path(
|
|
43
43
|
# str(_from_tomlkit(afterpython.get("website", {})).get("thumbnail", ""))
|
|
44
44
|
# )
|
|
@@ -62,7 +62,7 @@ def get_default_thumbnail(
|
|
|
62
62
|
|
|
63
63
|
if content_type_default_thumbnail:
|
|
64
64
|
return f"{static_prefix}static{content_type_default_thumbnail}"
|
|
65
|
-
# use "thumbnail" set in afterpython.toml [website]
|
|
65
|
+
# DEPRECATED: use "thumbnail" set in afterpython.toml [website]
|
|
66
66
|
# elif website_default_thumbnail:
|
|
67
67
|
# Website-wide static is one level up from content_type folder
|
|
68
68
|
# return f"{static_prefix}../static{website_default_thumbnail}"
|
|
@@ -234,12 +234,9 @@ def init_myst(content_types: Iterable[tContentType] | None = None):
|
|
|
234
234
|
env=node_env,
|
|
235
235
|
check=True,
|
|
236
236
|
)
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
# "twitter": "",
|
|
241
|
-
},
|
|
242
|
-
}
|
|
237
|
+
# analytics_google is filled in by `ap sync` from
|
|
238
|
+
# [website].google_analytics_id in afterpython.toml
|
|
239
|
+
site_defaults: dict[str, Any] = {"options": {}}
|
|
243
240
|
# keep `myst init`'s book-theme for every content type, blog included:
|
|
244
241
|
# article-theme v2.0.0 (2026-11-10) fails `myst build --html` with
|
|
245
242
|
# 'No route matches URL "/_public/..."'
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/deploy-workflow-template.yml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pdoc/syntax-highlighting.css
RENAMED
|
File without changes
|
|
File without changes
|
{afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/pre-commit-config-template.yaml
RENAMED
|
File without changes
|
{afterpython-0.4.9 → afterpython-0.5.1}/src/afterpython/templates/release-workflow-template.yml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|