|
1 | 1 | import os |
2 | 2 | import sys |
3 | 3 | from glob import glob |
4 | | -from pathlib import Path |
| 4 | +import shutil |
| 5 | +import re |
| 6 | +import functools |
5 | 7 |
|
| 8 | +import click |
6 | 9 |
|
7 | | -def publisher(project): |
8 | | - print(f"Generating {project} report...", end="") |
9 | | - basedir = os.path.dirname(__file__) |
10 | | - with open(os.path.join(basedir, "template.md")) as fh: |
11 | | - template = fh.read() |
12 | 10 |
|
13 | | - issues = glob(os.path.join(basedir, "reports/issues/*.md")) |
14 | | - for issue in issues: |
15 | | - with open(issue) as fh: |
16 | | - issue_text = fh.read() |
17 | | - issue_name = Path(issue).stem |
18 | | - template = template.replace(f"{{{{ {issue_name} }}}}", issue_text) |
| 11 | +@click.command() |
| 12 | +@click.argument("project") |
| 13 | +@click.option( |
| 14 | + "-o", "--outdir", default="source", help="Output directory", show_default=True |
| 15 | +) |
| 16 | +def template(project, outdir): |
| 17 | + """Generate myst report templates |
19 | 18 |
|
20 | | - prs = glob(os.path.join(basedir, "reports/pull_requests/*.md")) |
21 | | - for pr in prs: |
22 | | - with open(pr) as fh: |
23 | | - pr_text = fh.read() |
24 | | - pr_name = Path(pr).stem |
25 | | - template = template.replace(f"{{{{ {pr_name} }}}}", pr_text) |
| 19 | + These templates are copied from `devstats`, and still need to be compiled |
| 20 | + to substitute variables. |
| 21 | + """ |
| 22 | + os.makedirs(outdir, exist_ok=True) |
| 23 | + os.makedirs(os.path.join(outdir, project), exist_ok=True) |
26 | 24 |
|
27 | | - template = template.replace("{{ project }}", project) |
| 25 | + print(f"Populating [{outdir}] with templates for [{project}] report:", flush=True) |
28 | 26 |
|
29 | | - os.makedirs("_generated", exist_ok=True) |
30 | | - with open(f"_generated/{project}.md", "w") as fh: |
31 | | - fh.write(template) |
| 27 | + report_files = glob(os.path.join(os.path.dirname(__file__), "reports/*.md")) |
| 28 | + for f in report_files: |
| 29 | + dest = f"{outdir}/{project}/{os.path.basename(f)}" |
| 30 | + print(f" - {dest}") |
| 31 | + shutil.copyfile(f, dest) |
32 | 32 |
|
33 | | - print("OK") |
| 33 | + |
| 34 | +def _include_file(basedir, x): |
| 35 | + fn = x.group(1) |
| 36 | + with open(os.path.join(basedir, fn)) as f: |
| 37 | + return f.read() |
| 38 | + |
| 39 | + |
| 40 | +@click.command() |
| 41 | +@click.argument("project") |
| 42 | +@click.option( |
| 43 | + "-t", |
| 44 | + "--templatedir", |
| 45 | + default="source", |
| 46 | + help="Template directory", |
| 47 | + show_default=True, |
| 48 | +) |
| 49 | +@click.option( |
| 50 | + "-o", "--outdir", default="build", help="Output directory", show_default=True |
| 51 | +) |
| 52 | +def publish(project, templatedir, outdir): |
| 53 | + """Compile templates (substitute variables) into markdown files ready for sphinx / myst |
| 54 | +
|
| 55 | + Include sections like the following are executed: |
| 56 | +
|
| 57 | + ``` |
| 58 | + {include} filename.md |
| 59 | + ``` |
| 60 | +
|
| 61 | + Thereafter, the following variables are substituted: |
| 62 | +
|
| 63 | + - `{{ project }}`: Name of the project |
| 64 | +
|
| 65 | + """ |
| 66 | + os.makedirs(outdir, exist_ok=True) |
| 67 | + os.makedirs(os.path.join(outdir, project), exist_ok=True) |
| 68 | + |
| 69 | + variables = {"project": project} |
| 70 | + |
| 71 | + print(f"Templating [{project}] report from [{templatedir}] to [{outdir}]...") |
| 72 | + |
| 73 | + templatedir = f"{templatedir}/{project}" |
| 74 | + template_files = [f"{templatedir}/index.md"] |
| 75 | + |
| 76 | + for f in template_files: |
| 77 | + with open(f) as fh: |
| 78 | + template = fh.read() |
| 79 | + dest_dir = f"{outdir}/{project}" |
| 80 | + dest = f"{dest_dir}/{os.path.basename(f)}" |
| 81 | + with open(dest, "w") as fh: |
| 82 | + print(f" - {dest}") |
| 83 | + # Handle myst includes |
| 84 | + template = re.sub( |
| 85 | + r"```{include}\s*(.*)\s*```", |
| 86 | + functools.partial(_include_file, templatedir), |
| 87 | + template, |
| 88 | + flags=re.MULTILINE, |
| 89 | + ) |
| 90 | + |
| 91 | + for v in variables: |
| 92 | + template = template.replace("{{ " + v + " }}", variables[v]) |
| 93 | + |
| 94 | + fh.write(template) |
0 commit comments