33 lines
992 B
Python
33 lines
992 B
Python
"""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)<br\s*/?>", "\n", text)
|
|
text = re.sub(r"(?i)</p\s*>", "\n\n", text)
|
|
text = re.sub(r"(?i)</h[1-6]\s*>", "\n\n", text)
|
|
text = re.sub(r"(?i)</li\s*>", "\n", text)
|
|
text = re.sub(r"(?i)<li[^>]*>", "• ", text)
|
|
text = re.sub(r"(?i)</tr\s*>", "\n", text)
|
|
text = re.sub(r"(?i)</td\s*>", "\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>{p.replace(chr(10), '<br/>')}</p>" for p in parts)
|