generate_all_blocks_demo.py 28.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
#!/usr/bin/env python3
"""
生成覆盖全部允许block类型的演示 IR,用于验证 HTML 与 PDF 渲染。

执行后会在 `final_reports/ir` 写入一份带时间戳的 IR,
并分别在 `final_reports/html` 与 `final_reports/pdf` 输出对应的渲染文件。
"""

from __future__ import annotations

import json
import sys
from datetime import datetime
from pathlib import Path

# 允许直接以脚本形式运行
ROOT = Path(__file__).resolve().parents[2]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from ReportEngine.core import DocumentComposer
from ReportEngine.ir import IRValidator
from ReportEngine.ir.schema import ENGINE_AGENT_TITLES
from ReportEngine.renderers import HTMLRenderer, PDFRenderer
from ReportEngine.utils.config import settings


def build_inline_marks_demo() -> dict:
    """生成覆盖全部内联标记的 paragraph block。"""
    return {
        "type": "paragraph",
        "inlines": [
            {"text": "这一段覆盖全部内联标记:"},
            {"text": "粗体", "marks": [{"type": "bold"}]},
            {"text": " / 斜体", "marks": [{"type": "italic"}]},
            {"text": " / 下划线", "marks": [{"type": "underline"}]},
            {"text": " / 删除线", "marks": [{"type": "strike"}]},
            {"text": " / 代码", "marks": [{"type": "code"}]},
            {
                "text": " / 链接",
                "marks": [
                    {
                        "type": "link",
                        "href": "https://example.com/demo",
                        "title": "示例链接",
                    }
                ],
            },
            {"text": " / 颜色", "marks": [{"type": "color", "value": "#c0392b"}]},
            {
                "text": " / 字体",
                "marks": [
                    {
                        "type": "font",
                        "family": "Georgia, serif",
                        "size": "15px",
                        "weight": "600",
                    }
                ],
            },
            {"text": " / 高亮", "marks": [{"type": "highlight"}]},
            {"text": " / 下标", "marks": [{"type": "subscript"}]},
            {"text": " / 上标", "marks": [{"type": "superscript"}]},
            {"text": " / 行内公式", "marks": [{"type": "math", "value": "E=mc^2"}]},
            {"text": "。"},
        ],
    }


def build_widget_block() -> dict:
    """构造一个合法的 Chart.js widget block。"""
    return {
        "type": "widget",
        "widgetId": "demo-volume-trend",
        "widgetType": "chart.js/line",
        "props": {
            "type": "line",
            "options": {
                "responsive": True,
                "plugins": {"legend": {"position": "bottom"}},
                "scales": {"y": {"title": {"display": True, "text": "提及量"}}},
            },
        },
        "data": {
            "labels": ["T0", "T0+6h", "T0+12h", "T0+18h", "T0+24h"],
            "datasets": [
                {
                    "label": "主流媒体",
                    "data": [12, 18, 23, 30, 26],
                    "borderColor": "#2980b9",
                    "backgroundColor": "rgba(41,128,185,0.18)",
                    "tension": 0.25,
                    "fill": False,
                },
                {
                    "label": "社交平台",
                    "data": [8, 10, 15, 28, 40],
                    "borderColor": "#c0392b",
                    "backgroundColor": "rgba(192,57,43,0.2)",
                    "tension": 0.35,
                    "fill": False,
                },
            ],
        },
    }


