Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/templatefilters.py @ 13590:1a752dcfe062
templatefilters: wrap all filters in dedicated functions
This will highly simplify the docstring integration. I measured "hg log
--style=changelog" duration on mercurial itself and could not detect any
difference.
author | Patrick Mezard <pmezard@gmail.com> |
---|---|
date | Sat, 12 Mar 2011 12:46:31 +0100 |
parents | b0a4b05c25e2 |
children | 264f292a0c6f |
comparison
equal
deleted
inserted
replaced
13589:b0a4b05c25e2 | 13590:1a752dcfe062 |
---|---|
41 | 41 |
42 for t, s in agescales: | 42 for t, s in agescales: |
43 n = delta // s | 43 n = delta // s |
44 if n >= 2 or s == 1: | 44 if n >= 2 or s == 1: |
45 return '%s ago' % fmt(t, n) | 45 return '%s ago' % fmt(t, n) |
46 | |
47 def basename(path): | |
48 return os.path.basename(path) | |
49 | |
50 def datefilter(text): | |
51 return util.datestr(text) | |
46 | 52 |
47 def domain(author): | 53 def domain(author): |
48 '''get domain of author, or empty string if none.''' | 54 '''get domain of author, or empty string if none.''' |
49 f = author.find('@') | 55 f = author.find('@') |
50 if f == -1: | 56 if f == -1: |
52 author = author[f + 1:] | 58 author = author[f + 1:] |
53 f = author.find('>') | 59 f = author.find('>') |
54 if f >= 0: | 60 if f >= 0: |
55 author = author[:f] | 61 author = author[:f] |
56 return author | 62 return author |
63 | |
64 def email(text): | |
65 return util.email(text) | |
66 | |
67 def escape(text): | |
68 return cgi.escape(text, True) | |
57 | 69 |
58 para_re = None | 70 para_re = None |
59 space_re = None | 71 space_re = None |
60 | 72 |
61 def fill(text, width): | 73 def fill(text, width): |
81 start = m.end(1) | 93 start = m.end(1) |
82 | 94 |
83 return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest | 95 return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest |
84 for para, rest in findparas()]) | 96 for para, rest in findparas()]) |
85 | 97 |
98 def fill68(text): | |
99 return fill(text, 68) | |
100 | |
101 def fill76(text): | |
102 return fill(text, 76) | |
103 | |
86 def firstline(text): | 104 def firstline(text): |
87 '''return the first line of text''' | 105 '''return the first line of text''' |
88 try: | 106 try: |
89 return text.splitlines(True)[0].rstrip('\r\n') | 107 return text.splitlines(True)[0].rstrip('\r\n') |
90 except IndexError: | 108 except IndexError: |
91 return '' | 109 return '' |
110 | |
111 def hexfilter(text): | |
112 return node.hex(text) | |
113 | |
114 def hgdate(text): | |
115 return "%d %d" % text | |
116 | |
117 def isodate(text): | |
118 return util.datestr(text, '%Y-%m-%d %H:%M %1%2') | |
119 | |
120 def isodatesec(text): | |
121 return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2') | |
92 | 122 |
93 def indent(text, prefix): | 123 def indent(text, prefix): |
94 '''indent each non-empty line of text after first with prefix.''' | 124 '''indent each non-empty line of text after first with prefix.''' |
95 lines = text.splitlines() | 125 lines = text.splitlines() |
96 num_lines = len(lines) | 126 num_lines = len(lines) |
143 def jsonescape(s): | 173 def jsonescape(s): |
144 for k, v in _escapes: | 174 for k, v in _escapes: |
145 s = s.replace(k, v) | 175 s = s.replace(k, v) |
146 return ''.join(_uescape(c) for c in s) | 176 return ''.join(_uescape(c) for c in s) |
147 | 177 |
178 def localdate(text): | |
179 return (text[0], util.makedate()[1]) | |
180 | |
148 def nonempty(str): | 181 def nonempty(str): |
149 return str or "(none)" | 182 return str or "(none)" |
150 | 183 |
151 def obfuscate(text): | 184 def obfuscate(text): |
152 text = unicode(text, encoding.encoding, 'replace') | 185 text = unicode(text, encoding.encoding, 'replace') |
166 f = author.find('<') | 199 f = author.find('<') |
167 if f == -1: | 200 if f == -1: |
168 return util.shortuser(author) | 201 return util.shortuser(author) |
169 return author[:f].rstrip() | 202 return author[:f].rstrip() |
170 | 203 |
204 def rfc3339date(text): | |
205 return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2") | |
206 | |
207 def rfc822date(text): | |
208 return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2") | |
209 | |
210 def short(text): | |
211 return text[:12] | |
212 | |
213 def shortdate(text): | |
214 return util.shortdate(text) | |
215 | |
216 def stringescape(text): | |
217 return text.encode('string_escape') | |
218 | |
171 def stringify(thing): | 219 def stringify(thing): |
172 '''turn nested template iterator into string.''' | 220 '''turn nested template iterator into string.''' |
173 if hasattr(thing, '__iter__') and not isinstance(thing, str): | 221 if hasattr(thing, '__iter__') and not isinstance(thing, str): |
174 return "".join([stringify(t) for t in thing if t is not None]) | 222 return "".join([stringify(t) for t in thing if t is not None]) |
175 return str(thing) | 223 return str(thing) |
224 | |
225 def strip(text): | |
226 return text.strip() | |
176 | 227 |
177 def stripdir(text): | 228 def stripdir(text): |
178 '''Treat the text as path and strip a directory level, if possible.''' | 229 '''Treat the text as path and strip a directory level, if possible.''' |
179 dir = os.path.dirname(text) | 230 dir = os.path.dirname(text) |
180 if dir == "": | 231 if dir == "": |
181 return os.path.basename(text) | 232 return os.path.basename(text) |
182 else: | 233 else: |
183 return dir | 234 return dir |
235 | |
236 def tabindent(text): | |
237 return indent(text, '\t') | |
238 | |
239 def urlescape(text): | |
240 return urllib.quote(text) | |
241 | |
242 def userfilter(text): | |
243 return util.shortuser(text) | |
184 | 244 |
185 def xmlescape(text): | 245 def xmlescape(text): |
186 text = (text | 246 text = (text |
187 .replace('&', '&') | 247 .replace('&', '&') |
188 .replace('<', '<') | 248 .replace('<', '<') |
192 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) | 252 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
193 | 253 |
194 filters = { | 254 filters = { |
195 "addbreaks": addbreaks, | 255 "addbreaks": addbreaks, |
196 "age": age, | 256 "age": age, |
197 "basename": os.path.basename, | 257 "basename": basename, |
198 "date": lambda x: util.datestr(x), | 258 "date": datefilter, |
199 "domain": domain, | 259 "domain": domain, |
200 "email": util.email, | 260 "email": email, |
201 "escape": lambda x: cgi.escape(x, True), | 261 "escape": escape, |
202 "fill68": lambda x: fill(x, width=68), | 262 "fill68": fill68, |
203 "fill76": lambda x: fill(x, width=76), | 263 "fill76": fill76, |
204 "firstline": firstline, | 264 "firstline": firstline, |
205 "hex": node.hex, | 265 "hex": hexfilter, |
206 "hgdate": lambda x: "%d %d" % x, | 266 "hgdate": hgdate, |
207 "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'), | 267 "isodate": isodate, |
208 "isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'), | 268 "isodatesec": isodatesec, |
209 "json": json, | 269 "json": json, |
210 "jsonescape": jsonescape, | 270 "jsonescape": jsonescape, |
211 "localdate": lambda x: (x[0], util.makedate()[1]), | 271 "localdate": localdate, |
212 "nonempty": nonempty, | 272 "nonempty": nonempty, |
213 "obfuscate": obfuscate, | 273 "obfuscate": obfuscate, |
214 "permissions": permissions, | 274 "permissions": permissions, |
215 "person": person, | 275 "person": person, |
216 "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"), | 276 "rfc3339date": rfc3339date, |
217 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"), | 277 "rfc822date": rfc822date, |
218 "short": lambda x: x[:12], | 278 "short": short, |
219 "shortdate": util.shortdate, | 279 "shortdate": shortdate, |
220 "stringescape": lambda x: x.encode('string_escape'), | 280 "stringescape": stringescape, |
221 "stringify": stringify, | 281 "stringify": stringify, |
222 "strip": lambda x: x.strip(), | 282 "strip": strip, |
223 "stripdir": stripdir, | 283 "stripdir": stripdir, |
224 "tabindent": lambda x: indent(x, '\t'), | 284 "tabindent": tabindent, |
225 "urlescape": lambda x: urllib.quote(x), | 285 "urlescape": urlescape, |
226 "user": lambda x: util.shortuser(x), | 286 "user": userfilter, |
227 "xmlescape": xmlescape, | 287 "xmlescape": xmlescape, |
228 } | 288 } |