Mercurial > hg
comparison mercurial/templatefilters.py @ 6691:0dba955c2636
add graph page to hgweb
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Wed, 18 Jun 2008 07:06:41 +0200 |
parents | 8999d1249171 |
children | 9c8bbae02e9c |
comparison
equal
deleted
inserted
replaced
6690:127e8c3466d1 | 6691:0dba955c2636 |
---|---|
120 .replace('>', '>') | 120 .replace('>', '>') |
121 .replace('"', '"') | 121 .replace('"', '"') |
122 .replace("'", ''')) # ' invalid in HTML | 122 .replace("'", ''')) # ' invalid in HTML |
123 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) | 123 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
124 | 124 |
125 _escapes = [ | |
126 ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), | |
127 ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), | |
128 ] | |
129 | |
130 def json(obj): | |
131 if obj is None or obj is False or obj is True: | |
132 return {None: 'null', False: 'false', True: 'true'}[obj] | |
133 elif isinstance(obj, int) or isinstance(obj, float): | |
134 return str(obj) | |
135 elif isinstance(obj, str): | |
136 for k, v in _escapes: | |
137 obj = obj.replace(k, v) | |
138 return '"%s"' % obj | |
139 elif isinstance(obj, unicode): | |
140 return json(obj.encode('utf-8')) | |
141 elif hasattr(obj, 'keys'): | |
142 out = [] | |
143 for k, v in obj.iteritems(): | |
144 s = '%s: %s' % (json(k), json(v)) | |
145 out.append(s) | |
146 return '{' + ', '.join(out) + '}' | |
147 elif hasattr(obj, '__iter__'): | |
148 out = [] | |
149 for i in obj: | |
150 out.append(json(i)) | |
151 return '[' + ', '.join(out) + ']' | |
152 else: | |
153 raise TypeError('cannot encode type %s' % obj.__class__.__name__) | |
154 | |
125 filters = { | 155 filters = { |
126 "addbreaks": nl2br, | 156 "addbreaks": nl2br, |
127 "basename": os.path.basename, | 157 "basename": os.path.basename, |
128 "age": age, | 158 "age": age, |
129 "date": lambda x: util.datestr(x), | 159 "date": lambda x: util.datestr(x), |
148 "strip": lambda x: x.strip(), | 178 "strip": lambda x: x.strip(), |
149 "urlescape": lambda x: urllib.quote(x), | 179 "urlescape": lambda x: urllib.quote(x), |
150 "user": lambda x: util.shortuser(x), | 180 "user": lambda x: util.shortuser(x), |
151 "stringescape": lambda x: x.encode('string_escape'), | 181 "stringescape": lambda x: x.encode('string_escape'), |
152 "xmlescape": xmlescape, | 182 "xmlescape": xmlescape, |
153 } | 183 "json": json, |
154 | 184 } |