#!/usr/bin/env python3 import argparse import html import json import mimetypes import re import smtplib import ssl import uuid from email.message import EmailMessage from email.utils import formatdate, make_msgid from datetime import datetime from pathlib import Path try: from common.artifact_manager import resolve_artifact_path except ImportError: # pragma: no cover - supports direct CLI execution import sys sys.path.append(str(Path(__file__).resolve().parents[1])) from common.artifact_manager import resolve_artifact_path IMAGE_PATTERN = re.compile(r"!\[([^\]]*)\]\(([^)]+)\)") STRONG_PHRASES = [ "Wuling Overseas Business Department", "SGMW / Wuling", "30 million vehicles", "1 million vehicles", "60+ countries", "200+ overseas sales and service outlets", "BYD", "Chery", "more practical vehicle models, broader customer scenarios, and clearer visible profit margins for dealers", "20-30 minute online meeting", "20–30 minute online meeting", ] def extract_inline_images(markdown_body, base_dirs=None): images = [] base_dirs = [Path(item) for item in (base_dirs or []) if item] def replace(match): alt = match.group(1).strip() or "image" raw_path = match.group(2).strip().strip('"') path = Path(raw_path) if not path.is_absolute(): for base_dir in base_dirs: candidate = (base_dir / raw_path).resolve() if candidate.exists(): path = candidate break cid = uuid.uuid4().hex images.append({"alt": alt, "path": path, "cid": cid}) return f"[[INLINE_IMAGE:{len(images) - 1}]]" return IMAGE_PATTERN.sub(replace, markdown_body), images def render_inline_markdown(text): escaped = html.escape(text) escaped = re.sub(r"\*\*(.+?)\*\*", r"\1", escaped) escaped = re.sub( r"\[([^\]]+)\]\((mailto:[^)]+)\)", r'\1', escaped, ) escaped = re.sub( r"\[([^\]]+)\]\((https?://[^)]+)\)", r'\1', escaped, ) escaped = re.sub( r"(Email:\s*)(?!\2', escaped, ) escaped = re.sub( r"(Website:\s*)(?!\2', escaped, ) return escaped def markdownish_to_html(body, base_dirs=None): body_without_images, images = extract_inline_images(body, base_dirs=base_dirs) lines = [line.rstrip() for line in body_without_images.replace("\r\n", "\n").split("\n")] html_blocks = [] in_list = False def close_list(): nonlocal in_list if in_list: html_blocks.append("") in_list = False for line in lines: stripped = line.strip() if not stripped: close_list() continue if stripped.startswith("[[INLINE_IMAGE:"): close_list() index = int(re.search(r"\d+", stripped).group(0)) image = images[index] alt = html.escape(image["alt"]) html_blocks.append( f'
{render_inline_markdown(stripped)}
' ) close_list() styles = """ .email-shell { margin: 0; padding: 0; color: #1f2937; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 1.55; } .email-shell p { margin: 0 0 10px 0; } .email-shell strong { font-weight: 700; color: #111827; } .email-shell .signature { margin: 0 0 3px 0; } .email-shell .image-wrap { margin: 10px 0 16px 0; } .email-shell img { display: block; width: 300px; max-width: 300px; height: auto; border: 0; outline: none; text-decoration: none; } .email-shell a { color: #2563eb; text-decoration: underline; } """ html_body = "\n".join(html_blocks) return f"""