From 268d11f1818829d9cd2e6b7877df6be1bc5de79a Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sun, 10 Mar 2019 12:10:01 -0400 Subject: [PATCH] title handling and bulletproofing --- apprise/plugins/NotifyBase.py | 27 ++++++++++++++++++++++++--- apprise/plugins/NotifyGnome.py | 2 +- apprise/plugins/NotifyPushed.py | 4 ++++ apprise/plugins/NotifyRyver.py | 2 +- apprise/plugins/NotifyTelegram.py | 4 +++- test/test_rest_plugins.py | 10 ++++++++++ 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/apprise/plugins/NotifyBase.py b/apprise/plugins/NotifyBase.py index 09d9875a..30699888 100644 --- a/apprise/plugins/NotifyBase.py +++ b/apprise/plugins/NotifyBase.py @@ -74,6 +74,13 @@ class NotifyBase(URLBase): # Default Overflow Mode overflow_mode = OverflowMode.UPSTREAM + # Default Title HTML Tagging + # When a title is specified for a notification service that doesn't accept + # titles, by default apprise tries to give a plesant view and convert the + # title so that it can be placed into the body. The default is to just + # use a tag. The below causes the title to get generated: + default_html_tag_id = 'b' + def __init__(self, **kwargs): """ Initialize some general configuration that will keep things consistent @@ -224,9 +231,23 @@ class NotifyBase(URLBase): # default overflow = self.overflow_mode - if self.title_maxlen <= 0: - # Content is appended to body - body = '{}\r\n{}'.format(title, body) + if self.title_maxlen <= 0 and len(title) > 0: + if self.notify_format == NotifyFormat.MARKDOWN: + # Content is appended to body as markdown + body = '**{}**\r\n{}'.format(title, body) + + elif self.notify_format == NotifyFormat.HTML: + # Content is appended to body as html + body = '<{open_tag}>{title}' \ + '
\r\n{body}'.format( + open_tag=self.default_html_tag_id, + title=self.escape_html(title), + close_tag=self.default_html_tag_id, + body=body) + else: + # Content is appended to body as text + body = '{}\r\n{}'.format(title, body) + title = '' # Enforce the line count first always diff --git a/apprise/plugins/NotifyGnome.py b/apprise/plugins/NotifyGnome.py index e274dd15..cfb40d61 100644 --- a/apprise/plugins/NotifyGnome.py +++ b/apprise/plugins/NotifyGnome.py @@ -96,7 +96,7 @@ class NotifyGnome(NotifyBase): # content to display body_max_line_count = 10 - # A title can not be used for SMS Messages. Setting this to zero will + # A title can not be used for Gnome Messages. Setting this to zero will # cause any title (if defined) to get placed into the message body. title_maxlen = 0 diff --git a/apprise/plugins/NotifyPushed.py b/apprise/plugins/NotifyPushed.py index 404edad2..05782a72 100644 --- a/apprise/plugins/NotifyPushed.py +++ b/apprise/plugins/NotifyPushed.py @@ -64,6 +64,10 @@ class NotifyPushed(NotifyBase): # Pushed uses the http protocol with JSON requests notify_url = 'https://api.pushed.co/1/push' + # A title can not be used for Pushed Messages. Setting this to zero will + # cause any title (if defined) to get placed into the message body. + title_maxlen = 0 + # The maximum allowable characters allowed in the body per message body_maxlen = 140 diff --git a/apprise/plugins/NotifyRyver.py b/apprise/plugins/NotifyRyver.py index 2a9e69ef..a7cfcd8e 100644 --- a/apprise/plugins/NotifyRyver.py +++ b/apprise/plugins/NotifyRyver.py @@ -167,7 +167,7 @@ class NotifyRyver(NotifyBase): # prepare JSON Object payload = { - "body": body if not title else '**%s**\r\n%s' % (title, body), + "body": body if not title else '**{}**\r\n{}'.format(title, body), 'createSource': { "displayName": self.user, "avatar": self.image_url(notify_type), diff --git a/apprise/plugins/NotifyTelegram.py b/apprise/plugins/NotifyTelegram.py index 9bcf4d64..353c5095 100644 --- a/apprise/plugins/NotifyTelegram.py +++ b/apprise/plugins/NotifyTelegram.py @@ -367,8 +367,10 @@ class NotifyTelegram(NotifyBase): # Tabs become 3 spaces title = re.sub(' ?', ' ', title, re.I) + # HTML + title = NotifyBase.escape_html(title, whitespace=False) + # HTML - title = NotifyBase.escape_html(title, whitespace=False) body = NotifyBase.escape_html(body, whitespace=False) # Assign the body diff --git a/test/test_rest_plugins.py b/test/test_rest_plugins.py index fc5a2d4d..05ad351d 100644 --- a/test/test_rest_plugins.py +++ b/test/test_rest_plugins.py @@ -3128,6 +3128,16 @@ def test_notify_overflow_truncate(): # Verify that we break the title to a max length of our title_max # and that the body remains untouched + + obj.notify_format = NotifyFormat.HTML + chunks = obj._apply_overflow(body=body, title=title) + assert len(chunks) == 1 + + obj.notify_format = NotifyFormat.MARKDOWN + chunks = obj._apply_overflow(body=body, title=title) + assert len(chunks) == 1 + + obj.notify_format = NotifyFormat.TEXT chunks = obj._apply_overflow(body=body, title=title) assert len(chunks) == 1