|
| 1 | +"""Build plotly.js documentation using jinja template.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import frontmatter |
| 5 | +from jinja2 import Environment, FileSystemLoader |
| 6 | +from jinja2.exceptions import TemplateError |
| 7 | +import json |
| 8 | +import markdown |
| 9 | +from pathlib import Path |
| 10 | +import sys |
| 11 | + |
| 12 | +import plugins |
| 13 | + |
| 14 | + |
| 15 | +KEYS_TO_IGNORE = { |
| 16 | + "_isSubplotObj", |
| 17 | + "editType", |
| 18 | + "role", |
| 19 | +} |
| 20 | +SUBPLOT = "_isSubplotObj" |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + """Main driver.""" |
| 25 | + opt = parse_args() |
| 26 | + schema = json.loads(Path(opt.schema).read_text()) |
| 27 | + env = Environment(loader=FileSystemLoader(opt.theme)) |
| 28 | + env.filters["backtick"] = plugins.backtick |
| 29 | + env.filters["debug"] = plugins.debug |
| 30 | + all_pages = opt.page if opt.page else [p.name for p in Path(opt.stubs).glob("*.md")] |
| 31 | + err_count = 0 |
| 32 | + for page in all_pages: |
| 33 | + if opt.crash: |
| 34 | + render_page(opt, schema, env, page) |
| 35 | + else: |
| 36 | + try: |
| 37 | + render_page(opt, schema, env, page) |
| 38 | + except Exception as exc: |
| 39 | + print(f"ERROR in {page}: {exc}", file=sys.stderr) |
| 40 | + err_count += 1 |
| 41 | + print(f"ERRORS: {err_count} / {len(all_pages)}") |
| 42 | + |
| 43 | + |
| 44 | +def get_details(schema, page): |
| 45 | + """Temporary hack to pull details out of schema and page header.""" |
| 46 | + # Trace |
| 47 | + if "full_name" not in page: |
| 48 | + return page |
| 49 | + |
| 50 | + key = page["name"].split(".")[-1] |
| 51 | + entry = schema["layout"]["layoutAttributes"][key] |
| 52 | + |
| 53 | + # Subplot |
| 54 | + if SUBPLOT in entry: |
| 55 | + return entry |
| 56 | + |
| 57 | + # Figure |
| 58 | + return list(entry["items"].values())[0] |
| 59 | + |
| 60 | + |
| 61 | +def parse_args(): |
| 62 | + """Parse command-line arguments.""" |
| 63 | + parser = argparse.ArgumentParser() |
| 64 | + parser.add_argument("--crash", action="store_true", help="crash on first error") |
| 65 | + parser.add_argument("--out", help="name of output directory") |
| 66 | + parser.add_argument("--schema", required=True, help="path to schema JSON") |
| 67 | + parser.add_argument("--stubs", required=True, help="path to stubs directory") |
| 68 | + parser.add_argument("--theme", required=True, help="path to theme directory") |
| 69 | + parser.add_argument("page", nargs="...", help="name(s) of source file in stubs directory") |
| 70 | + return parser.parse_args() |
| 71 | + |
| 72 | + |
| 73 | +def render_page(opt, schema, env, page_name): |
| 74 | + """Render a single page.""" |
| 75 | + stem = Path(page_name).stem |
| 76 | + loaded = frontmatter.load(Path(opt.stubs, page_name)) |
| 77 | + metadata = loaded.metadata |
| 78 | + assert "template" in metadata, f"page {page_name} does not specify 'template'" |
| 79 | + content = loaded.content |
| 80 | + details = get_details(schema, metadata) |
| 81 | + template = env.get_template(metadata["template"]) |
| 82 | + html = template.render( |
| 83 | + page={"title": stem, "meta": metadata}, |
| 84 | + config={"data": {"plot-schema": schema}}, |
| 85 | + details=details, |
| 86 | + keys_to_ignore=KEYS_TO_IGNORE, |
| 87 | + content=content, |
| 88 | + ) |
| 89 | + if opt.out: |
| 90 | + Path(opt.out, stem).mkdir(parents=True, exist_ok=True) |
| 91 | + Path(opt.out, stem, "index.html").write_text(html) |
| 92 | + else: |
| 93 | + print(html) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments