comparison mercurial/templatefilters.py @ 6174:434139080ed4

Permit XML entities to be escaped in template output. Useful for creating XML documents directly from Hg logging. Can also be used for HTML. For use in content, will escape '&', '<', and for completeness '>' (although it is not strictly necessary). For use in attributes, will also escape ' and ". Will also replace nonprinting (ASCII) control characters with spaces, since these are illegal in XML.
author Jesse Glick <jesse.glick@sun.com>
date Mon, 28 Jan 2008 22:19:12 -0500
parents 7b937b26adf7
children c3182eeb70ea
comparison
equal deleted inserted replaced
6173:963000ed8cac 6174:434139080ed4
119 return "lrwxrwxrwx" 119 return "lrwxrwxrwx"
120 if "x" in flags: 120 if "x" in flags:
121 return "-rwxr-xr-x" 121 return "-rwxr-xr-x"
122 return "-rw-r--r--" 122 return "-rw-r--r--"
123 123
124 def xmlescape(text):
125 text = (text
126 .replace('&', '&amp;')
127 .replace('<', '&lt;')
128 .replace('>', '&gt;')
129 .replace('"', '&quot;')
130 .replace("'", '&#39;')) # &apos; invalid in HTML
131 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text)
132
124 filters = { 133 filters = {
125 "addbreaks": nl2br, 134 "addbreaks": nl2br,
126 "basename": os.path.basename, 135 "basename": os.path.basename,
127 "age": age, 136 "age": age,
128 "date": lambda x: util.datestr(x), 137 "date": lambda x: util.datestr(x),
145 "stringify": templater.stringify, 154 "stringify": templater.stringify,
146 "strip": lambda x: x.strip(), 155 "strip": lambda x: x.strip(),
147 "urlescape": lambda x: urllib.quote(x), 156 "urlescape": lambda x: urllib.quote(x),
148 "user": lambda x: util.shortuser(x), 157 "user": lambda x: util.shortuser(x),
149 "stringescape": lambda x: x.encode('string_escape'), 158 "stringescape": lambda x: x.encode('string_escape'),
159 "xmlescape": xmlescape,
150 } 160 }
151 161