Skip to content

Commit bcaeae7

Browse files
committed
starting to build replacement for Jekyll
1 parent 6375ea9 commit bcaeae7

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

docs/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Manage plotly.js documentation.
22

33
JEKYLL=bundle exec jekyll
4+
RUN = uv run
45
SCHEMA_SRC=../test/plot-schema.json
56
SCHEMA_DST=_data/plotschema.json
67

@@ -14,6 +15,11 @@ build:
1415
cp ${SCHEMA_SRC} ${SCHEMA_DST}
1516
${JEKYLL} build
1617

18+
## reference: build reference documentation in ./tmp
19+
reference:
20+
@mkdir -p tmp
21+
${RUN} bin/make_reference_pages.py --schema ${SCHEMA_SRC} --outdir tmp _posts/reference_pages/javascript/2020-07-20-bar.html
22+
1723
## serve: display documentation
1824
serve:
1925
@mkdir -p _data

docs/bin/make_reference_pages.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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()

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[project]
2+
name = "plotly.js"
3+
description = "Plotly JavaScript charting library"
4+
version = "3.1.0"
5+
requires-python = ">=3.12"
6+
dependencies = []

uv.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)