[Checkins] SVN: z3c.pt/trunk/src/z3c/pt/utils.py Added method to escape to HTML entities (missing from previous commit).

Malthe Borch mborch at gmail.com
Tue Sep 2 09:24:19 EDT 2008


Log message for revision 90704:
  Added method to escape to HTML entities (missing from previous commit).

Changed:
  U   z3c.pt/trunk/src/z3c/pt/utils.py

-=-
Modified: z3c.pt/trunk/src/z3c/pt/utils.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/utils.py	2008-09-02 13:22:13 UTC (rev 90703)
+++ z3c.pt/trunk/src/z3c/pt/utils.py	2008-09-02 13:24:18 UTC (rev 90704)
@@ -4,6 +4,8 @@
 import config
 import logging
 import interfaces
+import htmlentitydefs
+import re, string
 
 from UserDict import UserDict
 
@@ -45,6 +47,30 @@
         self.attrib[ns] = value
     return property(get, set)
 
+def escape(string, quote=None):
+    if not isinstance(string, unicode):
+        encoding = 'utf-8'
+        string = string.decode(encoding)
+    else:
+        encoding = None
+
+    table = htmlentitydefs.codepoint2name
+    def get(char):
+        key = ord(char)
+        if key in table:
+            return '&%s;' % table[key]
+        return char
+    
+    string = "".join(map(get, string))
+
+    if quote is not None:
+        string = string.replace(quote, '\\'+quote)
+
+    if encoding:
+        string = string.encode(encoding)
+        
+    return string
+
 class scope(list):
     def __init__(self, *args):
         global s_counter



More information about the Checkins mailing list