def build_chapters() -> list[dict]:
    """构造覆盖所有 block 类型的章节列表。"""
    inline_demo = build_inline_marks_demo()

    bullet_list = {
        "type": "list",
        "listType": "bullet",
        "items": [
            [
                {
                    "type": "paragraph",
                    "inlines": [{"text": "社交媒体热度在 48 小时内翻倍"}],
                }
            ],
            [
                {
                    "type": "paragraph",
                    "inlines": [{"text": "主流媒体报道集中在早间时段"}],
                },
                {
                    "type": "list",
                    "listType": "ordered",
                    "items": [
                        [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "07:00-09:00:首轮报道"}],
                            }
                        ],
                        [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "10:00-12:00:评论扩散"}],
                            }
                        ],
                    ],
                },
            ],
            [
                {
                    "type": "paragraph",
                    "inlines": [{"text": "地方政务号开始回应并同步线下通稿"}],
                }
            ],
        ],
    }

    task_list = {
        "type": "list",
        "listType": "task",
        "items": [
            [
                {
                    "type": "paragraph",
                    "inlines": [{"text": "跟踪权威辟谣素材是否上线"}],
                }
            ],
            [
                {
                    "type": "paragraph",
                    "inlines": [{"text": "监测新增关联关键词与长尾问题"}],
                }
            ],
            [
                {
                    "type": "paragraph",
                    "inlines": [{"text": "准备 FAQ 供客服统一答复"}],
                }
            ],
        ],
    }

    table_block = {
        "type": "table",
        "caption": "核心信源与传播路径",
        "zebra": True,
        "colgroup": [{"width": "22%"}, {"width": "38%"}, {"width": "40%"}],
        "rows": [
            {
                "cells": [
                    {
                        "align": "center",
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "时间节点", "marks": [{"type": "bold"}]}],
                            }
                        ],
                    },
                    {
                        "align": "center",
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "事件内容", "marks": [{"type": "bold"}]}],
                            }
                        ],
                    },
                    {
                        "align": "center",
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "主要渠道", "marks": [{"type": "bold"}]}],
                            }
                        ],
                    },
                ]
            },
            {
                "cells": [
                    {"blocks": [{"type": "paragraph", "inlines": [{"text": "T0"}]}]},
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "线下冲突视频首次上传"}],
                            }
                        ]
                    },
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "短视频平台 / 私聊转发"}],
                            }
                        ]
                    },
                ]
            },
            {
                "cells": [
                    {"blocks": [{"type": "paragraph", "inlines": [{"text": "T0+6h"}]}]},
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "登上热搜,出现二次剪辑"}],
                            }
                        ]
                    },
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "微博 / 朋友圈"}],
                            }
                        ]
                    },
                ]
            },
            {
                "cells": [
                    {"blocks": [{"type": "paragraph", "inlines": [{"text": "T0+18h"}]}]},
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "官方回应并发布事实澄清"}],
                            }
                        ]
                    },
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "政务号 / 新闻客户端"}],
                            }
                        ]
                    },
                ]
            },
            {
                "cells": [
                    {"blocks": [{"type": "paragraph", "inlines": [{"text": "T0+24h"}]}]},
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "专家解读,舆论重心转向责任归属"}],
                            }
                        ]
                    },
                    {
                        "blocks": [
                            {
                                "type": "paragraph",
                                "inlines": [{"text": "视频号直播 / 行业社群"}],
                            }
                        ]
                    },
                ]
            },
        ],
    }

    blockquote_block = {
        "type": "blockquote",
        "variant": "accent",
        "blocks": [
            {
                "type": "paragraph",
                "inlines": [{"text": "“公众最关心的信息是真相与责任边界。”"}],
            },
            {
                "type": "paragraph",
                "inlines": [{"text": "—— 模拟引用,验证引用块样式"}],
            },
        ],
    }

    engine_quote_block = {
        "type": "engineQuote",
        "engine": "insight",
        "title": ENGINE_AGENT_TITLES["insight"],
        "blocks": [
            {
                "type": "paragraph",
                "inlines": [
                    {
                        "text": "模型认为 24 小时内保持回应频次,可避免信息真空。",
                        "marks": [{"type": "bold"}],
                    }
                ],
            },
            {
                "type": "paragraph",
                "inlines": [
                    {"text": "建议同时准备简短 FAQ,便于多渠道统一口径。"}
                ],
            },
        ],
    }

    swot_block = {
        "type": "swotTable",
        "title": "舆论场 SWOT 速览",
        "summary": "覆盖当前情绪分布、潜在风险与机会。",
        "strengths": [
            {"title": "官方快速响应", "detail": "首条澄清视频 3 小时内上线"},
            {"title": "同城媒体配合", "impact": "高", "score": 8},
        ],
        "weaknesses": [
            {"title": "早期谣言存量大", "detail": "相关转发仍占 30%"},
            "外部专家尚未统一口径",
        ],
        "opportunities": [
            {
                "title": "社区共建讨论",
                "detail": "自发组织“辟谣志愿者”话题,情绪正向",
            },
            {"title": "公益合作窗口", "impact": "中"},
        ],
        "threats": [
            {"title": "跨平台剪辑继续发酵", "impact": "高", "score": 9},
            {"title": "个别自媒体煽动情绪", "evidence": "存在地域标签化倾向"},
        ],
    }

    pest_block = {
        "type": "pestTable",
        "title": "宏观环境脉冲扫描(PEST)",
        "summary": "模拟四大维度的外部约束与机会,验证 pestTable 的渲染样式。",
        "political": [
            {
                "title": "地方条例征求意见",
                "detail": "短视频发布需实名溯源,平台合规沟通窗口期开启",
                "trend": "正面利好",
                "impact": 7,
            },
            {
                "title": "监管关注情绪煽动",
                "detail": "对夸大矛盾的账号重点巡查,舆论阈值下调",
                "trend": "持续观察",
                "impact": 6,
            },
        ],
        "economic": [
            {
                "title": "周边商户营收波动",
                "detail": "客流短期下滑 12%,但直播带货订单上升",
                "trend": "中性",
                "impact": 5,
            },
            {
                "title": "品牌赞助谨慎",
                "detail": "赞助延期观察声誉风险,对官宣节奏有压力",
                "trend": "不确定",
                "impact": 4,
            },
        ],
        "social": [
            {
                "title": "核心群体情绪分化",
                "detail": "本地居民关注安全,外地游客关注体验与退款",
                "trend": "负面影响",
                "impact": 8,
            },
            {
                "title": "高校社群自发求证",
                "detail": "校媒与学生会组织“以图搜图”科普贴,情绪趋稳",
                "trend": "正面利好",
                "impact": 6,
            },
        ],
        "technological": [
            {
                "title": "AI 生成内容被混入",
                "detail": "局部画面被放大后再传播,需水印溯源工具辅助鉴伪",
                "trend": "负面影响",
                "impact": 7,
            },
            {
                "title": "多模态检索上线",
                "detail": "平台试行“视频反诈”模型,自动提示剪辑痕迹",
                "trend": "正面利好",
                "impact": 5,
            },
        ],
    }

    callout_block = {
        "type": "callout",
        "tone": "warning",
        "title": "排版边界提示",
        "blocks": [
            {
                "type": "paragraph",
                "inlines": [
                    {"text": "callout 内部仅放轻量内容,超出部分会自动溢出到外层。"}
                ],
            },
            {
                "type": "list",
                "listType": "bullet",
                "items": [
                    [
                        {
                            "type": "paragraph",
                            "inlines": [{"text": "支持嵌套列表 / 表格 / 数学公式"}],
                        }
                    ],
                    [
                        {
                            "type": "paragraph",
                            "inlines": [{"text": "可在这里放置提醒或操作步骤"}],
                        }
                    ],
                ],
            },
        ],
    }

    code_block = {
        "type": "code",
        "lang": "json",
        "caption": "演示代码块",
        "content": '{\n  "event": "热点示例",\n  "topic": "公共事件",\n  "status": "monitoring"\n}',
    }

    math_block = {
        "type": "math",
        "latex": r"E = mc^2",
        "displayMode": True,
    }

    figure_block = {
        "type": "figure",
        "img": {
            "src": "https://dummyimage.com/600x320/eeeeee/333333&text=Placeholder",
            "alt": "占位示意图",
            "width": 600,
            "height": 320,
        },
        "caption": "图像外链被替换为友好提示,可验证 figure 占位效果。",
        "responsive": True,
    }

    widget_block = build_widget_block()
    stacked_bar_chart_block = {
        "type": "widget",
        "widgetId": "demo-stacked-sentiment",
        "widgetType": "chart.js/bar",
        "props": {
            "type": "bar",
            "options": {
                "responsive": True,
                "plugins": {"legend": {"position": "bottom"}},
                "scales": {
                    "x": {"stacked": True},
                    "y": {"stacked": True, "title": {"display": True, "text": "信息量"}},
                },
            },
        },
        "data": {
            "labels": ["周一", "周二", "周三", "周四", "周五"],
            "datasets": [
                {"label": "正向", "data": [18, 22, 24, 19, 16], "backgroundColor": "#27ae60"},
                {"label": "中性", "data": [22, 20, 18, 21, 23], "backgroundColor": "#f39c12"},
                {"label": "负向", "data": [12, 14, 10, 9, 11], "backgroundColor": "#c0392b"},
            ],
        },
    }
    doughnut_chart_block = {
        "type": "widget",
        "widgetId": "demo-sentiment-share",
        "widgetType": "chart.js/doughnut",
        "props": {
            "type": "doughnut",
            "options": {"plugins": {"legend": {"position": "right"}, "tooltip": {"enabled": True}}},
        },
        "data": {
            "labels": ["政策", "经济", "社会", "技术"],
            "datasets": [
                {
                    "label": "关注度占比",
                    "data": [24, 30, 28, 18],
                    "backgroundColor": ["#8e44ad", "#16a085", "#e67e22", "#2980b9"],
                    "hoverOffset": 6,
                }
            ],
        },
    }
    radar_chart_block = {
        "type": "widget",
        "widgetId": "demo-response-radar",
        "widgetType": "chart.js/radar",
        "props": {
            "type": "radar",
            "options": {
                "plugins": {"legend": {"position": "top"}},
                "scales": {"r": {"beginAtZero": True, "max": 100}},
            },
        },
        "data": {
            "labels": ["透明度", "响应速度", "一致性", "互动度", "信息量"],
            "datasets": [
                {
                    "label": "官方渠道",
                    "data": [78, 88, 82, 66, 91],
                    "backgroundColor": "rgba(46,204,113,0.15)",
                    "borderColor": "#2ecc71",
                    "pointBackgroundColor": "#27ae60",
                },
                {
                    "label": "民间讨论",
                    "data": [64, 72, 58, 74, 63],
                    "backgroundColor": "rgba(52,152,219,0.12)",
                    "borderColor": "#3498db",
                    "pointBackgroundColor": "#2980b9",
                },
            ],
        },
    }
    polar_area_chart_block = {
        "type": "widget",
        "widgetId": "demo-channel-polar",
        "widgetType": "chart.js/polarArea",
        "props": {"type": "polarArea"},
        "data": {
            "labels": ["短视频", "微博", "社区论坛", "新闻客户端", "线下反馈"],
            "datasets": [
                {
                    "label": "渠道渗透度",
                    "data": [62, 54, 38, 45, 28],
                    "backgroundColor": [
                        "rgba(231,76,60,0.65)",
                        "rgba(142,68,173,0.6)",
                        "rgba(52,152,219,0.55)",
                        "rgba(46,204,113,0.55)",
                        "rgba(241,196,15,0.6)",
                    ],
                }
            ],
        },
    }
    scatter_chart_block = {
        "type": "widget",
        "widgetId": "demo-correlation-scatter",
        "widgetType": "chart.js/scatter",
        "props": {
            "type": "scatter",
            "options": {
                "plugins": {"legend": {"position": "bottom"}},
                "scales": {
                    "x": {"title": {"display": True, "text": "情绪极性"}, "min": -1, "max": 1},
                    "y": {"title": {"display": True, "text": "互动量"}, "beginAtZero": True},
                },
            },
        },
        "data": {
            "datasets": [
                {
                    "label": "帖子散点",
                    "data": [
                        {"x": -0.65, "y": 120},
                        {"x": -0.25, "y": 190},
                        {"x": 0.05, "y": 260},
                        {"x": 0.42, "y": 340},
                        {"x": 0.78, "y": 410},
                    ],
                    "backgroundColor": "rgba(52,152,219,0.7)",
                }
            ],
        },
    }
    bubble_chart_block = {
        "type": "widget",
        "widgetId": "demo-impact-bubble",
        "widgetType": "chart.js/bubble",
        "props": {
            "type": "bubble",
            "options": {
                "plugins": {"legend": {"position": "bottom"}},
                "scales": {
                    "x": {"title": {"display": True, "text": "曝光量 (万)"}, "beginAtZero": True},
                    "y": {"title": {"display": True, "text": "情绪强度"}, "min": -100, "max": 100},
                },
            },
        },
        "data": {
            "datasets": [
                {
                    "label": "渠道分布",
                    "data": [
                        {"x": 8, "y": 35, "r": 12},
                        {"x": 12, "y": -28, "r": 10},
                        {"x": 18, "y": 22, "r": 14},
                        {"x": 25, "y": 48, "r": 16},
                        {"x": 6, "y": -12, "r": 8},
                    ],
                    "backgroundColor": "rgba(192,57,43,0.55)",
                    "borderColor": "#c0392b",
                }
            ],
        },
    }

    chapter_1 = {
        "chapterId": "S1",
        "title": "封面与目录",
        "anchor": "overview",
        "order": 10,
        "blocks": [
            {"type": "heading", "level": 2, "text": "一、封面与目录", "anchor": "overview"},
            {
                "type": "paragraph",
                "inlines": [
                    {
                        "text": "模拟社会公共热点事件的摘要,便于快速确认排版与字体效果。",
                    }
                ],
            },
            inline_demo,
            {
                "type": "kpiGrid",
                "items": [
                    {"label": "24h提及量", "value": "98K", "delta": "+41%", "deltaTone": "up"},
                    {"label": "正向占比", "value": "32%", "delta": "+5pp", "deltaTone": "up"},
                    {"label": "负向占比", "value": "18%", "delta": "-3pp", "deltaTone": "down"},
                    {"label": "高频渠道", "value": "短视频 / 微博"},
                ],
                "cols": 4,
            },
            {"type": "toc"},
            {"type": "hr"},
        ],
    }

    chapter_2 = {
        "chapterId": "S2",
        "title": "块类型演示",
        "anchor": "blocks-showcase",
        "order": 20,
        "blocks": [
            {
                "type": "heading",
                "level": 2,
                "text": "二、块类型演示",
                "anchor": "blocks-showcase",
            },
            {
                "type": "paragraph",
                "inlines": [
                    {
                        "text": "以下内容逐一覆盖 paragraph/list/table/swot/pest/widget 等全部块类型。",
                    }
                ],
            },
            {
                "type": "heading",
                "level": 3,
                "text": "2.1 列表与表格",
                "anchor": "lists-and-tables",
            },
            bullet_list,
            task_list,
            table_block,
            {
                "type": "heading",
                "level": 3,
                "text": "2.2 图表组件演示",
                "anchor": "charts-demo",
            },
            {
                "type": "paragraph",
                "inlines": [
                    {
                        "text": "折线/柱状/饼图/雷达/极区/散点/气泡等多类型图表,用于验证 Chart.js 兼容性。",
                    }
                ],
            },
            widget_block,
            stacked_bar_chart_block,
            doughnut_chart_block,
            radar_chart_block,
            polar_area_chart_block,
            scatter_chart_block,
            bubble_chart_block,
            {
                "type": "heading",
                "level": 3,
                "text": "2.3 高阶块与富媒体",
                "anchor": "advanced-blocks",
            },
            blockquote_block,
            callout_block,
            engine_quote_block,
            swot_block,
            pest_block,
            code_block,
            math_block,
            figure_block,
            {
                "type": "hr",
                "variant": "dashed",
            },
            {
                "type": "paragraph",
                "align": "justify",
                "inlines": [
                    {
                        "text": "本章节的 inline math 兜底验证:",
                    },
                    {"text": "p(t)=p_0 e^{\\lambda t}", "marks": [{"type": "math"}]},
                    {"text": ";以上覆盖所有允许块及标记。"},
                ],
            },
        ],
    }

    return [chapter_1, chapter_2]


