comparison mercurial/templatefilters.py @ 11891:0bedf3a2062a stable

templatefilters: unnest uescape()
author Patrick Mezard <pmezard@gmail.com>
date Sun, 15 Aug 2010 17:50:52 +0200
parents 9dac951d0185
children 48a4acd1ccf1
comparison
equal deleted inserted replaced
11890:9dac951d0185 11891:0bedf3a2062a
138 .replace('>', '&gt;') 138 .replace('>', '&gt;')
139 .replace('"', '&quot;') 139 .replace('"', '&quot;')
140 .replace("'", '&#39;')) # &apos; invalid in HTML 140 .replace("'", '&#39;')) # &apos; invalid in HTML
141 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) 141 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text)
142 142
143 def uescape(c):
144 if ord(c) < 0x80:
145 return c
146 else:
147 return '\\u%04x' % ord(c)
148
143 _escapes = [ 149 _escapes = [
144 ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), 150 ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'),
145 ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), 151 ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'),
146 ] 152 ]
147 153
148 def jsonescape(s): 154 def jsonescape(s):
149 for k, v in _escapes: 155 for k, v in _escapes:
150 s = s.replace(k, v) 156 s = s.replace(k, v)
151
152 def uescape(c):
153 if ord(c) < 0x80:
154 return c
155 else:
156 return '\\u%04x' % ord(c)
157 return ''.join(uescape(c) for c in s) 157 return ''.join(uescape(c) for c in s)
158 158
159 def json(obj): 159 def json(obj):
160 if obj is None or obj is False or obj is True: 160 if obj is None or obj is False or obj is True:
161 return {None: 'null', False: 'false', True: 'true'}[obj] 161 return {None: 'null', False: 'false', True: 'true'}[obj]