PRE wrap plaintext watches in HTML output

This commit is contained in:
dgtlmoon
2025-10-25 16:04:03 +02:00
parent d0c825786e
commit 2ea6ca6cf6
2 changed files with 57 additions and 1 deletions
@@ -0,0 +1,46 @@
def as_monospaced_html_email(content: str, title: str) -> str:
"""
Wraps `content` in a minimal, email-safe HTML template
that forces monospace rendering across Gmail, Hotmail, Apple Mail, etc.
Args:
content: The body text (plain text or HTML-like).
convert_newlines: If True, replaces '\n' with '<br>' for display.
Returns:
A complete HTML document string suitable for sending as an email body.
"""
# All line feed types should be removed and then this function should only be fed <br>'s
# Then it works with our <pre> styling without double linefeeds
content = content.translate(str.maketrans('', '', '\r\n'))
if title:
import html
title = html.escape(title)
else:
title = ''
# 2. Full email-safe HTML
html_email = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="x-apple-disable-message-reformatting">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--[if mso]>
<style>
body, div, pre, td {{ font-family: "Courier New", Courier, monospace !important; }}
</style>
<![endif]-->
<title>{title}</title>
</head>
<body style="-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;">
<pre role="article" aria-roledescription="email" lang="en"
style="line-height:1.4;
font-family: monospace, 'Courier New', Courier;
white-space: pre-wrap; word-break: break-word;">
{content}
</pre>
</body>
</html>"""
return html_email
+11 -1
View File
@@ -6,6 +6,7 @@ from loguru import logger
from urllib.parse import urlparse
from .apprise_plugin.assets import apprise_asset, APPRISE_AVATAR_URL
from .apprise_plugin.custom_handlers import SUPPORTED_HTTP_METHODS
from .email_helpers import as_monospaced_html_email
from ..diff import HTML_REMOVED_STYLE, REMOVED_PLACEMARKER_OPEN, REMOVED_PLACEMARKER_CLOSED, ADDED_PLACEMARKER_OPEN, HTML_ADDED_STYLE, \
ADDED_PLACEMARKER_CLOSED, CHANGED_INTO_PLACEMARKER_OPEN, CHANGED_INTO_PLACEMARKER_CLOSED, CHANGED_PLACEMARKER_OPEN, \
CHANGED_PLACEMARKER_CLOSED, HTML_CHANGED_STYLE, HTML_CHANGED_INTO_STYLE
@@ -304,7 +305,7 @@ def process_notification(n_object: NotificationContextData, datastore):
# Could have arrived at any stage, so we dont end up running .escape on it
if 'html' in requested_output_format:
n_body = n_body.replace(CUSTOM_LINEBREAK_PLACEHOLDER, '<br>\n')
n_body = n_body.replace(CUSTOM_LINEBREAK_PLACEHOLDER, '<br>\r\n')
else:
# Markup, text types etc
n_body = n_body.replace(CUSTOM_LINEBREAK_PLACEHOLDER, '\r\n')
@@ -314,6 +315,15 @@ def process_notification(n_object: NotificationContextData, datastore):
'url': url})
apobj.add(url)
# Enforce monospace output for PLAINTEXT Document going to a HTML style notification
watch_mime_type = n_object.get('watch_mime_type')
if watch_mime_type and 'text/' in watch_mime_type.lower() and not 'html' in watch_mime_type.lower():
if 'html' in requested_output_format:
# Since they likely want HTML style notifications but of a Plain-text document, lets wrap the body
if not '<pre' in n_body and not '<body' in n_body:
# Wrap in monospace layout so it looks like "plaintext", remove double line-feeds
n_body = as_monospaced_html_email(content=n_body, title=n_title)
apobj.notify(
title=n_title,
body=n_body,