diff src/tools/xml_tools.py @ 1732:cf11cfc87ef9

xml_tools, plugin XEP-0071, XEP-0277: add method expandNewLinesToXHTML: - \n in XHTML content should not be converted to <br /> by the frontend - save it directly in the Atom payload for better compatibility with other clients
author souliane <souliane@mailoo.org>
date Thu, 10 Dec 2015 14:00:21 +0100
parents 0fdd8fe34fbf
children 3770d13776e8
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Thu Dec 10 10:44:04 2015 +0100
+++ b/src/tools/xml_tools.py	Thu Dec 10 14:00:21 2015 +0100
@@ -29,7 +29,7 @@
 from twisted.internet import defer
 from sat.core import exceptions
 from collections import OrderedDict
-
+import re
 
 """This library help manage XML used in SàT (parameters, registration, etc)"""
 
@@ -1351,3 +1351,13 @@
         tmp.addRawXml(raw_xml)
         parser.parse(tmp.toXml().encode('utf-8'))
         return self.result.firstChildElement()
+
+
+def expandNewLinesToXHTML(text):
+    """If not present yet, add a <br /> before all \n occurences.
+
+    @param text(unicode)
+    @return unicode
+    """
+    # if present, replace <br> (HTML), <br > and <br/> at end of line with <br /> (XHTML)
+    return re.sub(r"<br[ ]?/?>\n", "\n", text).replace("\n", "<br />\n")