From 8486805b982b49c1a12f28945ec7346a605d0f5b Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Wed, 5 Dec 2012 12:12:11 +0100 Subject: Snip quoted text in replies --- hyperkitty/static/css/style.css | 4 ++ hyperkitty/templates/messages/message.html | 9 +---- hyperkitty/templates/thread.html | 6 +-- hyperkitty/templatetags/hk_generic.py | 34 ++++++++++++++++ hyperkitty/tests/__init__.py | 1 + hyperkitty/tests/test_templatetags.py | 64 ++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 hyperkitty/tests/test_templatetags.py diff --git a/hyperkitty/static/css/style.css b/hyperkitty/static/css/style.css index 3a7803d..f68125e 100644 --- a/hyperkitty/static/css/style.css +++ b/hyperkitty/static/css/style.css @@ -697,3 +697,7 @@ ul.attachments-list li { margin-top: 5px; } +.email_body .quoted-text { + display: none; +} + diff --git a/hyperkitty/templates/messages/message.html b/hyperkitty/templates/messages/message.html index 0dd93a8..d235813 100644 --- a/hyperkitty/templates/messages/message.html +++ b/hyperkitty/templates/messages/message.html @@ -29,13 +29,8 @@ -{% if unfolded %} -
-{% else %} - +
{{ email.content|urlizetrunc:76|escapeemail|snip_quoted }}
{% if unfolded and email.attachments|count %}

Attachments:

diff --git a/hyperkitty/templates/thread.html b/hyperkitty/templates/thread.html index 20dc8c6..9e1333f 100644 --- a/hyperkitty/templates/thread.html +++ b/hyperkitty/templates/thread.html @@ -44,9 +44,9 @@ diff --git a/hyperkitty/templatetags/hk_generic.py b/hyperkitty/templatetags/hk_generic.py index bfe234c..18d397f 100644 --- a/hyperkitty/templatetags/hk_generic.py +++ b/hyperkitty/templatetags/hk_generic.py @@ -5,6 +5,8 @@ from dateutil.tz import tzutc, tzoffset from django import template from django.utils.datastructures import SortedDict from django.templatetags.tz import localtime +from django.utils.html import conditional_escape +from django.utils.safestring import mark_safe register = template.Library() @@ -114,3 +116,35 @@ def sender_date(email): def viewer_date(email): email_date = email.date.replace(tzinfo=tzutc()) return localtime(email_date) + + +SNIPPED_RE = re.compile("^\s*>.*$", re.M) +@register.filter(needs_autoescape=True) +def snip_quoted(content, quotemsg="[...]", autoescape=None): + """Snip quoted text in messages""" + if autoescape: + content = conditional_escape(content) + quoted = [] + current_quote = [] + quoting = False + lastline = None + for line in content.split("\n"): + if SNIPPED_RE.match(line): + quoting = True + if lastline == "": + current_quote.append(lastline) + else: + quoting = False + if current_quote: + quoted.append(current_quote[:]) + current_quote = [] + if quoting: + current_quote.append(line) + lastline = line + for quote in quoted: + replaced = ('%s' % quotemsg + +'\n' + +"\n".join([q for q in quote if q != ""]) + +'') + content = content.replace("\n".join(quote), replaced) + return mark_safe(content) diff --git a/hyperkitty/tests/__init__.py b/hyperkitty/tests/__init__.py index bf3e6b6..f3ee25f 100644 --- a/hyperkitty/tests/__init__.py +++ b/hyperkitty/tests/__init__.py @@ -22,3 +22,4 @@ from hyperkitty.tests.test_views import * from hyperkitty.tests.test_models import * from hyperkitty.tests.test_forms import * +from hyperkitty.tests.test_templatetags import * diff --git a/hyperkitty/tests/test_templatetags.py b/hyperkitty/tests/test_templatetags.py new file mode 100644 index 0000000..1275b25 --- /dev/null +++ b/hyperkitty/tests/test_templatetags.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 1998-2012 by the Free Software Foundation, Inc. +# +# This file is part of HyperKitty. +# +# HyperKitty is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# HyperKitty is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# HyperKitty. If not, see . +# +# Author: Aurelien Bompard +# + +from django.test import TestCase + +from hyperkitty.templatetags.hk_generic import snip_quoted + +class SnipQuotedTestCase(TestCase): + + quotemsg = "[SNIP]" + + def test_quote_1(self): + contents = """ +On Fri, 09.11.12 11:27, Someone wrote: +> This is the first quoted line +> This is the second quoted line +This is the response. +""" + expected = """ +On Fri, 09.11.12 11:27, Someone wrote: +%s +> This is the first quoted line +> This is the second quoted line +This is the response. +""" % self.quotemsg + result = snip_quoted(contents, self.quotemsg) + self.assertEqual(result, expected) + + def test_quote_2(self): + """The quote starts with a newline""" + contents = """ +On Fri, 09.11.12 11:27, Someone wrote: + +> This is the first quoted line +> This is the second quoted line +This is the response. +""" + expected = """ +On Fri, 09.11.12 11:27, Someone wrote: +%s +> This is the first quoted line +> This is the second quoted line +This is the response. +""" % self.quotemsg + result = snip_quoted(contents, self.quotemsg) + self.assertEqual(result, expected) -- cgit