Show More
@@ -1,424 +1,425 b'' | |||
|
1 | 1 | # template-filters.py - common template expansion filters |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2008 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | from i18n import _ | |
|
8 | 9 | import cgi, re, os, time, urllib |
|
9 | 10 | import encoding, node, util, error |
|
10 | 11 | import hbisect |
|
11 | 12 | |
|
12 | 13 | def addbreaks(text): |
|
13 | 14 | """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of |
|
14 | 15 | every line except the last. |
|
15 | 16 | """ |
|
16 | 17 | return text.replace('\n', '<br/>\n') |
|
17 | 18 | |
|
18 | 19 | agescales = [("year", 3600 * 24 * 365), |
|
19 | 20 | ("month", 3600 * 24 * 30), |
|
20 | 21 | ("week", 3600 * 24 * 7), |
|
21 | 22 | ("day", 3600 * 24), |
|
22 | 23 | ("hour", 3600), |
|
23 | 24 | ("minute", 60), |
|
24 | 25 | ("second", 1)] |
|
25 | 26 | |
|
26 | 27 | def age(date): |
|
27 | 28 | """:age: Date. Returns a human-readable date/time difference between the |
|
28 | 29 | given date/time and the current date/time. |
|
29 | 30 | """ |
|
30 | 31 | |
|
31 | 32 | def plural(t, c): |
|
32 | 33 | if c == 1: |
|
33 | 34 | return t |
|
34 | 35 | return t + "s" |
|
35 | 36 | def fmt(t, c): |
|
36 | 37 | return "%d %s" % (c, plural(t, c)) |
|
37 | 38 | |
|
38 | 39 | now = time.time() |
|
39 | 40 | then = date[0] |
|
40 | 41 | future = False |
|
41 | 42 | if then > now: |
|
42 | 43 | future = True |
|
43 | 44 | delta = max(1, int(then - now)) |
|
44 | 45 | if delta > agescales[0][1] * 30: |
|
45 | 46 | return 'in the distant future' |
|
46 | 47 | else: |
|
47 | 48 | delta = max(1, int(now - then)) |
|
48 | 49 | if delta > agescales[0][1] * 2: |
|
49 | 50 | return util.shortdate(date) |
|
50 | 51 | |
|
51 | 52 | for t, s in agescales: |
|
52 | 53 | n = delta // s |
|
53 | 54 | if n >= 2 or s == 1: |
|
54 | 55 | if future: |
|
55 | 56 | return '%s from now' % fmt(t, n) |
|
56 | 57 | return '%s ago' % fmt(t, n) |
|
57 | 58 | |
|
58 | 59 | def basename(path): |
|
59 | 60 | """:basename: Any text. Treats the text as a path, and returns the last |
|
60 | 61 | component of the path after splitting by the path separator |
|
61 | 62 | (ignoring trailing separators). For example, "foo/bar/baz" becomes |
|
62 | 63 | "baz" and "foo/bar//" becomes "bar". |
|
63 | 64 | """ |
|
64 | 65 | return os.path.basename(path) |
|
65 | 66 | |
|
66 | 67 | def datefilter(text): |
|
67 | 68 | """:date: Date. Returns a date in a Unix date format, including the |
|
68 | 69 | timezone: "Mon Sep 04 15:13:13 2006 0700". |
|
69 | 70 | """ |
|
70 | 71 | return util.datestr(text) |
|
71 | 72 | |
|
72 | 73 | def domain(author): |
|
73 | 74 | """:domain: Any text. Finds the first string that looks like an email |
|
74 | 75 | address, and extracts just the domain component. Example: ``User |
|
75 | 76 | <user@example.com>`` becomes ``example.com``. |
|
76 | 77 | """ |
|
77 | 78 | f = author.find('@') |
|
78 | 79 | if f == -1: |
|
79 | 80 | return '' |
|
80 | 81 | author = author[f + 1:] |
|
81 | 82 | f = author.find('>') |
|
82 | 83 | if f >= 0: |
|
83 | 84 | author = author[:f] |
|
84 | 85 | return author |
|
85 | 86 | |
|
86 | 87 | def email(text): |
|
87 | 88 | """:email: Any text. Extracts the first string that looks like an email |
|
88 | 89 | address. Example: ``User <user@example.com>`` becomes |
|
89 | 90 | ``user@example.com``. |
|
90 | 91 | """ |
|
91 | 92 | return util.email(text) |
|
92 | 93 | |
|
93 | 94 | def escape(text): |
|
94 | 95 | """:escape: Any text. Replaces the special XML/XHTML characters "&", "<" |
|
95 | 96 | and ">" with XML entities, and filters out NUL characters. |
|
96 | 97 | """ |
|
97 | 98 | return cgi.escape(text.replace('\0', ''), True) |
|
98 | 99 | |
|
99 | 100 | para_re = None |
|
100 | 101 | space_re = None |
|
101 | 102 | |
|
102 | 103 | def fill(text, width): |
|
103 | 104 | '''fill many paragraphs.''' |
|
104 | 105 | global para_re, space_re |
|
105 | 106 | if para_re is None: |
|
106 | 107 | para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) |
|
107 | 108 | space_re = re.compile(r' +') |
|
108 | 109 | |
|
109 | 110 | def findparas(): |
|
110 | 111 | start = 0 |
|
111 | 112 | while True: |
|
112 | 113 | m = para_re.search(text, start) |
|
113 | 114 | if not m: |
|
114 | 115 | uctext = unicode(text[start:], encoding.encoding) |
|
115 | 116 | w = len(uctext) |
|
116 | 117 | while 0 < w and uctext[w - 1].isspace(): |
|
117 | 118 | w -= 1 |
|
118 | 119 | yield (uctext[:w].encode(encoding.encoding), |
|
119 | 120 | uctext[w:].encode(encoding.encoding)) |
|
120 | 121 | break |
|
121 | 122 | yield text[start:m.start(0)], m.group(1) |
|
122 | 123 | start = m.end(1) |
|
123 | 124 | |
|
124 | 125 | return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest |
|
125 | 126 | for para, rest in findparas()]) |
|
126 | 127 | |
|
127 | 128 | def fill68(text): |
|
128 | 129 | """:fill68: Any text. Wraps the text to fit in 68 columns.""" |
|
129 | 130 | return fill(text, 68) |
|
130 | 131 | |
|
131 | 132 | def fill76(text): |
|
132 | 133 | """:fill76: Any text. Wraps the text to fit in 76 columns.""" |
|
133 | 134 | return fill(text, 76) |
|
134 | 135 | |
|
135 | 136 | def firstline(text): |
|
136 | 137 | """:firstline: Any text. Returns the first line of text.""" |
|
137 | 138 | try: |
|
138 | 139 | return text.splitlines(True)[0].rstrip('\r\n') |
|
139 | 140 | except IndexError: |
|
140 | 141 | return '' |
|
141 | 142 | |
|
142 | 143 | def hexfilter(text): |
|
143 | 144 | """:hex: Any text. Convert a binary Mercurial node identifier into |
|
144 | 145 | its long hexadecimal representation. |
|
145 | 146 | """ |
|
146 | 147 | return node.hex(text) |
|
147 | 148 | |
|
148 | 149 | def hgdate(text): |
|
149 | 150 | """:hgdate: Date. Returns the date as a pair of numbers: "1157407993 |
|
150 | 151 | 25200" (Unix timestamp, timezone offset). |
|
151 | 152 | """ |
|
152 | 153 | return "%d %d" % text |
|
153 | 154 | |
|
154 | 155 | def isodate(text): |
|
155 | 156 | """:isodate: Date. Returns the date in ISO 8601 format: "2009-08-18 13:00 |
|
156 | 157 | +0200". |
|
157 | 158 | """ |
|
158 | 159 | return util.datestr(text, '%Y-%m-%d %H:%M %1%2') |
|
159 | 160 | |
|
160 | 161 | def isodatesec(text): |
|
161 | 162 | """:isodatesec: Date. Returns the date in ISO 8601 format, including |
|
162 | 163 | seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date |
|
163 | 164 | filter. |
|
164 | 165 | """ |
|
165 | 166 | return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2') |
|
166 | 167 | |
|
167 | 168 | def indent(text, prefix): |
|
168 | 169 | '''indent each non-empty line of text after first with prefix.''' |
|
169 | 170 | lines = text.splitlines() |
|
170 | 171 | num_lines = len(lines) |
|
171 | 172 | endswithnewline = text[-1:] == '\n' |
|
172 | 173 | def indenter(): |
|
173 | 174 | for i in xrange(num_lines): |
|
174 | 175 | l = lines[i] |
|
175 | 176 | if i and l.strip(): |
|
176 | 177 | yield prefix |
|
177 | 178 | yield l |
|
178 | 179 | if i < num_lines - 1 or endswithnewline: |
|
179 | 180 | yield '\n' |
|
180 | 181 | return "".join(indenter()) |
|
181 | 182 | |
|
182 | 183 | def json(obj): |
|
183 | 184 | if obj is None or obj is False or obj is True: |
|
184 | 185 | return {None: 'null', False: 'false', True: 'true'}[obj] |
|
185 | 186 | elif isinstance(obj, int) or isinstance(obj, float): |
|
186 | 187 | return str(obj) |
|
187 | 188 | elif isinstance(obj, str): |
|
188 | 189 | u = unicode(obj, encoding.encoding, 'replace') |
|
189 | 190 | return '"%s"' % jsonescape(u) |
|
190 | 191 | elif isinstance(obj, unicode): |
|
191 | 192 | return '"%s"' % jsonescape(obj) |
|
192 | 193 | elif util.safehasattr(obj, 'keys'): |
|
193 | 194 | out = [] |
|
194 | 195 | for k, v in obj.iteritems(): |
|
195 | 196 | s = '%s: %s' % (json(k), json(v)) |
|
196 | 197 | out.append(s) |
|
197 | 198 | return '{' + ', '.join(out) + '}' |
|
198 | 199 | elif util.safehasattr(obj, '__iter__'): |
|
199 | 200 | out = [] |
|
200 | 201 | for i in obj: |
|
201 | 202 | out.append(json(i)) |
|
202 | 203 | return '[' + ', '.join(out) + ']' |
|
203 | 204 | else: |
|
204 | 205 | raise TypeError('cannot encode type %s' % obj.__class__.__name__) |
|
205 | 206 | |
|
206 | 207 | def _uescape(c): |
|
207 | 208 | if ord(c) < 0x80: |
|
208 | 209 | return c |
|
209 | 210 | else: |
|
210 | 211 | return '\\u%04x' % ord(c) |
|
211 | 212 | |
|
212 | 213 | _escapes = [ |
|
213 | 214 | ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), |
|
214 | 215 | ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), |
|
215 | 216 | ] |
|
216 | 217 | |
|
217 | 218 | def jsonescape(s): |
|
218 | 219 | for k, v in _escapes: |
|
219 | 220 | s = s.replace(k, v) |
|
220 | 221 | return ''.join(_uescape(c) for c in s) |
|
221 | 222 | |
|
222 | 223 | def localdate(text): |
|
223 | 224 | """:localdate: Date. Converts a date to local date.""" |
|
224 | 225 | return (util.parsedate(text)[0], util.makedate()[1]) |
|
225 | 226 | |
|
226 | 227 | def nonempty(str): |
|
227 | 228 | """:nonempty: Any text. Returns '(none)' if the string is empty.""" |
|
228 | 229 | return str or "(none)" |
|
229 | 230 | |
|
230 | 231 | def obfuscate(text): |
|
231 | 232 | """:obfuscate: Any text. Returns the input text rendered as a sequence of |
|
232 | 233 | XML entities. |
|
233 | 234 | """ |
|
234 | 235 | text = unicode(text, encoding.encoding, 'replace') |
|
235 | 236 | return ''.join(['&#%d;' % ord(c) for c in text]) |
|
236 | 237 | |
|
237 | 238 | def permissions(flags): |
|
238 | 239 | if "l" in flags: |
|
239 | 240 | return "lrwxrwxrwx" |
|
240 | 241 | if "x" in flags: |
|
241 | 242 | return "-rwxr-xr-x" |
|
242 | 243 | return "-rw-r--r--" |
|
243 | 244 | |
|
244 | 245 | def person(author): |
|
245 | 246 | """:person: Any text. Returns the name before an email address, |
|
246 | 247 | interpreting it as per RFC 5322. |
|
247 | 248 | |
|
248 | 249 | >>> person('foo@bar') |
|
249 | 250 | 'foo' |
|
250 | 251 | >>> person('Foo Bar <foo@bar>') |
|
251 | 252 | 'Foo Bar' |
|
252 | 253 | >>> person('"Foo Bar" <foo@bar>') |
|
253 | 254 | 'Foo Bar' |
|
254 | 255 | >>> person('"Foo \"buz\" Bar" <foo@bar>') |
|
255 | 256 | 'Foo "buz" Bar' |
|
256 | 257 | >>> # The following are invalid, but do exist in real-life |
|
257 | 258 | ... |
|
258 | 259 | >>> person('Foo "buz" Bar <foo@bar>') |
|
259 | 260 | 'Foo "buz" Bar' |
|
260 | 261 | >>> person('"Foo Bar <foo@bar>') |
|
261 | 262 | 'Foo Bar' |
|
262 | 263 | """ |
|
263 | 264 | if '@' not in author: |
|
264 | 265 | return author |
|
265 | 266 | f = author.find('<') |
|
266 | 267 | if f != -1: |
|
267 | 268 | return author[:f].strip(' "').replace('\\"', '"') |
|
268 | 269 | f = author.find('@') |
|
269 | 270 | return author[:f].replace('.', ' ') |
|
270 | 271 | |
|
271 | 272 | def rfc3339date(text): |
|
272 | 273 | """:rfc3339date: Date. Returns a date using the Internet date format |
|
273 | 274 | specified in RFC 3339: "2009-08-18T13:00:13+02:00". |
|
274 | 275 | """ |
|
275 | 276 | return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2") |
|
276 | 277 | |
|
277 | 278 | def rfc822date(text): |
|
278 | 279 | """:rfc822date: Date. Returns a date using the same format used in email |
|
279 | 280 | headers: "Tue, 18 Aug 2009 13:00:13 +0200". |
|
280 | 281 | """ |
|
281 | 282 | return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2") |
|
282 | 283 | |
|
283 | 284 | def short(text): |
|
284 | 285 | """:short: Changeset hash. Returns the short form of a changeset hash, |
|
285 | 286 | i.e. a 12 hexadecimal digit string. |
|
286 | 287 | """ |
|
287 | 288 | return text[:12] |
|
288 | 289 | |
|
289 | 290 | def shortbisect(text): |
|
290 | 291 | """:shortbisect: Any text. Treats `text` as a bisection status, and |
|
291 | 292 | returns a single-character representing the status (G: good, B: bad, |
|
292 | 293 | S: skipped, U: untested, I: ignored). Returns single space if `text` |
|
293 | 294 | is not a valid bisection status. |
|
294 | 295 | """ |
|
295 | 296 | return hbisect.shortlabel(text) or ' ' |
|
296 | 297 | |
|
297 | 298 | def shortdate(text): |
|
298 | 299 | """:shortdate: Date. Returns a date like "2006-09-18".""" |
|
299 | 300 | return util.shortdate(text) |
|
300 | 301 | |
|
301 | 302 | def stringescape(text): |
|
302 | 303 | return text.encode('string_escape') |
|
303 | 304 | |
|
304 | 305 | def stringify(thing): |
|
305 | 306 | """:stringify: Any type. Turns the value into text by converting values into |
|
306 | 307 | text and concatenating them. |
|
307 | 308 | """ |
|
308 | 309 | if util.safehasattr(thing, '__iter__') and not isinstance(thing, str): |
|
309 | 310 | return "".join([stringify(t) for t in thing if t is not None]) |
|
310 | 311 | return str(thing) |
|
311 | 312 | |
|
312 | 313 | def strip(text): |
|
313 | 314 | """:strip: Any text. Strips all leading and trailing whitespace.""" |
|
314 | 315 | return text.strip() |
|
315 | 316 | |
|
316 | 317 | def stripdir(text): |
|
317 | 318 | """:stripdir: Treat the text as path and strip a directory level, if |
|
318 | 319 | possible. For example, "foo" and "foo/bar" becomes "foo". |
|
319 | 320 | """ |
|
320 | 321 | dir = os.path.dirname(text) |
|
321 | 322 | if dir == "": |
|
322 | 323 | return os.path.basename(text) |
|
323 | 324 | else: |
|
324 | 325 | return dir |
|
325 | 326 | |
|
326 | 327 | def tabindent(text): |
|
327 | 328 | """:tabindent: Any text. Returns the text, with every line except the |
|
328 | 329 | first starting with a tab character. |
|
329 | 330 | """ |
|
330 | 331 | return indent(text, '\t') |
|
331 | 332 | |
|
332 | 333 | def urlescape(text): |
|
333 | 334 | """:urlescape: Any text. Escapes all "special" characters. For example, |
|
334 | 335 | "foo bar" becomes "foo%20bar". |
|
335 | 336 | """ |
|
336 | 337 | return urllib.quote(text) |
|
337 | 338 | |
|
338 | 339 | def userfilter(text): |
|
339 | 340 | """:user: Any text. Returns a short representation of a user name or email |
|
340 | 341 | address.""" |
|
341 | 342 | return util.shortuser(text) |
|
342 | 343 | |
|
343 | 344 | def emailuser(text): |
|
344 | 345 | """:emailuser: Any text. Returns the user portion of an email address.""" |
|
345 | 346 | return util.emailuser(text) |
|
346 | 347 | |
|
347 | 348 | def xmlescape(text): |
|
348 | 349 | text = (text |
|
349 | 350 | .replace('&', '&') |
|
350 | 351 | .replace('<', '<') |
|
351 | 352 | .replace('>', '>') |
|
352 | 353 | .replace('"', '"') |
|
353 | 354 | .replace("'", ''')) # ' invalid in HTML |
|
354 | 355 | return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
|
355 | 356 | |
|
356 | 357 | filters = { |
|
357 | 358 | "addbreaks": addbreaks, |
|
358 | 359 | "age": age, |
|
359 | 360 | "basename": basename, |
|
360 | 361 | "date": datefilter, |
|
361 | 362 | "domain": domain, |
|
362 | 363 | "email": email, |
|
363 | 364 | "escape": escape, |
|
364 | 365 | "fill68": fill68, |
|
365 | 366 | "fill76": fill76, |
|
366 | 367 | "firstline": firstline, |
|
367 | 368 | "hex": hexfilter, |
|
368 | 369 | "hgdate": hgdate, |
|
369 | 370 | "isodate": isodate, |
|
370 | 371 | "isodatesec": isodatesec, |
|
371 | 372 | "json": json, |
|
372 | 373 | "jsonescape": jsonescape, |
|
373 | 374 | "localdate": localdate, |
|
374 | 375 | "nonempty": nonempty, |
|
375 | 376 | "obfuscate": obfuscate, |
|
376 | 377 | "permissions": permissions, |
|
377 | 378 | "person": person, |
|
378 | 379 | "rfc3339date": rfc3339date, |
|
379 | 380 | "rfc822date": rfc822date, |
|
380 | 381 | "short": short, |
|
381 | 382 | "shortbisect": shortbisect, |
|
382 | 383 | "shortdate": shortdate, |
|
383 | 384 | "stringescape": stringescape, |
|
384 | 385 | "stringify": stringify, |
|
385 | 386 | "strip": strip, |
|
386 | 387 | "stripdir": stripdir, |
|
387 | 388 | "tabindent": tabindent, |
|
388 | 389 | "urlescape": urlescape, |
|
389 | 390 | "user": userfilter, |
|
390 | 391 | "emailuser": emailuser, |
|
391 | 392 | "xmlescape": xmlescape, |
|
392 | 393 | } |
|
393 | 394 | |
|
394 | 395 | def fillfunc(context, mapping, args): |
|
395 | 396 | if not (1 <= len(args) <= 2): |
|
396 | 397 | raise error.ParseError(_("fill expects one or two arguments")) |
|
397 | 398 | |
|
398 | 399 | text = stringify(args[0][0](context, mapping, args[0][1])) |
|
399 | 400 | width = 76 |
|
400 | 401 | if len(args) == 2: |
|
401 | 402 | try: |
|
402 | 403 | width = int(stringify(args[1][0](context, mapping, args[1][1]))) |
|
403 | 404 | except ValueError: |
|
404 | 405 | raise error.ParseError(_("fill expects an integer width")) |
|
405 | 406 | |
|
406 | 407 | return fill(text, width) |
|
407 | 408 | |
|
408 | 409 | def datefunc(context, mapping, args): |
|
409 | 410 | if not (1 <= len(args) <= 2): |
|
410 | 411 | raise error.ParseError(_("date expects one or two arguments")) |
|
411 | 412 | |
|
412 | 413 | date = args[0][0](context, mapping, args[0][1]) |
|
413 | 414 | if len(args) == 2: |
|
414 | 415 | fmt = stringify(args[1][0](context, mapping, args[1][1])) |
|
415 | 416 | return util.datestr(date, fmt) |
|
416 | 417 | return util.datestr(date) |
|
417 | 418 | |
|
418 | 419 | funcs = { |
|
419 | 420 | "fill": fillfunc, |
|
420 | 421 | "date": datefunc, |
|
421 | 422 | } |
|
422 | 423 | |
|
423 | 424 | # tell hggettext to extract docstrings from these functions: |
|
424 | 425 | i18nfunctions = filters.values() |
General Comments 0
You need to be logged in to leave comments.
Login now