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