sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -0,0 +1,356 @@
#!/usr/bin/env python3
"""Generate a complete Threads page from one compact high-level JSON spec."""
from __future__ import annotations
import argparse
import copy
import json
import sys
from collections.abc import Mapping
from pathlib import Path
import render_threads_page as renderer
REPLY_NAMES = ("林夏", "周野", "陈默", "顾念", "沈言", "宋可心", "许知夏", "苏小鹿")
REPLY_COLORS = (
("#5662f6", "#9f45d8"),
("#15a88b", "#2e88cc"),
("#f97316", "#db2777"),
("#0f7d78", "#51b35e"),
)
REPLY_TEMPLATES = (
"关于“{topic}”,这个表达很有意思。",
"我也在关注“{topic}”,期待看到后续更新。",
"从另一个角度看,“{topic}”还有很多值得讨论的细节。",
"这条串文把“{topic}”说得很清楚。",
"先收藏,也想听听大家对“{topic}”的看法。",
"内容不长,但“{topic}”这个重点已经交代得很完整。",
"这正是我最近在思考的“{topic}”问题。",
"希望评论区可以继续理性讨论“{topic}”。",
)
def _read_spec(path: Path) -> dict[str, object]:
try:
value = json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError as exc:
raise renderer.DataValidationError(f"Invalid spec JSON: {exc}") from exc
if not isinstance(value, dict):
raise renderer.DataValidationError("Spec JSON must be an object")
return value
def _string(value: object, path: str, *, required: bool = False) -> str:
if value is None and not required:
return ""
if not isinstance(value, str) or (required and not value.strip()):
suffix = "a non-empty string" if required else "a string"
raise renderer.DataValidationError(f"{path} must be {suffix}")
return value.strip()
def _integer(value: object, path: str, *, default: int = 0) -> int:
if value is None:
return default
if not isinstance(value, int) or isinstance(value, bool) or value < 0:
raise renderer.DataValidationError(f"{path} must be a non-negative integer")
return value
def _boolean(value: object, path: str, *, default: bool = False) -> bool:
if value is None:
return default
if not isinstance(value, bool):
raise renderer.DataValidationError(f"{path} must be a boolean")
return value
def _strings(value: object, path: str) -> list[str]:
if value is None:
return []
if not isinstance(value, list) or not all(isinstance(item, str) and item.strip() for item in value):
raise renderer.DataValidationError(f"{path} must be an array of non-empty strings")
return [item.strip() for item in value]
def _require_chinese(value: str, path: str) -> None:
if not any("\u3400" <= char <= "\u9fff" for char in value):
raise renderer.DataValidationError(f"{path} must contain Simplified Chinese reader-visible text")
def _reply_objects(
count: int,
topics: list[str],
custom_replies: list[object],
) -> list[dict[str, object]]:
if not 0 <= count <= 8:
raise renderer.DataValidationError("displayReplyCount must be between 0 and 8")
if len(custom_replies) > count:
raise renderer.DataValidationError("displayReplyCount cannot be smaller than replies.length")
replies: list[dict[str, object]] = []
for index in range(count):
name = REPLY_NAMES[index % len(REPLY_NAMES)]
reply: dict[str, object] = {
"author": {
"name": name,
"initial": name[:1],
"colors": list(REPLY_COLORS[index % len(REPLY_COLORS)]),
},
"time": f"{index + 1} 分钟",
"text": REPLY_TEMPLATES[index % len(REPLY_TEMPLATES)].format(topic=topics[index % len(topics)]),
"metrics": {
"likes": max(0, (count - index) * 9),
"replies": index % 4,
"reposts": index % 2,
},
}
if index < len(custom_replies):
custom = custom_replies[index]
if isinstance(custom, str):
reply["text"] = custom.strip()
elif isinstance(custom, Mapping):
reply["text"] = _string(
custom.get("text"),
f"replies[{index}].text",
required=True,
)
if "author" in custom:
author_name = _string(
custom.get("author"),
f"replies[{index}].author",
required=True,
)
reply["author"]["name"] = author_name
reply["author"]["initial"] = author_name[:1]
if "time" in custom:
reply["time"] = _string(
custom.get("time"),
f"replies[{index}].time",
required=True,
)
metrics = reply["metrics"]
for key in ("likes", "replies", "reposts"):
if key in custom:
metrics[key] = _integer(
custom.get(key),
f"replies[{index}].{key}",
)
else:
raise renderer.DataValidationError(f"replies[{index}] must be a string or object")
_require_chinese(str(reply["text"]), f"replies[{index}].text")
replies.append(reply)
return replies
def _media(value: object, path: str) -> dict[str, object] | None:
if value is None:
return None
if not isinstance(value, Mapping):
raise renderer.DataValidationError(f"{path} must be an object or null")
media_type = _string(value.get("type"), f"{path}.type") or "image"
if media_type not in {"image", "video", "webpage"}:
raise renderer.DataValidationError(f"{path}.type must be image, video, or webpage")
media: dict[str, object] = {"type": media_type}
if media_type == "webpage":
media["pageUrl"] = _string(
value.get("pageUrl"),
f"{path}.pageUrl",
required=True,
)
media["title"] = _string(
value.get("title"),
f"{path}.title",
required=True,
)
media["description"] = _string(
value.get("description"),
f"{path}.description",
required=True,
)
else:
media["mediaUrl"] = _string(
value.get("url"),
f"{path}.url",
required=True,
)
media["alt"] = _string(value.get("alt"), f"{path}.alt") or "用户提供的媒体"
if value.get("pageUrl"):
media["pageUrl"] = _string(value.get("pageUrl"), f"{path}.pageUrl")
return media
def build_document(spec: dict[str, object]) -> dict[str, object]:
allowed = {
"accountName",
"handle",
"verified",
"description",
"pageTitle",
"topic",
"replyTopics",
"displayReplyCount",
"totalReplyCount",
"replies",
"postText",
"posts",
"media",
"likes",
"reposts",
"following",
"followers",
"threadCount",
}
unknown = sorted(set(spec) - allowed)
if unknown:
raise renderer.DataValidationError("Unknown spec keys: " + ", ".join(unknown))
account_name = _string(spec.get("accountName"), "accountName", required=True)
handle = _string(spec.get("handle"), "handle") or "@threads_user"
topic = _string(spec.get("topic"), "topic") or account_name
reply_topics = _strings(spec.get("replyTopics"), "replyTopics") or [topic]
raw_posts = spec.get("posts")
if raw_posts is None:
raw_posts = [
{
"text": _string(spec.get("postText"), "postText", required=True),
"media": spec.get("media") if "media" in spec else "__example__",
"likes": spec.get("likes", 0),
"reposts": spec.get("reposts", 0),
}
]
if not isinstance(raw_posts, list) or not 1 <= len(raw_posts) <= 10:
raise renderer.DataValidationError("posts must contain between 1 and 10 objects")
if not all(isinstance(item, Mapping) for item in raw_posts):
raise renderer.DataValidationError("posts must contain only objects")
data = renderer._read_json(renderer.DEFAULT_DATA_PATH)
defaults = data["threads"]["items"]
custom_replies = spec.get("replies", [])
if not isinstance(custom_replies, list):
raise renderer.DataValidationError("replies must be an array")
display_reply_count = _integer(
spec.get("displayReplyCount"),
"displayReplyCount",
default=max(3, len(custom_replies)),
)
total_reply_count = _integer(
spec.get("totalReplyCount"),
"totalReplyCount",
default=display_reply_count,
)
if total_reply_count < display_reply_count:
raise renderer.DataValidationError("totalReplyCount cannot be smaller than displayReplyCount")
items: list[dict[str, object]] = []
for index, raw_post in enumerate(raw_posts):
post = copy.deepcopy(defaults[index % len(defaults)])
text = _string(
raw_post.get("text"),
f"posts[{index}].text",
required=True,
)
_require_chinese(text, f"posts[{index}].text")
author_name = _string(raw_post.get("authorName"), f"posts[{index}].authorName") or account_name
post["id"] = f"thread-{index + 1:03d}"
post["author"] = {
"name": author_name,
"handle": _string(raw_post.get("handle"), f"posts[{index}].handle") or handle,
"initial": author_name[:1],
"colors": post["author"]["colors"],
"verified": _boolean(
raw_post.get("verified"),
f"posts[{index}].verified",
default=_boolean(spec.get("verified"), "verified"),
),
}
post["time"] = _string(raw_post.get("time"), f"posts[{index}].time") or "刚刚"
post["text"] = text
post["followable"] = _boolean(
raw_post.get("followable"),
f"posts[{index}].followable",
)
post["quote"] = None
if "media" in raw_post and raw_post.get("media") != "__example__":
post["media"] = _media(raw_post.get("media"), f"posts[{index}].media")
if index == 0:
replies = _reply_objects(display_reply_count, reply_topics, custom_replies)
replies_metric = total_reply_count
else:
replies = []
replies_metric = _integer(
raw_post.get("totalReplyCount"),
f"posts[{index}].totalReplyCount",
)
post["replies"] = replies
post["metrics"] = {
"likes": _integer(raw_post.get("likes"), f"posts[{index}].likes"),
"replies": replies_metric,
"reposts": _integer(raw_post.get("reposts"), f"posts[{index}].reposts"),
}
post["preview"] = f"查看全部 {replies_metric} 条回复"
items.append(post)
data["site"]["pageTitle"] = _string(spec.get("pageTitle"), "pageTitle") or f"Threads · {account_name}"
data["site"]["description"] = _string(spec.get("description"), "description") or f"{account_name} 的 Threads 中文页面。"
data["site"]["currentUser"].update(
{
"name": account_name,
"handle": handle,
"initial": account_name[:1],
"stats": {
"following": str(spec.get("following", "0")),
"followers": str(spec.get("followers", "0")),
"threads": str(spec.get("threadCount", len(items))),
},
}
)
data["threads"]["items"] = items
return data
def _next_paths(output_dir: Path) -> tuple[Path, Path]:
for index in range(1, 1001):
stem = "threads-page" if index == 1 else f"threads-page-{index}"
data_path = output_dir / f"{stem}-data.json"
html_path = output_dir / f"{stem}.html"
if not data_path.exists() and not html_path.exists():
return data_path, html_path
raise renderer.DataValidationError("Unable to allocate output filenames")
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--spec", type=Path, required=True)
parser.add_argument("--output-dir", type=Path, required=True)
args = parser.parse_args(argv)
try:
data = build_document(_read_spec(args.spec))
summary = renderer.validate_document(data)
rendered = renderer.render_document(
data,
renderer.TEMPLATE_PATH.read_text(encoding="utf-8"),
renderer.RUNTIME_PATH.read_text(encoding="utf-8"),
)
data_path, html_path = _next_paths(args.output_dir)
renderer._write_new(
data_path,
json.dumps(data, ensure_ascii=False, indent=2) + "\n",
)
renderer._write_new(html_path, rendered)
except (renderer.DataValidationError, OSError, UnicodeError) as exc:
renderer._emit({"status": "error", "action": "fast-threads-page", "error": str(exc)})
return 2
renderer._emit(
{
"status": "ok",
"action": "fast-threads-page",
"data": str(data_path),
"html": str(html_path),
**summary,
}
)
return 0
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,540 @@
#!/usr/bin/env python3
"""Validate Threads page data and inject it into the bundled HTML template."""
from __future__ import annotations
import argparse
import html as html_lib
import json
import re
import sys
from pathlib import Path
from typing import Any
from urllib.parse import urlparse
SKILL_ROOT = Path(__file__).resolve().parent.parent
ASSETS_DIR = SKILL_ROOT / "assets"
DEFAULT_DATA_PATH = ASSETS_DIR / "default-data.json"
TEMPLATE_PATH = ASSETS_DIR / "threads-template.html"
RUNTIME_PATH = ASSETS_DIR / "threads-runtime.js"
DATA_MARKER = "__THREADS_PAGE_DATA__"
RUNTIME_MARKER = "__THREADS_PAGE_RUNTIME__"
TITLE_RE = re.compile(r"<title>[^<]*</title>", re.IGNORECASE)
MODULES = (
"site",
"navigation",
"composer",
"threads",
"recommendations",
"activity",
)
THEMES = {"auto", "light", "dark"}
MEDIA_TYPES = {"autumn-grid", "image", "video", "webpage"}
REMOTE_MEDIA_TYPES = {"image", "video", "webpage"}
class DataValidationError(ValueError):
"""Raised when the Threads page JSON cannot satisfy the template contract."""
def _read_json(path: Path) -> dict[str, Any]:
try:
data = json.loads(path.read_text(encoding="utf-8"))
except FileNotFoundError as exc:
raise DataValidationError(f"JSON file does not exist: {path}") from exc
except json.JSONDecodeError as exc:
raise DataValidationError(
f"Invalid JSON at line {exc.lineno}, column {exc.colno}: {exc.msg}"
) from exc
if not isinstance(data, dict):
raise DataValidationError("Top-level JSON value must be an object")
return data
def _mapping(value: Any, path: str, errors: list[str]) -> dict[str, Any]:
if not isinstance(value, dict):
errors.append(f"{path} must be an object")
return {}
return value
def _array(
value: Any,
path: str,
errors: list[str],
*,
limit: int | None = None,
) -> list[Any]:
if not isinstance(value, list):
errors.append(f"{path} must be an array")
return []
if limit is not None and len(value) > limit:
errors.append(f"{path} may contain at most {limit} items")
return value
def _non_empty_string(value: Any, path: str, errors: list[str]) -> None:
if not isinstance(value, str) or not value.strip():
errors.append(f"{path} must be a non-empty string")
def _remote_url(value: Any, path: str, errors: list[str], *, required: bool = False, data_prefix: str = "") -> None:
if value in (None, "") and not required:
return
if not isinstance(value, str) or not value.strip():
errors.append(f"{path} must be a non-empty URL")
return
candidate = value.strip()
if data_prefix and candidate.lower().startswith(data_prefix):
return
parsed = urlparse(candidate)
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
errors.append(f"{path} must use an http(s) URL")
def _validate_remote_media(media: dict[str, Any], path: str, errors: list[str]) -> None:
media_type = media.get("type")
if media_type not in REMOTE_MEDIA_TYPES:
return
if media_type in {"image", "video"}:
_remote_url(media.get("mediaUrl"), f"{path}.mediaUrl", errors, required=True, data_prefix=f"data:{media_type}/")
if media_type == "video":
_remote_url(media.get("posterUrl"), f"{path}.posterUrl", errors, data_prefix="data:image/")
if media_type == "webpage":
_remote_url(media.get("pageUrl"), f"{path}.pageUrl", errors, required=True)
_remote_url(media.get("mediaUrl"), f"{path}.mediaUrl", errors, data_prefix="data:image/")
if "embed" in media and not isinstance(media.get("embed"), bool):
errors.append(f"{path}.embed must be a boolean")
else:
_remote_url(media.get("pageUrl"), f"{path}.pageUrl", errors)
def _non_negative_number(value: Any, path: str, errors: list[str]) -> None:
if (
not isinstance(value, (int, float))
or isinstance(value, bool)
or value < 0
):
errors.append(f"{path} must be a non-negative number")
def _integer_range(
value: Any,
path: str,
minimum: int,
maximum: int,
errors: list[str],
) -> None:
if (
not isinstance(value, int)
or isinstance(value, bool)
or not minimum <= value <= maximum
):
errors.append(f"{path} must be an integer from {minimum} to {maximum}")
def _boolean(value: Any, path: str, errors: list[str]) -> None:
if not isinstance(value, bool):
errors.append(f"{path} must be a boolean")
def _colors(value: Any, path: str, errors: list[str]) -> None:
if (
not isinstance(value, list)
or len(value) != 2
or not all(isinstance(color, str) for color in value)
):
errors.append(f"{path} must contain exactly two color strings")
def _person(
value: Any,
path: str,
errors: list[str],
*,
handle: bool,
) -> dict[str, Any]:
person = _mapping(value, path, errors)
keys = ["name", "initial"]
if handle:
keys.append("handle")
for key in keys:
_non_empty_string(person.get(key), f"{path}.{key}", errors)
_colors(person.get("colors"), f"{path}.colors", errors)
return person
def _metrics(value: Any, path: str, errors: list[str]) -> None:
metrics = _mapping(value, path, errors)
for key in ("likes", "replies", "reposts"):
_non_negative_number(metrics.get(key), f"{path}.{key}", errors)
def validate_document(data: dict[str, Any]) -> dict[str, Any]:
errors: list[str] = []
for module in MODULES:
if module not in data:
errors.append(f"Missing top-level module: {module}")
site = _mapping(data.get("site"), "site", errors)
for key in ("brand", "pageTitle", "description"):
_non_empty_string(site.get(key), f"site.{key}", errors)
if site.get("theme") not in THEMES:
errors.append("site.theme must be one of: auto, dark, light")
current_user = _person(
site.get("currentUser"),
"site.currentUser",
errors,
handle=True,
)
stats = _mapping(
current_user.get("stats"),
"site.currentUser.stats",
errors,
)
for key in ("following", "followers", "threads"):
_non_empty_string(
stats.get(key),
f"site.currentUser.stats.{key}",
errors,
)
navigation = _mapping(data.get("navigation"), "navigation", errors)
for key in (
"feedTitle",
"feedFilter",
"searchPlaceholder",
"searchHints",
"copyright",
):
_non_empty_string(navigation.get(key), f"navigation.{key}", errors)
tabs = _array(navigation.get("tabs"), "navigation.tabs", errors, limit=4)
if not tabs:
errors.append("navigation.tabs must contain at least one item")
for index, label in enumerate(tabs):
_non_empty_string(label, f"navigation.tabs[{index}]", errors)
mobile_labels = _array(
navigation.get("mobileLabels"),
"navigation.mobileLabels",
errors,
limit=5,
)
if len(mobile_labels) != 5:
errors.append("navigation.mobileLabels must contain exactly 5 items")
for index, label in enumerate(mobile_labels):
_non_empty_string(
label,
f"navigation.mobileLabels[{index}]",
errors,
)
footer_links = _array(
navigation.get("footerLinks"),
"navigation.footerLinks",
errors,
limit=8,
)
for index, label in enumerate(footer_links):
_non_empty_string(
label,
f"navigation.footerLinks[{index}]",
errors,
)
composer = _mapping(data.get("composer"), "composer", errors)
for key in (
"inlinePlaceholder",
"dialogTitle",
"textareaPlaceholder",
"publishLabel",
):
_non_empty_string(composer.get(key), f"composer.{key}", errors)
_integer_range(
composer.get("maxLength"),
"composer.maxLength",
1,
2000,
errors,
)
_integer_range(
composer.get("replyMaxLength"),
"composer.replyMaxLength",
1,
500,
errors,
)
threads = _mapping(data.get("threads"), "threads", errors)
for key in ("emptyTitle", "emptyText"):
_non_empty_string(threads.get(key), f"threads.{key}", errors)
thread_items = _array(
threads.get("items"),
"threads.items",
errors,
limit=30,
)
if not thread_items:
errors.append("threads.items must contain at least one thread")
thread_ids: set[str] = set()
reply_count = 0
for index, value in enumerate(thread_items):
path = f"threads.items[{index}]"
thread = _mapping(value, path, errors)
thread_id = thread.get("id")
_non_empty_string(thread_id, f"{path}.id", errors)
if isinstance(thread_id, str):
if thread_id in thread_ids:
errors.append(f"{path}.id must be unique")
thread_ids.add(thread_id)
author = _person(
thread.get("author"),
f"{path}.author",
errors,
handle=True,
)
_boolean(
author.get("verified"),
f"{path}.author.verified",
errors,
)
for key in ("time", "text", "preview"):
_non_empty_string(thread.get(key), f"{path}.{key}", errors)
_boolean(thread.get("followable"), f"{path}.followable", errors)
_metrics(thread.get("metrics"), f"{path}.metrics", errors)
media = thread.get("media")
if media is not None:
media_data = _mapping(media, f"{path}.media", errors)
if media_data.get("type") not in MEDIA_TYPES:
errors.append(
f"{path}.media.type must be one of: "
f"{', '.join(sorted(MEDIA_TYPES))}"
)
_validate_remote_media(media_data, f"threads.items[{index}].media", errors)
if "label" in media_data:
_non_empty_string(
media_data.get("label"),
f"{path}.media.label",
errors,
)
quote = thread.get("quote")
if quote is not None:
quote_data = _person(
quote,
f"{path}.quote",
errors,
handle=True,
)
_non_empty_string(
quote_data.get("text"),
f"{path}.quote.text",
errors,
)
replies = _array(
thread.get("replies"),
f"{path}.replies",
errors,
limit=8,
)
reply_count += len(replies)
for reply_index, reply_value in enumerate(replies):
reply_path = f"{path}.replies[{reply_index}]"
reply = _mapping(reply_value, reply_path, errors)
_person(
reply.get("author"),
f"{reply_path}.author",
errors,
handle=False,
)
for key in ("time", "text"):
_non_empty_string(
reply.get(key),
f"{reply_path}.{key}",
errors,
)
_metrics(reply.get("metrics"), f"{reply_path}.metrics", errors)
recommendations = _mapping(
data.get("recommendations"),
"recommendations",
errors,
)
for key in ("followTitle", "topicTitle"):
_non_empty_string(
recommendations.get(key),
f"recommendations.{key}",
errors,
)
people = _array(
recommendations.get("people"),
"recommendations.people",
errors,
limit=20,
)
for index, value in enumerate(people):
_person(
value,
f"recommendations.people[{index}]",
errors,
handle=True,
)
topics = _array(
recommendations.get("topics"),
"recommendations.topics",
errors,
limit=20,
)
for index, value in enumerate(topics):
topic = _mapping(
value,
f"recommendations.topics[{index}]",
errors,
)
for key in ("meta", "title", "count"):
_non_empty_string(
topic.get(key),
f"recommendations.topics[{index}].{key}",
errors,
)
activity = _mapping(data.get("activity"), "activity", errors)
_non_empty_string(activity.get("title"), "activity.title", errors)
activity_items = _array(
activity.get("items"),
"activity.items",
errors,
limit=30,
)
for index, value in enumerate(activity_items):
path = f"activity.items[{index}]"
item = _person(value, path, errors, handle=False)
for key in ("text", "time"):
_non_empty_string(item.get(key), f"{path}.{key}", errors)
_boolean(item.get("followable"), f"{path}.followable", errors)
if errors:
raise DataValidationError("; ".join(errors))
return {
"modules": list(MODULES),
"counts": {
"threads": len(thread_items),
"replies": reply_count,
"people": len(people),
"topics": len(topics),
"activity": len(activity_items),
},
}
def render_document(data: dict[str, Any], template: str, runtime: str) -> str:
validate_document(data)
if template.count(DATA_MARKER) != 1:
raise DataValidationError(
"Bundled template must contain exactly one data marker"
)
if template.count(RUNTIME_MARKER) != 1:
raise DataValidationError(
"Bundled template must contain exactly one runtime marker"
)
serialized = json.dumps(data, ensure_ascii=False, indent=2).replace(
"</",
"<\\/",
)
safe_runtime = runtime.replace("</script", "<\\/script")
rendered = template.replace(DATA_MARKER, serialized)
rendered = rendered.replace(RUNTIME_MARKER, safe_runtime)
page_title = str((data.get("site") or {}).get("pageTitle") or "").strip()
if page_title:
rendered = TITLE_RE.sub(
f"<title>{html_lib.escape(page_title)}</title>",
rendered,
count=1,
)
return rendered
def _write_new(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
try:
with path.open("x", encoding="utf-8", newline="\n") as handle:
handle.write(content)
except FileExistsError as exc:
raise DataValidationError(
f"Refusing to overwrite existing file: {path}"
) from exc
def _emit(payload: dict[str, Any]) -> None:
print(json.dumps(payload, ensure_ascii=False))
def _command_init(output: Path) -> dict[str, Any]:
data = _read_json(DEFAULT_DATA_PATH)
summary = validate_document(data)
_write_new(
output,
json.dumps(data, ensure_ascii=False, indent=2) + "\n",
)
return {"status": "ok", "action": "init", "output": str(output), **summary}
def _command_validate(input_path: Path) -> dict[str, Any]:
data = _read_json(input_path)
summary = validate_document(data)
return {"status": "ok", "action": "validate", "input": str(input_path), **summary}
def _command_render(input_path: Path, output: Path) -> dict[str, Any]:
data = _read_json(input_path)
template = TEMPLATE_PATH.read_text(encoding="utf-8")
runtime = RUNTIME_PATH.read_text(encoding="utf-8")
rendered = render_document(data, template, runtime)
_write_new(output, rendered)
summary = validate_document(data)
return {
"status": "ok",
"action": "render",
"input": str(input_path),
"output": str(output),
**summary,
}
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
subparsers = parser.add_subparsers(dest="command", required=True)
init_parser = subparsers.add_parser("init", help="Create a new editable JSON document")
init_parser.add_argument("--output", type=Path, required=True)
validate_parser = subparsers.add_parser("validate", help="Validate an existing JSON document")
validate_parser.add_argument("--input", type=Path, required=True)
render_parser = subparsers.add_parser("render", help="Render validated JSON into the bundled HTML template")
render_parser.add_argument("--input", type=Path, required=True)
render_parser.add_argument("--output", type=Path, required=True)
return parser
def main(argv: list[str] | None = None) -> int:
parser = _build_parser()
args = parser.parse_args(argv)
try:
if args.command == "init":
result = _command_init(args.output)
elif args.command == "validate":
result = _command_validate(args.input)
else:
result = _command_render(args.input, args.output)
except (DataValidationError, OSError) as exc:
_emit({"status": "error", "action": args.command, "error": str(exc)})
return 2
_emit(result)
return 0
if __name__ == "__main__":
sys.exit(main())