feat(api): add memoir image placeholder parsing
Made-with: Cursor
This commit is contained in:
49
api/services/memoir_images/parser.py
Normal file
49
api/services/memoir_images/parser.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
PLACEHOLDER_RE = re.compile(r"\{\{\{\{IMAGE:(.*?)\}\}\}\}")
|
||||
|
||||
|
||||
def parse_image_placeholders(content: str, max_images: int) -> list[dict[str, Any]]:
|
||||
items: list[dict[str, Any]] = []
|
||||
for match in PLACEHOLDER_RE.finditer(content or ""):
|
||||
description = match.group(1).strip()
|
||||
if not description:
|
||||
continue
|
||||
items.append(
|
||||
{
|
||||
"index": len(items),
|
||||
"description": description,
|
||||
"placeholder": match.group(0),
|
||||
"start_offset": match.start(),
|
||||
}
|
||||
)
|
||||
if len(items) >= max_images:
|
||||
break
|
||||
return items
|
||||
|
||||
|
||||
def build_initial_image_assets(
|
||||
placeholders: list[dict[str, Any]],
|
||||
provider: str,
|
||||
style: str,
|
||||
size: str,
|
||||
now_iso: str,
|
||||
) -> list[dict[str, Any]]:
|
||||
return [
|
||||
{
|
||||
"index": item["index"],
|
||||
"placeholder": item["placeholder"],
|
||||
"description": item["description"],
|
||||
"prompt": None,
|
||||
"url": None,
|
||||
"status": "pending",
|
||||
"provider": provider,
|
||||
"style": style,
|
||||
"size": size,
|
||||
"error": None,
|
||||
"created_at": now_iso,
|
||||
"updated_at": now_iso,
|
||||
}
|
||||
for item in placeholders
|
||||
]
|
||||
Reference in New Issue
Block a user