From e6a90846779dade003230c1ceccdc42485dc70d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 7 Nov 2012 17:41:17 +0100 Subject: [PATCH] [IMP] tools: added text2html function, basically turning plaintext into html. Added a test for this function. Updated signature for demo data. bzr revid: tde@openerp.com-20121107164117-2caoqna2aclzowpk --- openerp/addons/base/base_data.xml | 2 +- openerp/addons/base/base_demo.xml | 2 +- openerp/tests/test_mail.py | 14 +++++++++- openerp/tools/mail.py | 45 +++++++++++++++++++++++++------ 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/openerp/addons/base/base_data.xml b/openerp/addons/base/base_data.xml index 4c429fe56ff..943ae73435d 100644 --- a/openerp/addons/base/base_data.xml +++ b/openerp/addons/base/base_data.xml @@ -76,7 +76,7 @@ - Administrator + --\nAdministrator diff --git a/openerp/addons/base/base_demo.xml b/openerp/addons/base/base_demo.xml index ce675ebb99e..e1ef2009f71 100644 --- a/openerp/addons/base/base_demo.xml +++ b/openerp/addons/base/base_demo.xml @@ -10,7 +10,7 @@ demo demo - Mr Demo + --\nMr Demo diff --git a/openerp/tests/test_mail.py b/openerp/tests/test_mail.py index ab82fc39caf..5a0c062b3a0 100755 --- a/openerp/tests/test_mail.py +++ b/openerp/tests/test_mail.py @@ -23,7 +23,7 @@ ############################################################################## import unittest2 -from openerp.tools.mail import html_sanitize, html_email_clean, append_content_to_html +from openerp.tools.mail import html_sanitize, html_email_clean, append_content_to_html, text2html test_case = """ test1 @@ -189,5 +189,17 @@ class TestCleaner(unittest2.TestCase): self.assertNotIn(new_html, 'Bert Tartopoils') +class TestText2Html(unittest2.TestCase): + + def test_text2html(self): + cases = [ + ("First \nSecond \nThird\n \nParagraph\n\r--\nSignature paragraph", 'div', + "

First
Second
Third

Paragraph

--
Signature paragraph

"), + ] + for content, container_tag, expected in cases: + html = text2html(content, container_tag) + self.assertEqual(html, expected, 'text2html is broken') + + if __name__ == '__main__': unittest2.main() diff --git a/openerp/tools/mail.py b/openerp/tools/mail.py index cebc6313b08..861f6d82243 100644 --- a/openerp/tools/mail.py +++ b/openerp/tools/mail.py @@ -192,7 +192,7 @@ def html2plaintext(html, body_id=None, encoding='utf-8'): tree = fromstring(html, parser=HTMLParser()) if body_id is not None: - source = tree.xpath('//*[@id=%s]'%(body_id,)) + source = tree.xpath('//*[@id=%s]' % (body_id,)) else: source = tree.xpath('//body') if len(source): @@ -210,12 +210,12 @@ def html2plaintext(html, body_id=None, encoding='utf-8'): html = ustr(tostring(tree, encoding=encoding)) - html = html.replace('','*').replace('','*') - html = html.replace('','*').replace('','*') - html = html.replace('

','*').replace('

','*') - html = html.replace('

','**').replace('

','**') - html = html.replace('

','**').replace('

','**') - html = html.replace('','/').replace('','/') + html = html.replace('', '*').replace('', '*') + html = html.replace('', '*').replace('', '*') + html = html.replace('

', '*').replace('

', '*') + html = html.replace('

', '**').replace('

', '**') + html = html.replace('

', '**').replace('

', '**') + html = html.replace('', '/').replace('', '/') html = html.replace('', '\n') html = html.replace('

', '\n') html = re.sub('', '\n', html) @@ -229,10 +229,39 @@ def html2plaintext(html, body_id=None, encoding='utf-8'): for i, url in enumerate(url_index): if i == 0: html += '\n\n' - html += ustr('[%s] %s\n') % (i+1, url) + html += ustr('[%s] %s\n') % (i + 1, url) return html +def text2html(text, container_tag='div'): + """ Convert plaintext into html. Content of the text is escaped to manage + html entities, using cgi.escape(). + - all \n,\r are replaced by
+ - enclose content into

+ - 2 or more consecutive
are considered as paragraph breaks + + :param string container_tag: container of the html; by default the + content is embedded into a

+ """ + text = cgi.escape(text) + + # 1. replace \n and \r + text = text.replace('\n', '
') + text = text.replace('\r', '
') + + # 2-3: form paragraphs + idx = 0 + final = '

' + br_tags = re.compile(r'(([<]\s*[bB][rR]\s*\/?[>]\s*){2,})') + for item in re.finditer(br_tags, text): + final += text[idx:item.start()] + '

' + idx = item.end() + final += text[idx:] + '

' + + # 4. container + if container_tag: + final = '<%s>%s' % (container_tag, final, container_tag) + return final #---------------------------------------------------------- # Emails