# ---------- misc helpers / filters ----------
from markupsafe import Markup, escape

def highlight(text: str, needle: str, cls: str = "bg-yellow-200 rounded") -> Markup:
    """
    Wraps case-insensitive matches of `needle` inside <mark class="...">.
    Escapes everything safely.
    """
    import re
    if not text or not needle:
        return Markup(escape(text or ""))

    pattern = re.compile(re.escape(needle), re.IGNORECASE)

    def repl(m: re.Match) -> str:
        return f'<mark class="{escape(cls)}">{escape(m.group(0))}</mark>'

    esc = escape(text)
    result = pattern.sub(lambda m: Markup(repl(m)), esc)
    return Markup(result)

