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
+11
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "_shared"))
|
||||
|
||||
import render_gma_news_page as renderer
|
||||
from fast_patch_page import run
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(run(renderer, output_stem="gma-news-page"))
|
||||
+494
@@ -0,0 +1,494 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Validate GMA News-style page data and render the bundled template."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
SKILL_DIR = Path(__file__).resolve().parents[1]
|
||||
ASSETS_DIR = SKILL_DIR / "assets"
|
||||
DEFAULT_DATA_PATH = ASSETS_DIR / "default-data.json"
|
||||
TEMPLATE_PATH = ASSETS_DIR / "gma-news-template.html"
|
||||
RUNTIME_PATH = ASSETS_DIR / "gma-news-runtime.js"
|
||||
MODULES = (
|
||||
"site",
|
||||
"navigation",
|
||||
"justIn",
|
||||
"lead",
|
||||
"news",
|
||||
"services",
|
||||
"broadcast",
|
||||
"channels",
|
||||
"media",
|
||||
"footer",
|
||||
)
|
||||
LEAD_VISUALS = {"transit-hub", "storm", "education"}
|
||||
NEWS_VISUALS = {"transit", "civic", "health", "weather", "fare", "regional", "cacao"}
|
||||
CHANNEL_VISUALS = {"showbiz", "lifestyle", "sports", "money"}
|
||||
VIDEO_VISUALS = {"transit-video", "rain-video", "market-video"}
|
||||
FORBIDDEN_TEXT = ("<script", "<iframe", "javascript:", "http://", "https://")
|
||||
REMOTE_MEDIA_TYPES = {"image", "video", "webpage"}
|
||||
MEDIA_URL_KEYS = {"mediaUrl", "posterUrl", "pageUrl"}
|
||||
TECHNICAL_TEXT_KEYS = {"id", "href", "visual", "theme", "language", "brandPrimary", "type", "mediaUrl", "posterUrl", "pageUrl"}
|
||||
|
||||
|
||||
class DataValidationError(ValueError):
|
||||
"""Raised when JSON does not satisfy the fixed page contract."""
|
||||
|
||||
|
||||
def _read_json(path: Path) -> dict[str, Any]:
|
||||
try:
|
||||
value = json.loads(path.read_text(encoding="utf-8"))
|
||||
except FileNotFoundError as exc:
|
||||
raise DataValidationError(f"input 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(value, dict):
|
||||
raise DataValidationError("top-level JSON value must be an object")
|
||||
return value
|
||||
|
||||
|
||||
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],
|
||||
*,
|
||||
minimum: int,
|
||||
maximum: int,
|
||||
) -> list[Any]:
|
||||
if not isinstance(value, list):
|
||||
errors.append(f"{path} must be an array")
|
||||
return []
|
||||
if len(value) < minimum:
|
||||
errors.append(f"{path} must contain at least {minimum} item(s)")
|
||||
if len(value) > maximum:
|
||||
errors.append(f"{path} must contain at most {maximum} item(s)")
|
||||
return value
|
||||
|
||||
|
||||
def _required_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 _strings(item: dict[str, Any], path: str, keys: tuple[str, ...], errors: list[str]) -> None:
|
||||
for key in keys:
|
||||
_required_string(item.get(key), f"{path}.{key}", errors)
|
||||
|
||||
|
||||
def _href(value: Any, path: str, errors: list[str]) -> None:
|
||||
_required_string(value, path, errors)
|
||||
if isinstance(value, str) and not value.startswith("#"):
|
||||
errors.append(f"{path} must be an in-page anchor beginning with #")
|
||||
|
||||
|
||||
def _unique_id(item: dict[str, Any], path: str, seen: set[str], errors: list[str]) -> None:
|
||||
item_id = item.get("id")
|
||||
_required_string(item_id, f"{path}.id", errors)
|
||||
if isinstance(item_id, str):
|
||||
if item_id in seen:
|
||||
errors.append(f"{path}.id duplicates {item_id!r}")
|
||||
seen.add(item_id)
|
||||
|
||||
|
||||
def _links(
|
||||
value: Any,
|
||||
path: str,
|
||||
errors: list[str],
|
||||
*,
|
||||
minimum: int,
|
||||
maximum: int,
|
||||
) -> list[Any]:
|
||||
items = _array(value, path, errors, minimum=minimum, maximum=maximum)
|
||||
for index, raw in enumerate(items):
|
||||
item = _mapping(raw, f"{path}[{index}]", errors)
|
||||
_required_string(item.get("label"), f"{path}[{index}].label", errors)
|
||||
_href(item.get("href"), f"{path}[{index}].href", errors)
|
||||
return items
|
||||
|
||||
|
||||
def _validate_no_remote_or_markup(value: Any, path: str, errors: list[str]) -> None:
|
||||
if isinstance(value, dict):
|
||||
for key, item in value.items():
|
||||
if key in MEDIA_URL_KEYS:
|
||||
continue
|
||||
_validate_no_remote_or_markup(item, f"{path}.{key}", errors)
|
||||
elif isinstance(value, list):
|
||||
for index, item in enumerate(value):
|
||||
_validate_no_remote_or_markup(item, f"{path}[{index}]", errors)
|
||||
elif isinstance(value, str):
|
||||
lowered = value.lower()
|
||||
if any(token in lowered for token in FORBIDDEN_TEXT):
|
||||
errors.append(f"{path} must not contain HTML, script, iframe, or remote URLs")
|
||||
|
||||
|
||||
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, media_type = f"{path}.media", media.get("type")
|
||||
if media_type not in REMOTE_MEDIA_TYPES:
|
||||
errors.append(
|
||||
f"{media_path}.type must be one of: "
|
||||
f"{', '.join(sorted(REMOTE_MEDIA_TYPES))}"
|
||||
)
|
||||
elif media_type in {"image", "video"}:
|
||||
_remote_url(
|
||||
media.get("mediaUrl"),
|
||||
f"{media_path}.mediaUrl",
|
||||
errors,
|
||||
required=True,
|
||||
data_prefix=f"data:{media_type}/",
|
||||
)
|
||||
if media_type == "video":
|
||||
_remote_url(
|
||||
media.get("posterUrl"),
|
||||
f"{media_path}.posterUrl",
|
||||
errors,
|
||||
data_prefix="data:image/",
|
||||
)
|
||||
if media_type == "webpage":
|
||||
_remote_url(
|
||||
media.get("pageUrl"),
|
||||
f"{media_path}.pageUrl",
|
||||
errors,
|
||||
required=True,
|
||||
)
|
||||
_remote_url(
|
||||
media.get("mediaUrl"),
|
||||
f"{media_path}.mediaUrl",
|
||||
errors,
|
||||
data_prefix="data:image/",
|
||||
)
|
||||
if "embed" in media and not isinstance(media.get("embed"), bool):
|
||||
errors.append(f"{media_path}.embed must be a boolean")
|
||||
elif media_type in {"image", "video"}:
|
||||
_remote_url(media.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 _validate_chinese_reader_copy(
|
||||
value: Any,
|
||||
path: str,
|
||||
errors: list[str],
|
||||
*,
|
||||
key: str | None = None,
|
||||
) -> None:
|
||||
if isinstance(value, dict):
|
||||
for child_key, item in value.items():
|
||||
_validate_chinese_reader_copy(
|
||||
item,
|
||||
f"{path}.{child_key}",
|
||||
errors,
|
||||
key=child_key,
|
||||
)
|
||||
elif isinstance(value, list):
|
||||
for index, item in enumerate(value):
|
||||
_validate_chinese_reader_copy(item, f"{path}[{index}]", errors, key=key)
|
||||
elif (
|
||||
isinstance(value, str)
|
||||
and key not in TECHNICAL_TEXT_KEYS
|
||||
and not (key and key.lower().endswith(("id", "href", "url")))
|
||||
):
|
||||
has_latin = any("a" <= character.lower() <= "z" for character in value)
|
||||
has_cjk = any("\u3400" <= character <= "\u9fff" for character in value)
|
||||
if has_latin and not has_cjk:
|
||||
errors.append(f"{path} must use Simplified Chinese reader-facing copy")
|
||||
|
||||
|
||||
def _validate_items(
|
||||
value: Any,
|
||||
path: str,
|
||||
errors: list[str],
|
||||
*,
|
||||
minimum: int,
|
||||
maximum: int,
|
||||
string_keys: tuple[str, ...],
|
||||
href_key: str | None = None,
|
||||
visual_key: str | None = None,
|
||||
visuals: set[str] | None = None,
|
||||
) -> list[Any]:
|
||||
items = _array(value, path, errors, minimum=minimum, maximum=maximum)
|
||||
seen: set[str] = set()
|
||||
for index, raw in enumerate(items):
|
||||
item_path = f"{path}[{index}]"
|
||||
item = _mapping(raw, item_path, errors)
|
||||
_unique_id(item, item_path, seen, errors)
|
||||
_strings(item, item_path, string_keys, errors)
|
||||
if href_key:
|
||||
_href(item.get(href_key), f"{item_path}.{href_key}", errors)
|
||||
if visual_key and visuals is not None and item.get(visual_key) not in visuals:
|
||||
errors.append(f"{item_path}.{visual_key} must be one of {sorted(visuals)}")
|
||||
return items
|
||||
|
||||
|
||||
def validate_document(data: dict[str, Any]) -> dict[str, int]:
|
||||
errors: list[str] = []
|
||||
missing = [name for name in MODULES if name not in data]
|
||||
extra = [name for name in data if name not in MODULES]
|
||||
if missing:
|
||||
errors.append("missing top-level module(s): " + ", ".join(missing))
|
||||
if extra:
|
||||
errors.append("unknown top-level module(s): " + ", ".join(extra))
|
||||
|
||||
site = _mapping(data.get("site"), "site", errors)
|
||||
_strings(site, "site", ("brandPrimary", "brandAccent", "tagline", "dateLabel", "pageTitle", "description", "language", "skipLabel"), errors)
|
||||
if site.get("language") != "zh-CN":
|
||||
errors.append("site.language must be zh-CN")
|
||||
if site.get("theme") not in {"light", "dark", "auto"}:
|
||||
errors.append("site.theme must be light, dark, or auto")
|
||||
weather_mini = _validate_items(site.get("weatherMini"), "site.weatherMini", errors, minimum=1, maximum=6, string_keys=("city", "temp", "condition"))
|
||||
|
||||
navigation = _mapping(data.get("navigation"), "navigation", errors)
|
||||
_strings(navigation, "navigation", ("menuLabel", "searchLabel", "contrastOnLabel", "contrastOffLabel", "liveLabel"), errors)
|
||||
top_links = _links(navigation.get("topLinks"), "navigation.topLinks", errors, minimum=1, maximum=6)
|
||||
primary = _links(navigation.get("primary"), "navigation.primary", errors, minimum=4, maximum=12)
|
||||
drawer = _mapping(navigation.get("drawer"), "navigation.drawer", errors)
|
||||
_strings(drawer, "navigation.drawer", ("eyebrow", "title", "text", "ctaLabel"), errors)
|
||||
_href(drawer.get("ctaHref"), "navigation.drawer.ctaHref", errors)
|
||||
search = _mapping(navigation.get("search"), "navigation.search", errors)
|
||||
_strings(search, "navigation.search", ("title", "placeholder", "hint", "emptyMessage", "resultPrefix"), errors)
|
||||
|
||||
just_in = _mapping(data.get("justIn"), "justIn", errors)
|
||||
_strings(just_in, "justIn", ("label", "moreLabel", "closeLabel", "featured"), errors)
|
||||
just_items = _validate_items(just_in.get("items"), "justIn.items", errors, minimum=1, maximum=6, string_keys=("time", "title", "category"), href_key="href")
|
||||
|
||||
lead = _mapping(data.get("lead"), "lead", errors)
|
||||
main = _mapping(lead.get("main"), "lead.main", errors)
|
||||
_unique_id(main, "lead.main", set(), errors)
|
||||
_strings(main, "lead.main", ("kicker", "title", "summary", "byline", "time", "readTime", "videoLabel", "visual"), errors)
|
||||
if main.get("visual") not in LEAD_VISUALS:
|
||||
errors.append(f"lead.main.visual must be one of {sorted(LEAD_VISUALS)}")
|
||||
side_stories = _validate_items(lead.get("sideStories"), "lead.sideStories", errors, minimum=1, maximum=4, string_keys=("kicker", "title", "summary", "time", "visual"), visual_key="visual", visuals=LEAD_VISUALS)
|
||||
headlines = _validate_items(lead.get("headlines"), "lead.headlines", errors, minimum=2, maximum=6, string_keys=("kicker", "title"), href_key="href")
|
||||
|
||||
news = _mapping(data.get("news"), "news", errors)
|
||||
_strings(news, "news", ("trackingTitle", "trackingStatusPrefix", "trendingTitle", "trendingLabel"), errors)
|
||||
topics = _validate_items(news.get("topics"), "news.topics", errors, minimum=2, maximum=8, string_keys=("label", "count"))
|
||||
feeds = _array(news.get("feeds"), "news.feeds", errors, minimum=2, maximum=4)
|
||||
seen_feeds: set[str] = set()
|
||||
feed_items = 0
|
||||
for feed_index, raw_feed in enumerate(feeds):
|
||||
feed_path = f"news.feeds[{feed_index}]"
|
||||
feed = _mapping(raw_feed, feed_path, errors)
|
||||
_unique_id(feed, feed_path, seen_feeds, errors)
|
||||
_required_string(feed.get("label"), f"{feed_path}.label", errors)
|
||||
items = _validate_items(feed.get("items"), f"{feed_path}.items", errors, minimum=1, maximum=8, string_keys=("kicker", "title", "summary", "time", "visual"), visual_key="visual", visuals=NEWS_VISUALS)
|
||||
feed_items += len(items)
|
||||
trending_items = _validate_items(news.get("trendingItems"), "news.trendingItems", errors, minimum=3, maximum=8, string_keys=("title",), href_key="href")
|
||||
|
||||
services = _mapping(data.get("services"), "services", errors)
|
||||
_strings(services, "services", ("weatherTitle", "weatherLiveLabel", "publicTitle"), errors)
|
||||
locations = _array(services.get("locations"), "services.locations", errors, minimum=2, maximum=6)
|
||||
seen_locations: set[str] = set()
|
||||
forecast_count = 0
|
||||
for location_index, raw_location in enumerate(locations):
|
||||
location_path = f"services.locations[{location_index}]"
|
||||
location = _mapping(raw_location, location_path, errors)
|
||||
_unique_id(location, location_path, seen_locations, errors)
|
||||
_strings(location, location_path, ("city", "temp", "condition"), errors)
|
||||
forecast = _array(location.get("forecast"), f"{location_path}.forecast", errors, minimum=2, maximum=5)
|
||||
forecast_count += len(forecast)
|
||||
for item_index, raw_item in enumerate(forecast):
|
||||
item = _mapping(raw_item, f"{location_path}.forecast[{item_index}]", errors)
|
||||
_strings(item, f"{location_path}.forecast[{item_index}]", ("day", "temp", "label"), errors)
|
||||
public_items = _validate_items(services.get("publicItems"), "services.publicItems", errors, minimum=2, maximum=8, string_keys=("label", "message"))
|
||||
|
||||
broadcast = _mapping(data.get("broadcast"), "broadcast", errors)
|
||||
_strings(broadcast, "broadcast", ("eyebrow", "title", "moreLabel", "scheduleTitle"), errors)
|
||||
_href(broadcast.get("moreHref"), "broadcast.moreHref", errors)
|
||||
broadcast_hero = _mapping(broadcast.get("hero"), "broadcast.hero", errors)
|
||||
_strings(broadcast_hero, "broadcast.hero", ("title", "summary", "visual", "actionLabel"), errors)
|
||||
if broadcast_hero.get("visual") != "newsroom":
|
||||
errors.append("broadcast.hero.visual must be newsroom")
|
||||
slots = _validate_items(broadcast.get("slots"), "broadcast.slots", errors, minimum=2, maximum=8, string_keys=("time", "title", "summary"))
|
||||
for index, slot in enumerate(slots):
|
||||
if isinstance(slot, dict) and not isinstance(slot.get("live"), bool):
|
||||
errors.append(f"broadcast.slots[{index}].live must be a boolean")
|
||||
modal = _mapping(broadcast.get("modal"), "broadcast.modal", errors)
|
||||
_strings(modal, "broadcast.modal", ("brand", "title", "note", "quality", "captionsOn", "captionsOff"), errors)
|
||||
|
||||
channels = _mapping(data.get("channels"), "channels", errors)
|
||||
_strings(channels, "channels", ("glanceTitle", "glanceMoreLabel", "deskTitle", "deskMoreLabel"), errors)
|
||||
_href(channels.get("glanceMoreHref"), "channels.glanceMoreHref", errors)
|
||||
_href(channels.get("deskMoreHref"), "channels.deskMoreHref", errors)
|
||||
glance_items = _validate_items(channels.get("glanceItems"), "channels.glanceItems", errors, minimum=2, maximum=6, string_keys=("kicker", "value", "text", "actionLabel"), href_key="actionHref")
|
||||
desk_items = _validate_items(channels.get("deskItems"), "channels.deskItems", errors, minimum=2, maximum=6, string_keys=("kicker", "title", "summary", "visual"), visual_key="visual", visuals=CHANNEL_VISUALS)
|
||||
|
||||
media = _mapping(data.get("media"), "media", errors)
|
||||
_strings(media, "media", ("videosTitle", "videosMoreLabel"), errors)
|
||||
_href(media.get("videosMoreHref"), "media.videosMoreHref", errors)
|
||||
videos = _validate_items(media.get("videos"), "media.videos", errors, minimum=1, maximum=6, string_keys=("kicker", "duration", "title", "summary", "visual"), visual_key="visual", visuals=VIDEO_VISUALS)
|
||||
podcast = _mapping(media.get("podcast"), "media.podcast", errors)
|
||||
_strings(podcast, "media.podcast", ("eyebrow", "title", "text", "playLabel", "pauseLabel", "startTime", "playingTime"), errors)
|
||||
lotto = _mapping(media.get("lotto"), "media.lotto", errors)
|
||||
_strings(lotto, "media.lotto", ("eyebrow", "title", "date"), errors)
|
||||
games = _array(lotto.get("games"), "media.lotto.games", errors, minimum=2, maximum=4)
|
||||
seen_games: set[str] = set()
|
||||
lotto_numbers = 0
|
||||
for game_index, raw_game in enumerate(games):
|
||||
game_path = f"media.lotto.games[{game_index}]"
|
||||
game = _mapping(raw_game, game_path, errors)
|
||||
_unique_id(game, game_path, seen_games, errors)
|
||||
_required_string(game.get("label"), f"{game_path}.label", errors)
|
||||
numbers = _array(game.get("numbers"), f"{game_path}.numbers", errors, minimum=5, maximum=8)
|
||||
lotto_numbers += len(numbers)
|
||||
for number_index, number in enumerate(numbers):
|
||||
_required_string(number, f"{game_path}.numbers[{number_index}]", errors)
|
||||
newsletter = _mapping(media.get("newsletter"), "media.newsletter", errors)
|
||||
_strings(newsletter, "media.newsletter", ("title", "text", "label", "placeholder", "button", "successPrefix"), errors)
|
||||
|
||||
footer = _mapping(data.get("footer"), "footer", errors)
|
||||
_strings(footer, "footer", ("description", "copyright", "legal"), errors)
|
||||
footer_columns = _array(footer.get("columns"), "footer.columns", errors, minimum=1, maximum=5)
|
||||
seen_footer: set[str] = set()
|
||||
footer_links = 0
|
||||
for index, raw in enumerate(footer_columns):
|
||||
path = f"footer.columns[{index}]"
|
||||
item = _mapping(raw, path, errors)
|
||||
_unique_id(item, path, seen_footer, errors)
|
||||
_required_string(item.get("title"), f"{path}.title", errors)
|
||||
footer_links += len(_links(item.get("links"), f"{path}.links", errors, minimum=1, maximum=8))
|
||||
|
||||
_validate_remote_media_tree(data, "$", errors)
|
||||
_validate_no_remote_or_markup(data, "$", errors)
|
||||
_validate_chinese_reader_copy(data, "$", errors)
|
||||
if errors:
|
||||
raise DataValidationError("\n".join(f"- {error}" for error in errors))
|
||||
return {
|
||||
"weather_mini": len(weather_mini),
|
||||
"navigation_links": len(top_links) + len(primary),
|
||||
"just_in_items": len(just_items),
|
||||
"lead_stories": 1 + len(side_stories),
|
||||
"headlines": len(headlines),
|
||||
"tracking_topics": len(topics),
|
||||
"feeds": len(feeds),
|
||||
"feed_items": feed_items,
|
||||
"trending_items": len(trending_items),
|
||||
"weather_locations": len(locations),
|
||||
"forecast_items": forecast_count,
|
||||
"public_services": len(public_items),
|
||||
"schedule_slots": len(slots),
|
||||
"glance_items": len(glance_items),
|
||||
"desk_items": len(desk_items),
|
||||
"videos": len(videos),
|
||||
"lotto_games": len(games),
|
||||
"lotto_numbers": lotto_numbers,
|
||||
"footer_columns": len(footer_columns),
|
||||
"footer_links": footer_links,
|
||||
}
|
||||
|
||||
|
||||
def render_document(data: dict[str, Any], template: str, runtime: str) -> str:
|
||||
validate_document(data)
|
||||
data_text = json.dumps(data, ensure_ascii=False, separators=(",", ":"))
|
||||
data_text = data_text.replace("</script", "<\\/script")
|
||||
if template.count("__GMA_NEWS_PAGE_DATA__") != 1:
|
||||
raise DataValidationError("template must contain one __GMA_NEWS_PAGE_DATA__ marker")
|
||||
if template.count("__GMA_NEWS_PAGE_RUNTIME__") != 1:
|
||||
raise DataValidationError("template must contain one __GMA_NEWS_PAGE_RUNTIME__ marker")
|
||||
rendered = template.replace("__GMA_NEWS_PAGE_DATA__", data_text).replace("__GMA_NEWS_PAGE_RUNTIME__", runtime)
|
||||
if "__GMA_NEWS_PAGE_" in rendered:
|
||||
raise DataValidationError("unresolved template marker remains in rendered HTML")
|
||||
return rendered
|
||||
|
||||
|
||||
def _write_new(path: Path, content: str) -> None:
|
||||
if path.exists():
|
||||
raise DataValidationError(f"refusing to overwrite existing file: {path}; choose a new path")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(content, encoding="utf-8", newline="\n")
|
||||
|
||||
|
||||
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)
|
||||
counts = validate_document(data)
|
||||
_write_new(output, json.dumps(data, ensure_ascii=False, indent=2) + "\n")
|
||||
return {"status":"ok","action":"init","output":str(output),"modules":list(MODULES),"counts":counts}
|
||||
|
||||
|
||||
def _command_validate(input_path: Path) -> dict[str, Any]:
|
||||
counts = validate_document(_read_json(input_path))
|
||||
return {"status":"ok","action":"validate","input":str(input_path),"modules":list(MODULES),"counts":counts}
|
||||
|
||||
|
||||
def _command_render(input_path: Path, output: Path) -> dict[str, Any]:
|
||||
data = _read_json(input_path)
|
||||
counts = validate_document(data)
|
||||
template = TEMPLATE_PATH.read_text(encoding="utf-8")
|
||||
runtime = RUNTIME_PATH.read_text(encoding="utf-8")
|
||||
_write_new(output, render_document(data, template, runtime))
|
||||
return {"status":"ok","action":"render","input":str(input_path),"output":str(output),"modules":list(MODULES),"counts":counts}
|
||||
|
||||
|
||||
def _build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="Initialize, validate, and render Chinese GMA News-style portal JSON.")
|
||||
subparsers = parser.add_subparsers(dest="command", required=True)
|
||||
init_parser = subparsers.add_parser("init", help="write a fresh default JSON file")
|
||||
init_parser.add_argument("--output", required=True, type=Path)
|
||||
validate_parser = subparsers.add_parser("validate", help="validate a JSON file")
|
||||
validate_parser.add_argument("--input", required=True, type=Path)
|
||||
render_parser = subparsers.add_parser("render", help="render validated JSON into the fixed HTML template")
|
||||
render_parser.add_argument("--input", required=True, type=Path)
|
||||
render_parser.add_argument("--output", required=True, type=Path)
|
||||
return parser
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = _build_parser()
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
if args.command == "init":
|
||||
payload = _command_init(args.output)
|
||||
elif args.command == "validate":
|
||||
payload = _command_validate(args.input)
|
||||
else:
|
||||
payload = _command_render(args.input, args.output)
|
||||
_emit(payload)
|
||||
return 0
|
||||
except (DataValidationError, OSError) as exc:
|
||||
_emit({"status":"error","action":args.command,"message":str(exc)})
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user