\s*
\s*<\/div>)', re.IGNORECASE)
- html = _replace_matching_regex(br_div_tags, html, '
')
+ inner_html = _replace_matching_regex(br_div_tags, html, '
')
# form a tree
- root = lxml.html.fromstring(html)
+ root = lxml.html.fromstring(inner_html)
if not len(root) and root.text is None and root.tail is None:
- html = '
%s
' % html
- root = lxml.html.fromstring(html)
+ inner_html = '
%s
' % inner_html
+ root = lxml.html.fromstring(inner_html)
quote_tags = re.compile(r'(\n(>)+[^\n\r]*)')
signature = re.compile(r'([-]{2,}[\s]?[\r\n]{1,2}[\s\S]+)')
@@ -333,6 +333,7 @@ def html_email_clean(html, remove=False, shorten=False, max_length=300, expand_o
# tree: tag nodes
# signature_begin = False # try dynamic signature recognition
+ quoted = False
quote_begin = False
overlength = False
overlength_section_id = None
@@ -381,9 +382,11 @@ def html_email_clean(html, remove=False, shorten=False, max_length=300, expand_o
if node.tag == 'blockquote' or node.get('text_quote') or node.get('text_signature'):
# here no quote_begin because we want to be able to remove some quoted
# text without removing all the remaining context
+ quoted = True
node.set('in_quote', '1')
if node.getparent() is not None and node.getparent().get('in_quote'):
# inside a block of removed text but not in quote_begin (see above)
+ quoted = True
node.set('in_quote', '1')
# shorten:
@@ -445,12 +448,14 @@ def html_email_clean(html, remove=False, shorten=False, max_length=300, expand_o
node_class = node.get('class', '') + ' oe_mail_cleaned'
node.set('class', node_class)
+ if not overlength and not quote_begin and not quoted:
+ return html
+
# html: \n that were tail of elements have been encapsulated into
-> back to \n
- html = etree.tostring(root, pretty_print=False)
+ html = etree.tostring(root, pretty_print=False, encoding='UTF-8')
linebreaks = re.compile(r']*>([\s]*[\r\n]+[\s]*)<\/span>', re.IGNORECASE | re.DOTALL)
html = _replace_matching_regex(linebreaks, html, '\n')
-
- return html
+ return ustr(html)
#----------------------------------------------------------