Отличное форматирование

This commit is contained in:
2026-05-26 14:59:25 +03:00
parent 2b139a18b3
commit e3a615cb50
49 changed files with 1294 additions and 501 deletions

View File

@@ -96,6 +96,13 @@ def typst_escape_path(rel: str) -> str:
return rel.replace("\\", "/")
def typst_escape_caption(text: str) -> str:
"""Экранирование для текста в [...]; без обратных кавычек (inline raw ловит raw.line)."""
for ch in "#$\\[]":
text = text.replace(ch, "\\" + ch)
return text
def write_listing(
out_path: Path,
rel_from_report: str,
@@ -104,19 +111,15 @@ def write_listing(
lang: str,
) -> None:
rel_typ = typst_escape_path(rel_from_report)
# Подпись перед кодом: figure.caption(position: top) в теле (placement: top — про float, не порядок).
content = (
f'#let lst-body-{label} = read("{rel_typ}")\n'
f"#figure(\n"
f" [\n"
f" #figure.caption(position: top)[{caption}]\n"
f" #block(breakable: true)[\n"
f" #raw(lst-body-{label}, lang: \"{lang}\", block: true)\n"
f" ]\n"
f" #raw(read(\"{rel_typ}\"), lang: \"{lang}\", block: true, tab-size: 4, align: left)\n"
f" ],\n"
f" supplement: [Листинг],\n"
f" gap: 0.4em,\n"
f") <lst-{label}>\n\n"
f") <lst-{label}>\n"
f"#pagebreak(weak: true)\n\n"
)
out_path.write_text(content, encoding="utf-8")
@@ -159,7 +162,7 @@ def main() -> int:
rel_report_str = rel_report.as_posix()
hid = path_hash(rel_repo)
label = hid
cap = f"Исходный файл `{rel_repo}`"
cap = f"Исходный файл {typst_escape_caption(rel_repo)}"
lang = lang_for(f, ext_map, name_map)
listing_name = f"listing-{hid}.typ"
listing_path = generated / listing_name
@@ -182,9 +185,13 @@ def main() -> int:
ix.write(f"// Generated listings: {len(files)} files\n\n")
appendix = generated / "appendix-a.typ"
styles_path = report_dir / "includes" / "listings-appendix.typ"
styles = styles_path.read_text(encoding="utf-8")
with appendix.open("w", encoding="utf-8") as ap:
ap.write("// AUTO-GENERATED by gen_listings.py — do not edit\n\n")
ap.write("#set figure(gap: 0.4em)\n\n")
ap.write("// Стили листингов (вставлены напрямую: #include не распространяет show-правила)\n")
ap.write(styles)
ap.write("\n")
for mi in module_includes:
ap.write(f'#include "{mi}"\n')