def validate_chapters(chapters: list[dict]) -> None:
    """使用 IRValidator 校验章节结构,发现错误时抛出异常。"""
    validator = IRValidator()
    for chapter in chapters:
        ok, errors = validator.validate_chapter(chapter)
        if not ok:
            raise ValueError(f"{chapter.get('chapterId', 'unknown')} 校验失败: {errors}")


def render_and_save(document_ir: dict, timestamp: str) -> tuple[Path, Path, Path]:
    """将 IR 保存为 JSON,并渲染 HTML / PDF,返回三个路径。"""
    ir_dir = Path(settings.DOCUMENT_IR_OUTPUT_DIR)
    html_dir = Path(settings.OUTPUT_DIR) / "html"
    pdf_dir = Path(settings.OUTPUT_DIR) / "pdf"
    ir_dir.mkdir(parents=True, exist_ok=True)
    html_dir.mkdir(parents=True, exist_ok=True)
    pdf_dir.mkdir(parents=True, exist_ok=True)

    ir_path = ir_dir / f"report_ir_all_blocks_demo_{timestamp}.json"
    ir_path.write_text(json.dumps(document_ir, ensure_ascii=False, indent=2), encoding="utf-8")

    html_renderer = HTMLRenderer()
    html_content = html_renderer.render(document_ir)
    html_path = html_dir / f"report_html_all_blocks_demo_{timestamp}.html"
    html_path.write_text(html_content, encoding="utf-8")

    pdf_renderer = PDFRenderer()
    pdf_path = pdf_dir / f"report_pdf_all_blocks_demo_{timestamp}.pdf"
    pdf_renderer.render_to_pdf(document_ir, pdf_path)

    return ir_path, html_path, pdf_path


