29 lines
833 B
Python
29 lines
833 B
Python
import re
|
|
from html import escape as html_escape
|
|
|
|
import gi
|
|
|
|
gi.require_version("Pango", "1.0")
|
|
from gi.repository import GLib, Pango
|
|
|
|
# Pango cannot render <img ...> from the freedesktop notification spec; strip it.
|
|
_IMG_RE = re.compile(r"<img\b[^>]*/?>", re.IGNORECASE)
|
|
|
|
|
|
def render_body_markup(body: str) -> tuple[str, bool]:
|
|
"""Return ``(text, is_markup)`` for a notification body.
|
|
|
|
If the body parses as Pango markup, ``text`` is the cleaned-up markup
|
|
string and ``is_markup`` is True. Otherwise ``text`` is the XML-escaped
|
|
plain text and ``is_markup`` is False.
|
|
"""
|
|
if not body:
|
|
return "", False
|
|
|
|
candidate = _IMG_RE.sub("", body)
|
|
try:
|
|
Pango.parse_markup(candidate, -1, "\0")
|
|
except GLib.Error:
|
|
return html_escape(body, quote=False), False
|
|
return candidate, True
|