sync update from LAN repo (source master @ ca48c4ec)
This commit is contained in:
1 parent
8bc8b0e082
commit
ea9dcff9c0
873 files changed
+107866
-14011
No files matched your search
+372
@@ -0,0 +1,372 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate a complete CNN-style Chinese page from a 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_cnn_page as renderer
|
||||
|
||||
LATEST_VISUALS = ("coast", "city", "deep-sea")
|
||||
VIDEO_VISUALS = ("night-city", "space", "music")
|
||||
WORLD_VISUALS = ("trade", "market", "payments", "energy")
|
||||
|
||||
|
||||
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 _strings(value: object, path: str, *, limit: int) -> list[str]:
|
||||
if value is None:
|
||||
return []
|
||||
if not isinstance(value, list) or not 1 <= len(value) <= limit:
|
||||
raise renderer.DataValidationError(f"{path} must contain between 1 and {limit} strings")
|
||||
result: list[str] = []
|
||||
for index, item in enumerate(value):
|
||||
text = _string(item, f"{path}[{index}]", required=True)
|
||||
_require_chinese(text, f"{path}[{index}]")
|
||||
result.append(text)
|
||||
return result
|
||||
|
||||
|
||||
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 _optional_chinese(spec: Mapping[str, object], key: str) -> str:
|
||||
value = _string(spec.get(key), key)
|
||||
if value:
|
||||
_require_chinese(value, key)
|
||||
return value
|
||||
|
||||
|
||||
def _media(value: object) -> dict[str, object] | None:
|
||||
if value is None:
|
||||
return None
|
||||
if not isinstance(value, Mapping):
|
||||
raise renderer.DataValidationError("media must be an object or null")
|
||||
media_type = _string(value.get("type"), "media.type") or "image"
|
||||
if media_type not in {"image", "video", "webpage"}:
|
||||
raise renderer.DataValidationError("media.type must be image, video, or webpage")
|
||||
media: dict[str, object] = {"type": media_type}
|
||||
if media_type == "webpage":
|
||||
media["pageUrl"] = _string(
|
||||
value.get("pageUrl"),
|
||||
"media.pageUrl",
|
||||
required=True,
|
||||
)
|
||||
media["title"] = _string(
|
||||
value.get("title"),
|
||||
"media.title",
|
||||
required=True,
|
||||
)
|
||||
if value.get("description"):
|
||||
media["description"] = _string(
|
||||
value.get("description"),
|
||||
"media.description",
|
||||
)
|
||||
media["embed"] = bool(value.get("embed", False))
|
||||
else:
|
||||
media["mediaUrl"] = _string(
|
||||
value.get("url"),
|
||||
"media.url",
|
||||
required=True,
|
||||
)
|
||||
media["alt"] = _string(value.get("alt"), "media.alt") or "用户提供的新闻媒体"
|
||||
if value.get("pageUrl"):
|
||||
media["pageUrl"] = _string(value.get("pageUrl"), "media.pageUrl")
|
||||
if media_type == "video" and value.get("posterUrl"):
|
||||
media["posterUrl"] = _string(
|
||||
value.get("posterUrl"),
|
||||
"media.posterUrl",
|
||||
)
|
||||
return media
|
||||
|
||||
|
||||
def _replace_story_titles(
|
||||
defaults: list[dict[str, object]],
|
||||
titles: list[str],
|
||||
*,
|
||||
prefix: str,
|
||||
visuals: tuple[str, ...],
|
||||
include_summary: bool,
|
||||
topic: str,
|
||||
) -> list[dict[str, object]]:
|
||||
items: list[dict[str, object]] = []
|
||||
for index, title in enumerate(titles):
|
||||
item = copy.deepcopy(defaults[index % len(defaults)])
|
||||
item["id"] = f"{prefix}-{index + 1:02d}"
|
||||
item["title"] = title
|
||||
item["visual"] = visuals[index % len(visuals)]
|
||||
if include_summary:
|
||||
item["kicker"] = "专题"
|
||||
item["summary"] = f"围绕{topic}整理的中文示意报道,页面内容可在设置中继续编辑。"
|
||||
item["time"] = f"{index + 1} 小时前 · 示意"
|
||||
elif "meta" in item:
|
||||
item["meta"] = "视频示意"
|
||||
item["duration"] = f"0{index + 3}:1{index}"
|
||||
else:
|
||||
item["kicker"] = "观察"
|
||||
item["time"] = f"{index + 1} 小时前 · 示意"
|
||||
items.append(item)
|
||||
return items
|
||||
|
||||
|
||||
def build_document(spec: dict[str, object]) -> dict[str, object]:
|
||||
allowed = {
|
||||
"topic",
|
||||
"headline",
|
||||
"headlineSummary",
|
||||
"breaking",
|
||||
"pageTitle",
|
||||
"description",
|
||||
"theme",
|
||||
"kicker",
|
||||
"byline",
|
||||
"latestHeadlines",
|
||||
"videoHeadlines",
|
||||
"worldHeadlines",
|
||||
"sideHeadlines",
|
||||
"updates",
|
||||
"analysisTitle",
|
||||
"analysisText",
|
||||
"media",
|
||||
}
|
||||
unknown = sorted(set(spec) - allowed)
|
||||
if unknown:
|
||||
raise renderer.DataValidationError("Unknown spec keys: " + ", ".join(unknown))
|
||||
|
||||
topic = _string(spec.get("topic"), "topic", required=True)
|
||||
headline = _optional_chinese(spec, "headline") or f"{topic}发布最新计划,中文专题页面同步上线"
|
||||
headline_summary = _optional_chinese(spec, "headlineSummary") or (f"本页面围绕{topic}整理品牌动态、内容观察与社区反馈,所有新闻和数据均为中文示意内容。")
|
||||
breaking = _optional_chinese(spec, "breaking") or f"{topic}成为今日中文专题关注焦点"
|
||||
page_title = _optional_chinese(spec, "pageTitle") or f"CNN 中文专题 · {topic}"
|
||||
description = _optional_chinese(spec, "description") or f"围绕{topic}制作的 CNN 风格中文新闻示意页面。"
|
||||
kicker = _optional_chinese(spec, "kicker") or "特别报道 · 中文示意"
|
||||
byline = _string(spec.get("byline"), "byline") or "CNN 中文编辑台"
|
||||
theme = _string(spec.get("theme"), "theme") or "light"
|
||||
if theme not in renderer.THEMES:
|
||||
raise renderer.DataValidationError("theme must be auto, light, or dark")
|
||||
|
||||
latest_titles = _strings(spec.get("latestHeadlines"), "latestHeadlines", limit=12) or [
|
||||
f"{topic}推出新计划,首批体验活动正式启动",
|
||||
f"从创意到产品:{topic}团队分享设计思路",
|
||||
f"社区观察:用户如何参与{topic}内容共创",
|
||||
]
|
||||
video_titles = _strings(spec.get("videoHeadlines"), "videoHeadlines", limit=8) or [
|
||||
f"现场直击:走进{topic}的创作空间",
|
||||
f"三分钟了解{topic}最新进展",
|
||||
f"幕后故事:{topic}团队讲述项目历程",
|
||||
]
|
||||
world_titles = _strings(spec.get("worldHeadlines"), "worldHeadlines", limit=16) or [
|
||||
f"{topic}跨区域合作计划公布",
|
||||
f"市场观察:{topic}相关讨论持续升温",
|
||||
f"数字内容让{topic}触达更多社区",
|
||||
f"可持续实践成为{topic}下一阶段重点",
|
||||
]
|
||||
side_titles = _strings(spec.get("sideHeadlines"), "sideHeadlines", limit=4) or [
|
||||
f"{topic}如何把创意转化为日常体验",
|
||||
f"团队访谈:{topic}背后的设计选择",
|
||||
]
|
||||
update_titles = _strings(spec.get("updates"), "updates", limit=12) or [
|
||||
f"{topic}专题页面完成首轮内容更新",
|
||||
"中文示意报道与栏目内容已同步",
|
||||
"读者可通过设置按钮继续编辑页面数据",
|
||||
]
|
||||
analysis_title = _optional_chinese(spec, "analysisTitle") or f"{topic}为何能够持续获得关注?"
|
||||
analysis_text = _optional_chinese(spec, "analysisText") or (f"{topic}把清晰的内容表达、稳定的视觉识别和持续的社区互动结合起来。真正的考验在于后续内容能否保持一致,并形成可持续的参与机制。")
|
||||
|
||||
data = renderer._read_json(renderer.DEFAULT_DATA_PATH)
|
||||
data["site"].update(
|
||||
{
|
||||
"pageTitle": page_title,
|
||||
"description": description,
|
||||
"theme": theme,
|
||||
"date": "中文示意页面 · 非实时新闻",
|
||||
"utilityLinks": ["专题", "新闻简报"],
|
||||
"marketSummary": [
|
||||
{"label": "关注度", "value": "示意", "direction": "flat"},
|
||||
{"label": "讨论度", "value": "非实时", "direction": "flat"},
|
||||
],
|
||||
"weather": "页面内容为中文示意",
|
||||
"frontLabel": f"头版 · {topic}",
|
||||
}
|
||||
)
|
||||
data["navigation"]["drawerFooter"] = "CNN 中文专题 · 概念页面"
|
||||
data["breaking"].update({"label": "快讯", "text": breaking, "time": "刚刚 · 示意"})
|
||||
|
||||
primary = data["lead"]["primary"]
|
||||
primary.update(
|
||||
{
|
||||
"kicker": kicker,
|
||||
"title": headline,
|
||||
"summary": headline_summary,
|
||||
"byline": byline,
|
||||
"updated": "更新于刚刚 · 示意",
|
||||
"readingTime": "阅读 5 分钟",
|
||||
"visualLabel": f"{topic}中文专题示意视觉",
|
||||
"tag": "专题",
|
||||
"credit": "CNN / 中文示意视觉",
|
||||
}
|
||||
)
|
||||
if "media" in spec:
|
||||
media = _media(spec.get("media"))
|
||||
if media is None:
|
||||
primary.pop("media", None)
|
||||
else:
|
||||
primary["media"] = media
|
||||
|
||||
data["lead"]["sideStories"] = [
|
||||
{
|
||||
"kicker": "延伸阅读",
|
||||
"title": title,
|
||||
"summary": f"从不同角度理解{topic}的内容表达与用户体验。",
|
||||
"visual": "train" if index == 0 else None,
|
||||
}
|
||||
for index, title in enumerate(side_titles)
|
||||
]
|
||||
data["lead"]["updatesTitle"] = "专题更新"
|
||||
data["lead"]["updates"] = [{"time": f"0{9 + index}:0{index}", "text": text} for index, text in enumerate(update_titles)]
|
||||
|
||||
data["latest"]["title"] = "最新报道"
|
||||
data["latest"]["items"] = _replace_story_titles(
|
||||
data["latest"]["items"],
|
||||
latest_titles,
|
||||
prefix="latest",
|
||||
visuals=LATEST_VISUALS,
|
||||
include_summary=True,
|
||||
topic=topic,
|
||||
)
|
||||
data["video"].update(
|
||||
{
|
||||
"title": "视频与现场",
|
||||
"modalTitle": "CNN 中文视频",
|
||||
"liveTitle": "专题视频",
|
||||
"liveSubtitle": f"正在播放:{topic}",
|
||||
"modalNote": "视频内容为互动示意,可通过上传按钮替换素材。",
|
||||
}
|
||||
)
|
||||
data["video"]["items"] = _replace_story_titles(
|
||||
data["video"]["items"],
|
||||
video_titles,
|
||||
prefix="video",
|
||||
visuals=VIDEO_VISUALS,
|
||||
include_summary=False,
|
||||
topic=topic,
|
||||
)
|
||||
data["world"]["title"] = f"{topic}与相关观察"
|
||||
data["world"]["items"] = _replace_story_titles(
|
||||
data["world"]["items"],
|
||||
world_titles,
|
||||
prefix="world",
|
||||
visuals=WORLD_VISUALS,
|
||||
include_summary=False,
|
||||
topic=topic,
|
||||
)
|
||||
data["world"]["analysis"].update(
|
||||
{
|
||||
"kicker": "CNN 中文分析 · 示意",
|
||||
"title": analysis_title,
|
||||
"text": analysis_text,
|
||||
"author": "中文专题编辑部",
|
||||
"role": "页面内容策划",
|
||||
"initial": "编",
|
||||
}
|
||||
)
|
||||
data["markets"].update(
|
||||
{
|
||||
"title": "趋势快照 · 示意数据",
|
||||
"moreLabel": "非实时数据 →",
|
||||
"items": [
|
||||
{"name": "内容关注", "value": "示意", "change": "非实时", "direction": "flat"},
|
||||
{"name": "社区讨论", "value": "示意", "change": "非实时", "direction": "flat"},
|
||||
{"name": "视频热度", "value": "示意", "change": "非实时", "direction": "flat"},
|
||||
{"name": "专题更新", "value": "示意", "change": "非实时", "direction": "flat"},
|
||||
],
|
||||
}
|
||||
)
|
||||
data["newsletter"].update(
|
||||
{
|
||||
"title": f"订阅{topic}中文专题更新",
|
||||
"text": "接收页面内容更新提醒;本表单仅用于交互演示。",
|
||||
"button": "演示订阅",
|
||||
"successPrefix": "演示订阅信息已记录:",
|
||||
}
|
||||
)
|
||||
data["footer"].update(
|
||||
{
|
||||
"tagline": f"从中文专题出发,持续关注{topic}。",
|
||||
"copyright": "© 2026 CNN 风格概念页面。本页面内容均为中文示意。",
|
||||
"legal": "隐私 · 条款 · 内容说明",
|
||||
}
|
||||
)
|
||||
return data
|
||||
|
||||
|
||||
def _next_paths(output_dir: Path) -> tuple[Path, Path]:
|
||||
for index in range(1, 1001):
|
||||
stem = "cnn-page" if index == 1 else f"cnn-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-cnn-page", "error": str(exc)})
|
||||
return 2
|
||||
renderer._emit(
|
||||
{
|
||||
"status": "ok",
|
||||
"action": "fast-cnn-page",
|
||||
"data": str(data_path),
|
||||
"html": str(html_path),
|
||||
**summary,
|
||||
}
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
+571
@@ -0,0 +1,571 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Validate CNN 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 / "cnn-template.html"
|
||||
RUNTIME_PATH = ASSETS_DIR / "cnn-runtime.js"
|
||||
DATA_MARKER = "__CNN_PAGE_DATA__"
|
||||
RUNTIME_MARKER = "__CNN_PAGE_RUNTIME__"
|
||||
TITLE_RE = re.compile(r"<title>[^<]*</title>", re.IGNORECASE)
|
||||
MODULES = (
|
||||
"site",
|
||||
"navigation",
|
||||
"breaking",
|
||||
"lead",
|
||||
"latest",
|
||||
"video",
|
||||
"world",
|
||||
"markets",
|
||||
"newsletter",
|
||||
"footer",
|
||||
)
|
||||
THEMES = {"auto", "light", "dark"}
|
||||
DIRECTIONS = {"up", "down", "flat"}
|
||||
LATEST_VISUALS = {"coast", "city", "deep-sea"}
|
||||
VIDEO_VISUALS = {"night-city", "space", "music"}
|
||||
WORLD_VISUALS = {"trade", "market", "payments", "energy"}
|
||||
REMOTE_MEDIA_TYPES = {"image", "video", "webpage"}
|
||||
|
||||
|
||||
class DataValidationError(ValueError):
|
||||
"""Raised when the CNN 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_tree(value: Any, path: str, errors: list[str]) -> None:
|
||||
if isinstance(value, list):
|
||||
for index, item in enumerate(value):
|
||||
_validate_remote_media_tree(item, f"{path}[{index}]", errors)
|
||||
return
|
||||
if not isinstance(value, dict):
|
||||
return
|
||||
media = value.get("media")
|
||||
if isinstance(media, dict) and "type" in media:
|
||||
media_path = f"{path}.media"
|
||||
media_data = _mapping(media, media_path, errors)
|
||||
media_type = media_data.get("type")
|
||||
if media_type not in REMOTE_MEDIA_TYPES:
|
||||
errors.append(f"{media_path}.type must be one of: {', '.join(sorted(REMOTE_MEDIA_TYPES))}")
|
||||
elif media_type in {"image", "video"}:
|
||||
_remote_url(media_data.get("mediaUrl"), f"{media_path}.mediaUrl", errors, required=True, data_prefix=f"data:{media_type}/")
|
||||
if media_type == "video":
|
||||
_remote_url(media_data.get("posterUrl"), f"{media_path}.posterUrl", errors, data_prefix="data:image/")
|
||||
if media_type == "webpage":
|
||||
_remote_url(media_data.get("pageUrl"), f"{media_path}.pageUrl", errors, required=True)
|
||||
_remote_url(media_data.get("mediaUrl"), f"{media_path}.mediaUrl", errors, data_prefix="data:image/")
|
||||
if "embed" in media_data and not isinstance(media_data.get("embed"), bool):
|
||||
errors.append(f"{media_path}.embed must be a boolean")
|
||||
elif media_type in {"image", "video"}:
|
||||
_remote_url(media_data.get("pageUrl"), f"{media_path}.pageUrl", errors)
|
||||
for key, item in value.items():
|
||||
if key != "media":
|
||||
_validate_remote_media_tree(item, f"{path}.{key}", errors)
|
||||
|
||||
|
||||
def _href(value: Any, path: str, errors: list[str]) -> None:
|
||||
_non_empty_string(value, path, errors)
|
||||
if isinstance(value, str) and not value.startswith("#"):
|
||||
errors.append(f"{path} must be an in-page anchor starting with #")
|
||||
|
||||
|
||||
def _direction(value: Any, path: str, errors: list[str]) -> None:
|
||||
if value not in DIRECTIONS:
|
||||
errors.append(f"{path} must be one of: down, flat, up")
|
||||
|
||||
|
||||
def _strings(
|
||||
value: dict[str, Any],
|
||||
keys: tuple[str, ...],
|
||||
path: str,
|
||||
errors: list[str],
|
||||
) -> None:
|
||||
for key in keys:
|
||||
_non_empty_string(value.get(key), f"{path}.{key}", errors)
|
||||
|
||||
|
||||
def _validate_story_items(
|
||||
items: list[Any],
|
||||
path: str,
|
||||
errors: list[str],
|
||||
*,
|
||||
visuals: set[str],
|
||||
include_summary: bool,
|
||||
include_duration: bool = False,
|
||||
) -> None:
|
||||
seen: set[str] = set()
|
||||
for index, value in enumerate(items):
|
||||
item_path = f"{path}[{index}]"
|
||||
item = _mapping(value, item_path, errors)
|
||||
item_id = item.get("id")
|
||||
_non_empty_string(item_id, f"{item_path}.id", errors)
|
||||
if isinstance(item_id, str):
|
||||
if item_id in seen:
|
||||
errors.append(f"{item_path}.id must be unique")
|
||||
seen.add(item_id)
|
||||
keys = ["title"]
|
||||
if include_summary:
|
||||
keys.extend(["kicker", "summary", "time"])
|
||||
elif include_duration:
|
||||
keys.extend(["meta", "duration"])
|
||||
else:
|
||||
keys.extend(["kicker", "time"])
|
||||
_strings(item, tuple(keys), item_path, errors)
|
||||
if item.get("visual") not in visuals:
|
||||
errors.append(
|
||||
f"{item_path}.visual must be one of: "
|
||||
f"{', '.join(sorted(visuals))}"
|
||||
)
|
||||
|
||||
|
||||
def validate_document(data: dict[str, Any]) -> dict[str, Any]:
|
||||
errors: list[str] = []
|
||||
_validate_remote_media_tree(data, "$", errors)
|
||||
for module in MODULES:
|
||||
if module not in data:
|
||||
errors.append(f"Missing top-level module: {module}")
|
||||
|
||||
site = _mapping(data.get("site"), "site", errors)
|
||||
_strings(
|
||||
site,
|
||||
("brand", "pageTitle", "description", "date", "weather", "frontLabel"),
|
||||
"site",
|
||||
errors,
|
||||
)
|
||||
if site.get("theme") not in THEMES:
|
||||
errors.append("site.theme must be one of: auto, dark, light")
|
||||
utility_links = _array(
|
||||
site.get("utilityLinks"),
|
||||
"site.utilityLinks",
|
||||
errors,
|
||||
limit=6,
|
||||
)
|
||||
for index, value in enumerate(utility_links):
|
||||
_non_empty_string(value, f"site.utilityLinks[{index}]", errors)
|
||||
summary_items = _array(
|
||||
site.get("marketSummary"),
|
||||
"site.marketSummary",
|
||||
errors,
|
||||
limit=6,
|
||||
)
|
||||
for index, value in enumerate(summary_items):
|
||||
item = _mapping(value, f"site.marketSummary[{index}]", errors)
|
||||
_strings(
|
||||
item,
|
||||
("label", "value"),
|
||||
f"site.marketSummary[{index}]",
|
||||
errors,
|
||||
)
|
||||
_direction(
|
||||
item.get("direction"),
|
||||
f"site.marketSummary[{index}].direction",
|
||||
errors,
|
||||
)
|
||||
|
||||
navigation = _mapping(data.get("navigation"), "navigation", errors)
|
||||
_strings(
|
||||
navigation,
|
||||
("searchPlaceholder", "searchButton", "liveLabel", "drawerFooter"),
|
||||
"navigation",
|
||||
errors,
|
||||
)
|
||||
nav_items = _array(
|
||||
navigation.get("items"),
|
||||
"navigation.items",
|
||||
errors,
|
||||
limit=10,
|
||||
)
|
||||
if not nav_items:
|
||||
errors.append("navigation.items must contain at least one item")
|
||||
for index, value in enumerate(nav_items):
|
||||
item = _mapping(value, f"navigation.items[{index}]", errors)
|
||||
_non_empty_string(
|
||||
item.get("label"),
|
||||
f"navigation.items[{index}].label",
|
||||
errors,
|
||||
)
|
||||
_href(item.get("href"), f"navigation.items[{index}].href", errors)
|
||||
|
||||
breaking = _mapping(data.get("breaking"), "breaking", errors)
|
||||
_strings(breaking, ("label", "text", "time"), "breaking", errors)
|
||||
|
||||
lead = _mapping(data.get("lead"), "lead", errors)
|
||||
primary = _mapping(lead.get("primary"), "lead.primary", errors)
|
||||
_strings(
|
||||
primary,
|
||||
(
|
||||
"kicker",
|
||||
"title",
|
||||
"summary",
|
||||
"byline",
|
||||
"updated",
|
||||
"readingTime",
|
||||
"visualLabel",
|
||||
"tag",
|
||||
"credit",
|
||||
),
|
||||
"lead.primary",
|
||||
errors,
|
||||
)
|
||||
if primary.get("visual") != "summit":
|
||||
errors.append("lead.primary.visual must be: summit")
|
||||
side_stories = _array(
|
||||
lead.get("sideStories"),
|
||||
"lead.sideStories",
|
||||
errors,
|
||||
limit=4,
|
||||
)
|
||||
for index, value in enumerate(side_stories):
|
||||
item_path = f"lead.sideStories[{index}]"
|
||||
item = _mapping(value, item_path, errors)
|
||||
_strings(item, ("kicker", "title", "summary"), item_path, errors)
|
||||
if item.get("visual") not in {None, "train"}:
|
||||
errors.append(f"{item_path}.visual must be train or null")
|
||||
_non_empty_string(
|
||||
lead.get("updatesTitle"),
|
||||
"lead.updatesTitle",
|
||||
errors,
|
||||
)
|
||||
updates = _array(
|
||||
lead.get("updates"),
|
||||
"lead.updates",
|
||||
errors,
|
||||
limit=12,
|
||||
)
|
||||
for index, value in enumerate(updates):
|
||||
item = _mapping(value, f"lead.updates[{index}]", errors)
|
||||
_strings(item, ("time", "text"), f"lead.updates[{index}]", errors)
|
||||
|
||||
latest = _mapping(data.get("latest"), "latest", errors)
|
||||
_strings(latest, ("title", "moreLabel"), "latest", errors)
|
||||
latest_items = _array(
|
||||
latest.get("items"),
|
||||
"latest.items",
|
||||
errors,
|
||||
limit=12,
|
||||
)
|
||||
if not latest_items:
|
||||
errors.append("latest.items must contain at least one item")
|
||||
_validate_story_items(
|
||||
latest_items,
|
||||
"latest.items",
|
||||
errors,
|
||||
visuals=LATEST_VISUALS,
|
||||
include_summary=True,
|
||||
)
|
||||
|
||||
video = _mapping(data.get("video"), "video", errors)
|
||||
_strings(
|
||||
video,
|
||||
(
|
||||
"title",
|
||||
"moreLabel",
|
||||
"modalTitle",
|
||||
"liveTitle",
|
||||
"liveSubtitle",
|
||||
"modalNote",
|
||||
),
|
||||
"video",
|
||||
errors,
|
||||
)
|
||||
video_items = _array(
|
||||
video.get("items"),
|
||||
"video.items",
|
||||
errors,
|
||||
limit=8,
|
||||
)
|
||||
if not video_items:
|
||||
errors.append("video.items must contain at least one item")
|
||||
_validate_story_items(
|
||||
video_items,
|
||||
"video.items",
|
||||
errors,
|
||||
visuals=VIDEO_VISUALS,
|
||||
include_summary=False,
|
||||
include_duration=True,
|
||||
)
|
||||
|
||||
world = _mapping(data.get("world"), "world", errors)
|
||||
_strings(world, ("title", "moreLabel"), "world", errors)
|
||||
world_items = _array(
|
||||
world.get("items"),
|
||||
"world.items",
|
||||
errors,
|
||||
limit=16,
|
||||
)
|
||||
if not world_items:
|
||||
errors.append("world.items must contain at least one item")
|
||||
_validate_story_items(
|
||||
world_items,
|
||||
"world.items",
|
||||
errors,
|
||||
visuals=WORLD_VISUALS,
|
||||
include_summary=False,
|
||||
)
|
||||
analysis = _mapping(world.get("analysis"), "world.analysis", errors)
|
||||
_strings(
|
||||
analysis,
|
||||
("kicker", "title", "text", "author", "role", "initial"),
|
||||
"world.analysis",
|
||||
errors,
|
||||
)
|
||||
|
||||
markets = _mapping(data.get("markets"), "markets", errors)
|
||||
_strings(markets, ("title", "moreLabel"), "markets", errors)
|
||||
market_items = _array(
|
||||
markets.get("items"),
|
||||
"markets.items",
|
||||
errors,
|
||||
limit=8,
|
||||
)
|
||||
if not market_items:
|
||||
errors.append("markets.items must contain at least one item")
|
||||
for index, value in enumerate(market_items):
|
||||
item = _mapping(value, f"markets.items[{index}]", errors)
|
||||
_strings(
|
||||
item,
|
||||
("name", "value", "change"),
|
||||
f"markets.items[{index}]",
|
||||
errors,
|
||||
)
|
||||
_direction(
|
||||
item.get("direction"),
|
||||
f"markets.items[{index}].direction",
|
||||
errors,
|
||||
)
|
||||
|
||||
newsletter = _mapping(data.get("newsletter"), "newsletter", errors)
|
||||
_strings(
|
||||
newsletter,
|
||||
("title", "text", "placeholder", "button", "successPrefix"),
|
||||
"newsletter",
|
||||
errors,
|
||||
)
|
||||
|
||||
footer = _mapping(data.get("footer"), "footer", errors)
|
||||
_strings(footer, ("tagline", "copyright", "legal"), "footer", errors)
|
||||
columns = _array(
|
||||
footer.get("columns"),
|
||||
"footer.columns",
|
||||
errors,
|
||||
limit=6,
|
||||
)
|
||||
for index, value in enumerate(columns):
|
||||
column_path = f"footer.columns[{index}]"
|
||||
column = _mapping(value, column_path, errors)
|
||||
_non_empty_string(column.get("title"), f"{column_path}.title", errors)
|
||||
links = _array(
|
||||
column.get("links"),
|
||||
f"{column_path}.links",
|
||||
errors,
|
||||
limit=8,
|
||||
)
|
||||
for link_index, link_value in enumerate(links):
|
||||
link_path = f"{column_path}.links[{link_index}]"
|
||||
link = _mapping(link_value, link_path, errors)
|
||||
_non_empty_string(link.get("label"), f"{link_path}.label", errors)
|
||||
_href(link.get("href"), f"{link_path}.href", errors)
|
||||
|
||||
if errors:
|
||||
raise DataValidationError("; ".join(errors))
|
||||
|
||||
return {
|
||||
"modules": list(MODULES),
|
||||
"counts": {
|
||||
"navigation": len(nav_items),
|
||||
"side_stories": len(side_stories),
|
||||
"updates": len(updates),
|
||||
"latest": len(latest_items),
|
||||
"videos": len(video_items),
|
||||
"world": len(world_items),
|
||||
"markets": len(market_items),
|
||||
"footer_columns": len(columns),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
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())
|
||||
|
||||
Reference in new issue
Block a user