(
note_text: str,
target_path: Path,
decisions: dict,
asset_subdir: str,
)
| 84 | |
| 85 | |
| 86 | def materialize_insert_decisions( |
| 87 | note_text: str, |
| 88 | target_path: Path, |
| 89 | decisions: dict, |
| 90 | asset_subdir: str, |
| 91 | ) -> list[dict]: |
| 92 | materialized: list[dict] = [] |
| 93 | asset_dir = target_path.parent / asset_subdir |
| 94 | for item in insert_decisions(decisions): |
| 95 | source_value = str(item.get("source_image_path", "")).strip() |
| 96 | source_image = Path(source_value).expanduser() |
| 97 | if not source_value or not source_image.is_file(): |
| 98 | label = item.get("source_id") or item.get("label") or item.get("item_id") or "unknown" |
| 99 | raise SystemExit(f"Insert decision source image does not exist for {label}: {source_value}") |
| 100 | filename = safe_image_filename( |
| 101 | str(item.get("source_image_filename", "")), |
| 102 | source_image, |
| 103 | ) |
| 104 | expected_relative = f"{asset_subdir}/{filename}" |
| 105 | if not note_references_image_embed(note_text, expected_relative): |
| 106 | label = item.get("source_id") or item.get("label") or item.get("item_id") or filename |
| 107 | raise SystemExit( |
| 108 | f"Insert decision for {label} is not referenced as an image embed: {expected_relative}." |
| 109 | ) |
| 110 | asset_dir.mkdir(parents=True, exist_ok=True) |
| 111 | dest_image = asset_dir / filename |
| 112 | if dest_image.resolve().parent != asset_dir.resolve(): |
| 113 | raise SystemExit(f"Unsafe figure image destination: {dest_image}") |
| 114 | if source_image.resolve() != dest_image.resolve(): |
| 115 | shutil.copy2(source_image, dest_image) |
| 116 | materialized.append( |
| 117 | { |
| 118 | "source_id": item.get("source_id") or item.get("label") or item.get("item_id") or "", |
| 119 | "source_image": str(source_image.resolve()), |
| 120 | "dest_image_path": str(dest_image), |
| 121 | "relative_markdown_path": expected_relative, |
| 122 | } |
| 123 | ) |
| 124 | return materialized |
| 125 | |
| 126 | |
| 127 | def main() -> None: |
no test coverage detected