|
| 1 | +"""Rebuild a reference page from the Jekyll HTML and plot schema JSON file.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | +import re |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +INCLUDE_RE = re.compile( |
| 11 | + r'\{%\s*include\s+posts/reference-trace.html\s+trace_name="(.+?)"\s+trace_data=site\.data\.plotschema\.traces\.(.+?)\s*%}' |
| 12 | +) |
| 13 | +TITLE_RE = re.compile(r"<h2>.+?<code>(.+?)</code>.+?</h2>") |
| 14 | + |
| 15 | + |
| 16 | +PLOT_SCHEMA_CONTENT = """\ |
| 17 | +<div class="description"> |
| 18 | + A <code>{trace_name}</code> trace is an object with the key <code>"type"</code> equal to <code>"{trace_data_attributes_type}"</code> |
| 19 | + (i.e. <code>{{"type": "{trace_data_attributes_type}"}}</code>) and any of the keys listed below. |
| 20 | + <br><br>{trace_data_meta_description}<br><br> |
| 21 | +</div> |
| 22 | +""" |
| 23 | + |
| 24 | +PLOT_SCHEMA_REPLACEMENTS = ( |
| 25 | + ('*', '"'), |
| 26 | + ("{array}", "array"), |
| 27 | + ("{arrays}", "arrays"), |
| 28 | + ("{object}", "object"), |
| 29 | + ("{2D array}", "2D array"), |
| 30 | +) |
| 31 | + |
| 32 | +def main(): |
| 33 | + """Main driver.""" |
| 34 | + args = _parse_args() |
| 35 | + args.outdir.mkdir(parents=True, exist_ok=True) |
| 36 | + |
| 37 | + schema = json.loads(args.schema.read_text()) |
| 38 | + assert "traces" in schema, f"'traces' missing from {args.schema}" |
| 39 | + |
| 40 | + for src_path in args.inputs: |
| 41 | + _log(args.verbose > 0, f"...{src_path}") |
| 42 | + src_content = src_path.read_text() |
| 43 | + |
| 44 | + m = TITLE_RE.search(src_content) |
| 45 | + if _log(not m, f"failed to match title in {src_path}"): |
| 46 | + continue |
| 47 | + title = m.group(1) |
| 48 | + |
| 49 | + m = INCLUDE_RE.search(src_content) |
| 50 | + if _log(not m, f"failed to match include in {src_path}"): |
| 51 | + continue |
| 52 | + if _log(m.group(1) != title, f"title {title} != include title {m.group(1)} in {src_path}"): |
| 53 | + continue |
| 54 | + trace_name = m.group(2) |
| 55 | + trace_data = schema["traces"].get(trace_name, None) |
| 56 | + if _log(trace_data is None, f"trace '{trace_name}' not found in {args.schema}"): |
| 57 | + continue |
| 58 | + |
| 59 | + html = _reference_trace(args, schema, trace_name, trace_data) |
| 60 | + print(html) |
| 61 | + |
| 62 | + |
| 63 | +def _log(condition, msg): |
| 64 | + """Conditionally report progress.""" |
| 65 | + if condition: |
| 66 | + print(msg, file=sys.stderr) |
| 67 | + return condition |
| 68 | + |
| 69 | + |
| 70 | +def _parse_args(): |
| 71 | + """Parse command-line arguments.""" |
| 72 | + parser = argparse.ArgumentParser(description="Generate HTML reference documentation") |
| 73 | + parser.add_argument("inputs", nargs="+", type=Path, help="Input Jekyll files") |
| 74 | + parser.add_argument("--schema", type=Path, help="Path to plot schema JSON file") |
| 75 | + parser.add_argument("--outdir", type=Path, help="Output directory") |
| 76 | + parser.add_argument("--verbose", type=int, default=0, help="Integer verbosity level") |
| 77 | + return parser.parse_args() |
| 78 | + |
| 79 | + |
| 80 | +def _reference_block(args, accum, attributes, parent_link, parent_path, block): |
| 81 | + """Generate HTML documentation for a trace's attributes.""" |
| 82 | + |
| 83 | + |
| 84 | +def _reference_trace(args, schema, trace_name, trace_data): |
| 85 | + """Generate HTML documentation for a trace.""" |
| 86 | + plot_schema_content = PLOT_SCHEMA_CONTENT.format( |
| 87 | + trace_name=trace_name, |
| 88 | + trace_data_attributes_type=trace_data["attributes"]["type"], |
| 89 | + trace_data_meta_description=trace_data["meta"]["description"], |
| 90 | + ) |
| 91 | + plot_schema_content = _replace_special(plot_schema_content) |
| 92 | + accum = [plot_schema_content] |
| 93 | + |
| 94 | + parent_link = trace_name |
| 95 | + parent_path = f"data[type={trace_name}]" |
| 96 | + attributes = trace_data["attributes"] |
| 97 | + _reference_block(args, accum, attributes, parent_link, parent_path, "data") |
| 98 | + |
| 99 | + return "\n".join(accum) |
| 100 | + |
| 101 | + |
| 102 | +def _replace_special(text): |
| 103 | + """Handle our funky special-case strings.""" |
| 104 | + for original, replacement in PLOT_SCHEMA_REPLACEMENTS: |
| 105 | + text = text.replace(original, replacement) |
| 106 | + return text |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments