|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Generate the CAD simulation tutorial section from its sources in AliceO2. |
| 4 | +
|
| 5 | +The tutorial is written and reviewed in AliceO2, under |
| 6 | +`Detectors/CADSupport/doc/tutorial/docs`, so that it stays next to the code it documents. |
| 7 | +This script converts those pages into what this Jekyll site expects and writes them to |
| 8 | +`docs/cadtutorial`. Do not edit that directory by hand: the next run overwrites it. |
| 9 | +
|
| 10 | +Two things are converted. |
| 11 | +
|
| 12 | + * GitHub alerts (`> [!NOTE]`) become fenced `note` and `warning` blocks, which the theme |
| 13 | + renders as callouts. The bold line under the marker stays as the callout's own title, |
| 14 | + because the theme's title is fixed. |
| 15 | + * Every page gets the `sort`/`title` front matter the navigation is built from. |
| 16 | +
|
| 17 | +Usage: |
| 18 | + sync_cad_tutorial.py --source <AliceO2 checkout> [--out docs/cadtutorial] |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import os |
| 23 | +import re |
| 24 | +import shutil |
| 25 | +import sys |
| 26 | + |
| 27 | +SECTION_SORT = 9 |
| 28 | +SECTION_TITLE = "CAD simulation tutorial" |
| 29 | + |
| 30 | +# The reading order of the tutorial, and the title each page carries here. |
| 31 | +PAGES = [ |
| 32 | + ("index.md", "README.md", "CAD simulation tutorial"), |
| 33 | + ("install.md", "install.md", "Install the software"), |
| 34 | + ("first-conversion.md", "first-conversion.md", "Convert your first model"), |
| 35 | + ("representation.md", "representation.md", "How a part is represented"), |
| 36 | + ("partial.md", "partial.md", "Convert only part of a model"), |
| 37 | + ("materials.md", "materials.md", "Give it materials"), |
| 38 | + ("field-and-cuts.md", "field-and-cuts.md", "Field and cuts"), |
| 39 | + ("geom-c.md", "geom-c.md", "The geom.C file"), |
| 40 | + ("passive.md", "passive.md", "Add passive geometry"), |
| 41 | + ("hits.md", "hits.md", "Make it produce hits"), |
| 42 | + ("real-detector.md", "real-detector.md", "Grow it into a real detector"), |
| 43 | + ("its-round-trip.md", "its-round-trip.md", "The ITS, out and back again"), |
| 44 | + ("checks.md", "checks.md", "Check your geometry"), |
| 45 | + ("limits.md", "limits.md", "Limits and pain points"), |
| 46 | +] |
| 47 | + |
| 48 | +ALERT = {"NOTE": "note", "TIP": "tip", "IMPORTANT": "note", |
| 49 | + "WARNING": "warning", "CAUTION": "danger"} |
| 50 | + |
| 51 | +SOURCE_URL = ("https://github.com/AliceO2Group/AliceO2/tree/dev/" |
| 52 | + "Detectors/CADSupport/doc/tutorial") |
| 53 | + |
| 54 | +PROVENANCE = f""" |
| 55 | +
|
| 56 | +--- |
| 57 | +
|
| 58 | +*These pages are generated from the tutorial sources in AliceO2, |
| 59 | +[Detectors/CADSupport/doc/tutorial]({SOURCE_URL}), which is where corrections belong.* |
| 60 | +""" |
| 61 | + |
| 62 | + |
| 63 | +def alerts_to_fences(text): |
| 64 | + """`> [!WARNING]` blocks become ```warning fences, whose body the theme markdownifies.""" |
| 65 | + lines, out, i = text.split("\n"), [], 0 |
| 66 | + while i < len(lines): |
| 67 | + m = re.match(r"^> \[!(\w+)\]\s*$", lines[i]) |
| 68 | + if not m or m.group(1) not in ALERT: |
| 69 | + out.append(lines[i]) |
| 70 | + i += 1 |
| 71 | + continue |
| 72 | + kind = ALERT[m.group(1)] |
| 73 | + i += 1 |
| 74 | + body = [] |
| 75 | + while i < len(lines) and lines[i].startswith(">"): |
| 76 | + body.append(lines[i][2:] if lines[i].startswith("> ") else lines[i][1:]) |
| 77 | + i += 1 |
| 78 | + if any(b.startswith("```") for b in body): |
| 79 | + raise SystemExit("a code fence inside an alert cannot be carried into a " |
| 80 | + "fenced callout; rewrite the source page") |
| 81 | + out.append("```" + kind) |
| 82 | + out.extend(body) |
| 83 | + out.append("```") |
| 84 | + return "\n".join(out) |
| 85 | + |
| 86 | + |
| 87 | +def relink(text, names): |
| 88 | + """Links between tutorial pages: index.md is README.md here, the rest keep their names.""" |
| 89 | + text = text.replace("(index.md)", "(README.md)") |
| 90 | + return text |
| 91 | + |
| 92 | + |
| 93 | +def front_matter(sort, title): |
| 94 | + return f"---\nsort: {sort}\ntitle: {title}\n---\n\n" |
| 95 | + |
| 96 | + |
| 97 | +def main(): |
| 98 | + p = argparse.ArgumentParser(description=__doc__, |
| 99 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 100 | + p.add_argument("--source", required=True, |
| 101 | + help="an AliceO2 checkout, or its Detectors/CADSupport/doc/tutorial directory") |
| 102 | + p.add_argument("--out", default="docs/cadtutorial") |
| 103 | + args = p.parse_args() |
| 104 | + |
| 105 | + src = args.source |
| 106 | + if os.path.isdir(os.path.join(src, "Detectors")): |
| 107 | + src = os.path.join(src, "Detectors/CADSupport/doc/tutorial") |
| 108 | + docs = os.path.join(src, "docs") |
| 109 | + if not os.path.isdir(docs): |
| 110 | + raise SystemExit(f"no tutorial sources under {docs}") |
| 111 | + |
| 112 | + os.makedirs(args.out, exist_ok=True) |
| 113 | + names = {a for a, _, _ in PAGES} |
| 114 | + written = [] |
| 115 | + for i, (source, target, title) in enumerate(PAGES): |
| 116 | + path = os.path.join(docs, source) |
| 117 | + if not os.path.exists(path): |
| 118 | + raise SystemExit(f"missing tutorial page: {path}") |
| 119 | + body = relink(alerts_to_fences(open(path).read()), names) |
| 120 | + sort = SECTION_SORT if target == "README.md" else i |
| 121 | + with open(os.path.join(args.out, target), "w") as fh: |
| 122 | + fh.write(front_matter(sort, SECTION_TITLE if target == "README.md" else title)) |
| 123 | + fh.write(body) |
| 124 | + if target == "README.md": |
| 125 | + fh.write(PROVENANCE) |
| 126 | + written.append(target) |
| 127 | + |
| 128 | + images_in = os.path.join(docs, "images") |
| 129 | + if os.path.isdir(images_in): |
| 130 | + images_out = os.path.join(args.out, "images") |
| 131 | + os.makedirs(images_out, exist_ok=True) |
| 132 | + for f in sorted(os.listdir(images_in)): |
| 133 | + shutil.copy2(os.path.join(images_in, f), os.path.join(images_out, f)) |
| 134 | + written.append(os.path.join("images", f)) |
| 135 | + |
| 136 | + print(f"wrote {len(written)} file(s) to {args.out}") |
| 137 | + return 0 |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + sys.exit(main()) |
0 commit comments