def main() -> int:
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    report_id = f"all-blocks-demo-{timestamp}"
    metadata = {
        "title": "社会公共热点事件渲染测试",
        "subtitle": "覆盖全部 IR 块类型的示例数据,含多种图表与 PEST 演示",
        "query": "公共事件渲染能力自检 / Chart & PEST",
        "toc": {"title": "目录", "depth": 3},
        "hero": {
            "summary": "用于验证 Report Engine 在 HTML / PDF 渲染时对各类区块、Chart.js 组件与 PEST 模块的兼容性。",
            "kpis": [
                {"label": "示例块数量", "value": "20+", "delta": "含 PEST", "tone": "up"},
                {"label": "图表数", "value": "7", "delta": "新增多类型", "tone": "neutral"},
            ],
            "highlights": ["覆盖全部 block", "含行内/块级公式", "Chart.js 多类型", "PEST + SWOT"],
            "actions": ["重新生成", "导出 PDF"],
        },
    }

    chapters = build_chapters()
    validate_chapters(chapters)

    composer = DocumentComposer()
    document_ir = composer.build_document(report_id, metadata, chapters)

    ir_path, html_path, pdf_path = render_and_save(document_ir, timestamp)

    print("✅ 演示 IR 生成完成")
    print(f"IR:   {ir_path}")
    print(f"HTML: {html_path}")
    print(f"PDF:  {pdf_path}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())