|
| 1 | +import gzip |
| 2 | +import json |
| 3 | +import re |
1 | 4 | from datetime import date |
2 | 5 | from pathlib import Path |
3 | 6 | from urllib.request import urlretrieve |
|
8 | 11 | } |
9 | 12 |
|
10 | 13 |
|
| 14 | +def on_post_build(config): |
| 15 | + site_dir = Path(config.site_dir) |
| 16 | + pattern = re.compile(r'(href="[^"]*)\.html(?=["#?])') |
| 17 | + for html_file in site_dir.rglob("*.html"): |
| 18 | + content = html_file.read_text() |
| 19 | + updated = pattern.sub(r"\1", content) |
| 20 | + if content != updated: |
| 21 | + html_file.write_text(updated) |
| 22 | + sitemap = site_dir / "sitemap.xml" |
| 23 | + if sitemap.exists(): |
| 24 | + content = sitemap.read_text() |
| 25 | + sitemap.write_text(content.replace(".html</loc>", "</loc>")) |
| 26 | + sitemap_gz = site_dir / "sitemap.xml.gz" |
| 27 | + if sitemap_gz.exists(): |
| 28 | + sitemap_gz.write_bytes(gzip.compress(sitemap.read_bytes())) |
| 29 | + search_index = site_dir / "search" / "search_index.json" |
| 30 | + if search_index.exists(): |
| 31 | + data = json.loads(search_index.read_text()) |
| 32 | + for doc in data.get("docs", []): |
| 33 | + doc["location"] = re.sub(r"\.html(?=[#?]|$)", "", doc.get("location", "")) |
| 34 | + search_index.write_text(json.dumps(data, separators=(",", ":"))) |
| 35 | + |
| 36 | + |
11 | 37 | def on_config(config): |
12 | 38 | config.copyright = f"Copyright © 2019-{date.today().year} MrNaif2018" |
13 | 39 | img_dir = Path(config.theme.custom_dir) / "images" |
|
0 commit comments