File size: 685 Bytes
6793beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
General-purpose helper utilities.
"""

import re


def clean_email_body(body: str) -> str:
    """Strip quoted reply chains and excessive whitespace from an email body."""
    # Remove lines starting with '>' (quoted replies)
    lines = [line for line in body.splitlines() if not line.strip().startswith(">")]
    cleaned = "\n".join(lines).strip()
    # Collapse multiple blank lines
    cleaned = re.sub(r"\n{3,}", "\n\n", cleaned)
    return cleaned


def truncate(text: str, max_length: int = 500) -> str:
    """Truncate text to a maximum length, adding an ellipsis if trimmed."""
    if len(text) <= max_length:
        return text
    return text[: max_length - 1] + "…"