from __future__ import annotations import re HELPSCOUT_EMAIL_TEMPLATE = """\
{greeting}
{body}
{signature}
""" HELPSCOUT_NOTE_TEMPLATE = """\
AI Analysis: {content}
""" def wrap_email_reply( body_html: str, greeting: str = "", signature: str = "", ) -> str: greeting_html = f"

{greeting}

" if greeting else "" signature_html = f"

{signature}

" if signature else "" return HELPSCOUT_EMAIL_TEMPLATE.format( greeting=greeting_html, body=body_html, signature=signature_html, ) def wrap_ai_note(content: str) -> str: escaped = content.replace("&", "&").replace("<", "<").replace(">", ">") return HELPSCOUT_NOTE_TEMPLATE.format(content=escaped) def text_to_html_body(text: str) -> str: result = text result = re.sub(r"\*\*(.+?)\*\*", r"\1", result) result = re.sub(r"\*(.+?)\*", r"\1", result) result = re.sub(r"\[([^\]]+)\]\(([^\)]+)\)", r'\1', result) lines = result.split("\n") paragraphs = [] current = "" for line in lines: line = line.strip() if not line: if current: paragraphs.append(f"

{current}

") current = "" continue if line.startswith("- ") or line.startswith("* "): if current: paragraphs.append(f"

{current}

") current = "" item = line[2:] paragraphs.append(f"
{item}
") else: if current: current += f"
{line}" else: current = line if current: paragraphs.append(f"

{current}

") return "\n".join(paragraphs)