Skip to content

Commit 62cb4be

Browse files
fix: correct formatting for NOTICE files
1 parent 22eb364 commit 62cb4be

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

download.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import logging
5+
import re
56
import tomllib
67
from collections import deque
78
from datetime import UTC, date, datetime
@@ -49,14 +50,28 @@ def download(src_url: str, dst_path: Path, /, *, title: str, level: Literal[0, 1
4950

5051
r = client.get(src_url, follow_redirects=True)
5152
r.raise_for_status()
52-
dst_path.write_text(normalize_markdown(r.text), encoding="utf-8")
53+
dst_path.write_text(
54+
normalize_markdown(
55+
r.text,
56+
# NOTICE files are not really markdown
57+
tolerant=dst_path.stem == "NOTICE",
58+
),
59+
encoding="utf-8",
60+
)
5361

5462

55-
def normalize_markdown(content: str) -> str:
63+
def normalize_markdown(content: str, *, tolerant=False) -> str:
5664
"""Ad hoc normalization for https://docs.rs/pulldown-cmark-to-cmark"""
65+
5766
# For https://github.com/typst/packages/blob/09e558d2b8a5342dc6c273e6ec85eb2da1c47b44/docs/manifest.md?plain=1#L195-L196
5867
# pulldown-cmark can recognize it, but pulldown-cmark-to-cmark will collapse `[local packages]: …` to `[localpackages]: …` without this normalization.
59-
return content.replace("[local\n packages]", "[local packages]")
68+
content = content.replace("[local\n packages]", "[local packages]")
69+
70+
if tolerant:
71+
# Treat `===` as `<hr>` rather than heading markers.
72+
content = re.sub(r"^={5,}$", "\n---\n", content, flags=re.MULTILINE)
73+
74+
return content
6075

6176

6277
def download_typst(repo, /) -> None:

0 commit comments

Comments
 (0)