[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
This commit is contained in:
parent
b36547e3ef
commit
e6a9084677
|
@ -76,7 +76,7 @@
|
|||
<field name="company_id" ref="main_company"/>
|
||||
<field name="company_ids" eval="[(4, ref('main_company'))]"/>
|
||||
<field name="menu_id" ref="action_menu_admin"/>
|
||||
<field name="signature">Administrator</field>
|
||||
<field name="signature">--\nAdministrator</field>
|
||||
</record>
|
||||
|
||||
<record id="main_partner" model="res.partner">
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
<field name="partner_id" ref="base.partner_demo"/>
|
||||
<field name="login">demo</field>
|
||||
<field name="password">demo</field>
|
||||
<field name="signature">Mr Demo</field>
|
||||
<field name="signature">--\nMr Demo</field>
|
||||
<field name="company_id" ref="main_company"/>
|
||||
<field name="groups_id" eval="[(6,0,[ref('base.group_user')])]"/>
|
||||
</record>
|
||||
|
|
|
@ -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 = """
|
||||
<font size="2" style="color: rgb(31, 31, 31); font-family: monospace; font-variant: normal; line-height: normal; ">test1</font>
|
||||
|
@ -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',
|
||||
"<div><p>First <br/>Second <br/>Third</p><p>Paragraph</p><p>--<br/>Signature paragraph</p></div>"),
|
||||
]
|
||||
for content, container_tag, expected in cases:
|
||||
html = text2html(content, container_tag)
|
||||
self.assertEqual(html, expected, 'text2html is broken')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest2.main()
|
||||
|
|
|
@ -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('<strong>','*').replace('</strong>','*')
|
||||
html = html.replace('<b>','*').replace('</b>','*')
|
||||
html = html.replace('<h3>','*').replace('</h3>','*')
|
||||
html = html.replace('<h2>','**').replace('</h2>','**')
|
||||
html = html.replace('<h1>','**').replace('</h1>','**')
|
||||
html = html.replace('<em>','/').replace('</em>','/')
|
||||
html = html.replace('<strong>', '*').replace('</strong>', '*')
|
||||
html = html.replace('<b>', '*').replace('</b>', '*')
|
||||
html = html.replace('<h3>', '*').replace('</h3>', '*')
|
||||
html = html.replace('<h2>', '**').replace('</h2>', '**')
|
||||
html = html.replace('<h1>', '**').replace('</h1>', '**')
|
||||
html = html.replace('<em>', '/').replace('</em>', '/')
|
||||
html = html.replace('<tr>', '\n')
|
||||
html = html.replace('</p>', '\n')
|
||||
html = re.sub('<br\s*/?>', '\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 <br />
|
||||
- enclose content into <p>
|
||||
- 2 or more consecutive <br /> are considered as paragraph breaks
|
||||
|
||||
:param string container_tag: container of the html; by default the
|
||||
content is embedded into a <div>
|
||||
"""
|
||||
text = cgi.escape(text)
|
||||
|
||||
# 1. replace \n and \r
|
||||
text = text.replace('\n', '<br/>')
|
||||
text = text.replace('\r', '<br/>')
|
||||
|
||||
# 2-3: form paragraphs
|
||||
idx = 0
|
||||
final = '<p>'
|
||||
br_tags = re.compile(r'(([<]\s*[bB][rR]\s*\/?[>]\s*){2,})')
|
||||
for item in re.finditer(br_tags, text):
|
||||
final += text[idx:item.start()] + '</p><p>'
|
||||
idx = item.end()
|
||||
final += text[idx:] + '</p>'
|
||||
|
||||
# 4. container
|
||||
if container_tag:
|
||||
final = '<%s>%s</%s>' % (container_tag, final, container_tag)
|
||||
return final
|
||||
|
||||
#----------------------------------------------------------
|
||||
# Emails
|
||||
|
|
Loading…
Reference in New Issue