[ADD] tools.misc: new function append_to_html, can be used to inject blocks (html or plaintext) to existing HTML snippets

bzr revid: odo@openerp.com-20120831132337-of2p1wlh5l4lpvez
This commit is contained in:
Olivier Dony 2012-08-31 15:23:37 +02:00
parent fed157bcad
commit d37745559e
3 changed files with 54 additions and 1 deletions

View File

@ -9,7 +9,7 @@ See the :ref:`test-framework` section in the :ref:`features` list.
"""
from . import test_expression, test_html_sanitize, test_ir_sequence, test_orm,\
test_view_validation, test_uninstall
test_view_validation, test_uninstall, test_misc
fast_suite = [
test_ir_sequence,
@ -20,6 +20,7 @@ checks = [
test_html_sanitize,
test_orm,
test_view_validation,
test_misc,
]
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,21 @@
# This test can be run stand-alone with something like:
# > PYTHONPATH=. python2 openerp/tests/test_misc.py
import unittest2
class test_misc(unittest2.TestCase):
""" Test some of our generic utility functions """
def test_append_to_html(self):
from openerp.tools import append_content_to_html
test_samples = [
('<!DOCTYPE...><HTML encoding="blah">some <b>content</b></HtMl>', '--\nYours truly', True,
'<!DOCTYPE...><html encoding="blah">some <b>content</b>\n<pre>--\nYours truly</pre>\n</html>'),
('<html><body>some <b>content</b></body></html>', '<!DOCTYPE...>\n<html><body>\n<p>--</p>\n<p>Yours truly</p>\n</body>\n</html>', False,
'<html><body>some <b>content</b>\n\n\n<p>--</p>\n<p>Yours truly</p>\n\n\n</body></html>'),
]
for html, content, flag, expected in test_samples:
self.assertEqual(append_content_to_html(html,content,flag), expected, 'append_content_to_html is broken')
if __name__ == '__main__':
unittest2.main()

View File

@ -400,6 +400,37 @@ def email_split(text):
if not text: return []
return re.findall(r'([^ ,<@]+@[^> ,]+)', text)
def append_content_to_html(html, content, plaintext=True):
"""Append extra content at the end of an HTML snippet, trying
to locate the end of the HTML document (</body>, </html>, or
EOF), and wrapping the provided content in a <pre/> block
unless ``plaintext`` is False. A side-effect of this
method is to coerce all HTML tags to lowercase in ``html``,
and strips enclosing <html> or <body> tags in content if
``plaintext`` is False.
:param str html: html tagsoup (doesn't have to be XHTML)
:param str content: extra content to append
:param bool plaintext: whether content is plaintext and should
be wrapped in a <pre/> tag.
"""
html = ustr(html)
if plaintext:
content = u'\n<pre>%s</pre>\n' % ustr(content)
else:
content = re.sub(r'(?i)(</?html.*>|</?body.*>|<!\W*DOCTYPE.*>)', '', content)
content = u'\n%s\n'% ustr(content)
# Force all tags to lowercase
html = re.sub(r'(</?)\W*(\w+)([ >])',
lambda m: '%s%s%s' % (m.group(1),m.group(2).lower(),m.group(3)), html)
insert_location = html.find('</body>')
if insert_location == -1:
insert_location = html.find('</html>')
if insert_location == -1:
return '%s%s' % (html, content)
return '%s%s%s' % (html[:insert_location], content, html[insert_location:])
#----------------------------------------------------------
# SMS
#----------------------------------------------------------