|
1 | 1 | import os |
2 | 2 | import sys |
3 | 3 | from glob import glob |
| 4 | +import shutil |
| 5 | +import re |
| 6 | +import functools |
| 7 | + |
4 | 8 | import click |
5 | 9 |
|
6 | 10 |
|
7 | 11 | @click.command() |
8 | 12 | @click.argument("project") |
9 | 13 | @click.option( |
10 | | - "-o", "--outdir", default="build", help="Output directory", show_default=True |
| 14 | + "-o", "--outdir", default="source", help="Output directory", show_default=True |
11 | 15 | ) |
12 | | -def publish(project, outdir): |
13 | | - """Generate myst report for `repo_owner`/`repo_name`.""" |
| 16 | +def template(project, outdir): |
| 17 | + """Generate myst report template for `repo_owner`/`repo_name`""" |
14 | 18 | os.makedirs(outdir, exist_ok=True) |
15 | 19 | os.makedirs(os.path.join(outdir, project), exist_ok=True) |
16 | 20 |
|
| 21 | + print(f"Populating [{outdir}] with templates for [{project}] report:", flush=True) |
| 22 | + |
17 | 23 | report_files = glob(os.path.join(os.path.dirname(__file__), "reports/*.md")) |
| 24 | + for f in report_files: |
| 25 | + dest = f"{outdir}/{project}/{os.path.basename(f)}" |
| 26 | + print(f" - {dest}") |
| 27 | + shutil.copyfile(f, dest) |
| 28 | + |
| 29 | + |
| 30 | +def _include_file(basedir, x): |
| 31 | + fn = x.group(1) |
| 32 | + with open(os.path.join(basedir, fn)) as f: |
| 33 | + return f.read() |
| 34 | + |
| 35 | + |
| 36 | +@click.command() |
| 37 | +@click.argument("project") |
| 38 | +@click.option( |
| 39 | + "-t", |
| 40 | + "--templatedir", |
| 41 | + default="source", |
| 42 | + help="Template directory", |
| 43 | + show_default=True, |
| 44 | +) |
| 45 | +@click.option( |
| 46 | + "-o", "--outdir", default="build", help="Output directory", show_default=True |
| 47 | +) |
| 48 | +def publish(project, templatedir, outdir): |
| 49 | + """Compile templates into markdown files ready for sphinx / myst""" |
| 50 | + os.makedirs(outdir, exist_ok=True) |
| 51 | + os.makedirs(os.path.join(outdir, project), exist_ok=True) |
18 | 52 |
|
19 | 53 | variables = {"project": project} |
20 | 54 |
|
21 | | - print(f"Generating [{project}] report in [{outdir}]...", end="", flush=True) |
| 55 | + print(f"Templating [{project}] report from [{templatedir}] to [{outdir}]...") |
| 56 | + |
| 57 | + template_files = glob(f"{templatedir}/{project}/*.md") |
22 | 58 |
|
23 | | - for report in report_files: |
24 | | - with open(report) as fh: |
| 59 | + for f in template_files: |
| 60 | + with open(f) as fh: |
25 | 61 | template = fh.read() |
26 | | - with open(f"{outdir}/{project}/{os.path.basename(report)}", "w") as fh: |
| 62 | + dest_dir = f"{outdir}/{project}" |
| 63 | + dest = f"{dest_dir}/{os.path.basename(f)}" |
| 64 | + with open(dest, "w") as fh: |
| 65 | + print(f" - {dest}") |
27 | 66 | for v in variables: |
28 | 67 | template = template.replace("{{ " + v + " }}", variables[v]) |
29 | | - fh.write(template) |
30 | 68 |
|
31 | | - print("OK") |
| 69 | + # Handle myst includes |
| 70 | + template = re.sub( |
| 71 | + r"```{include}\s*(.*)\s*```", |
| 72 | + functools.partial(_include_file, dest_dir), |
| 73 | + template, |
| 74 | + flags=re.MULTILINE, |
| 75 | + ) |
| 76 | + |
| 77 | + fh.write(template) |
0 commit comments