| 1 | """scaffold: turn a slide library into an editable fill-plan skeleton.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from typing import Any |
| 6 | |
| 7 | |
| 8 | def scaffold_plan( |
| 9 | library: dict[str, Any], |
| 10 | selected_slides: list[int] | None = None, |
| 11 | *, |
| 12 | include_empty: bool = False, |
| 13 | ) -> dict[str, Any]: |
| 14 | """Build an editable fill plan skeleton from a slide library.""" |
| 15 | selected = set(selected_slides or []) |
| 16 | source_slides = library.get("slides", []) |
| 17 | if not selected: |
| 18 | source_slides = source_slides[: min(6, len(source_slides))] |
| 19 | else: |
| 20 | source_slides = [slide for slide in source_slides if int(slide["slide_index"]) in selected] |
| 21 | |
| 22 | slides: list[dict[str, Any]] = [] |
| 23 | for slide in source_slides: |
| 24 | replacements = [] |
| 25 | for slot in slide.get("slots", []): |
| 26 | if not include_empty and not str(slot.get("text") or "").strip(): |
| 27 | continue |
| 28 | replacements.append( |
| 29 | { |
| 30 | "slot_id": slot["slot_id"], |
| 31 | "old_text": slot["text"], |
| 32 | "text": slot["text"], |
| 33 | } |
| 34 | ) |
| 35 | table_edits = [] |
| 36 | for table in slide.get("tables", []): |
| 37 | table_edits.append( |
| 38 | { |
| 39 | "table_id": table["table_id"], |
| 40 | "cells": [ |
| 41 | { |
| 42 | "row": cell["row"], |
| 43 | "col": cell["col"], |
| 44 | "old_text": cell["text"], |
| 45 | "text": cell["text"], |
| 46 | } |
| 47 | for row in table.get("rows", []) |
| 48 | for cell in row.get("cells", []) |
| 49 | ], |
| 50 | } |
| 51 | ) |
| 52 | chart_edits = [] |
| 53 | for chart in slide.get("charts", []): |
| 54 | chart_edits.append( |
| 55 | { |
| 56 | "chart_id": chart["chart_id"], |
| 57 | "categories": chart.get("categories", []), |
| 58 | "series": chart.get("series") or [{"name": "系列1", "values": []}], |
| 59 | } |
| 60 | ) |
| 61 | slides.append( |
| 62 | { |
| 63 | "source_slide": slide["slide_index"], |
| 64 | "purpose": slide.get("page_type", "content_candidate"), |
| 65 | "layout_rationale": { |
| 66 | "layout_pattern": "", |
| 67 | "why_fit": "", |
| 68 | "risk": "", |
| 69 | }, |
| 70 | "replacements": replacements, |
| 71 | "table_edits": table_edits, |
| 72 | "chart_edits": chart_edits, |
| 73 | } |
| 74 | ) |
| 75 | |
| 76 | return { |
| 77 | "schema": "template_fill_pptx_plan.v1", |
| 78 | "status": "draft", |
| 79 | "source_pptx": library.get("source_pptx"), |
| 80 | "accepted_warnings": [], |
| 81 | "slides": slides, |
| 82 | } |
| 83 |