"""HTML helpers for ESPD rich-text content."""
from __future__ import annotations
import re
from html import unescape
def html_to_plain(html: str) -> str:
if not html:
return ""
text = html
text = re.sub(r"(?i)
", "\n", text)
text = re.sub(r"(?i)
", "\n\n", text)
text = re.sub(r"(?i)", "\n\n", text)
text = re.sub(r"(?i)", "\n", text)
text = re.sub(r"(?i)]*>", "• ", text)
text = re.sub(r"(?i)", "\n", text)
text = re.sub(r"(?i)", "\t", text)
text = re.sub(r"<[^>]+>", "", text)
text = unescape(text)
text = re.sub(r"\n{3,}", "\n\n", text)
return text.strip()
def plain_to_html(text: str) -> str:
if not text:
return ""
parts = [p.strip() for p in text.replace("\r\n", "\n").split("\n\n") if p.strip()]
if not parts:
parts = [text.strip()] if text.strip() else []
return "".join(f"{p.replace(chr(10), '
')}
" for p in parts)