author | Alexander Solovyov <piranha@piranha.org.ua> |
Mon, 23 Mar 2009 13:15:57 +0100 | |
changeset 7878 | 8c09952cd39a |
parent 7682 | 9c8bbae02e9c |
child 7948 | de377b1a9a84 |
permissions | -rw-r--r-- |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 |
# template-filters.py - common template expansion filters |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 |
# |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 |
# Copyright 2005-2008 Matt Mackall <mpm@selenic.com> |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 |
# |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
8 |
import cgi, re, os, time, urllib, textwrap |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 |
import util, templater |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
10 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 |
agescales = [("second", 1), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 |
("minute", 60), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 |
("hour", 3600), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 |
("day", 3600 * 24), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 |
("week", 3600 * 24 * 7), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 |
("month", 3600 * 24 * 30), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 |
("year", 3600 * 24 * 365)] |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
18 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
19 |
agescales.reverse() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
20 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
21 |
def age(date): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
22 |
'''turn a (timestamp, tzoff) tuple into an age string.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
23 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 |
def plural(t, c): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 |
if c == 1: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
26 |
return t |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
27 |
return t + "s" |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 |
def fmt(t, c): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 |
return "%d %s" % (c, plural(t, c)) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
31 |
now = time.time() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
32 |
then = date[0] |
7682
9c8bbae02e9c
templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6691
diff
changeset
|
33 |
if then > now: |
9c8bbae02e9c
templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6691
diff
changeset
|
34 |
return 'in the future' |
9c8bbae02e9c
templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6691
diff
changeset
|
35 |
|
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
36 |
delta = max(1, int(now - then)) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 |
for t, s in agescales: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 |
n = delta / s |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 |
if n >= 2 or s == 1: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 |
return fmt(t, n) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 |
para_re = None |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 |
space_re = None |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
44 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 |
def fill(text, width): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 |
'''fill many paragraphs.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 |
global para_re, space_re |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 |
if para_re is None: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 |
para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 |
space_re = re.compile(r' +') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 |
def findparas(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 |
start = 0 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
while True: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 |
m = para_re.search(text, start) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 |
if not m: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
57 |
w = len(text) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
58 |
while w > start and text[w-1].isspace(): w -= 1 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
59 |
yield text[start:w], text[w:] |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
60 |
break |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 |
yield text[start:m.start(0)], m.group(1) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 |
start = m.end(1) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
64 |
return "".join([space_re.sub(' ', textwrap.fill(para, width)) + rest |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
65 |
for para, rest in findparas()]) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
66 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 |
def firstline(text): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 |
'''return the first line of text''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
try: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 |
return text.splitlines(1)[0].rstrip('\r\n') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 |
except IndexError: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 |
return '' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
74 |
def nl2br(text): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
75 |
'''replace raw newlines with xhtml line breaks.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
76 |
return text.replace('\n', '<br/>\n') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
77 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
78 |
def obfuscate(text): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
79 |
text = unicode(text, util._encoding, 'replace') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
80 |
return ''.join(['&#%d;' % ord(c) for c in text]) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
81 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 |
def domain(author): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
83 |
'''get domain of author, or empty string if none.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 |
f = author.find('@') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 |
if f == -1: return '' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
86 |
author = author[f+1:] |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
87 |
f = author.find('>') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 |
if f >= 0: author = author[:f] |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 |
return author |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
90 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
91 |
def person(author): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
92 |
'''get name of author, or else username.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 |
f = author.find('<') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
94 |
if f == -1: return util.shortuser(author) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
95 |
return author[:f].rstrip() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
96 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
97 |
def indent(text, prefix): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
98 |
'''indent each non-empty line of text after first with prefix.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
99 |
lines = text.splitlines() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
100 |
num_lines = len(lines) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
101 |
def indenter(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
102 |
for i in xrange(num_lines): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
103 |
l = lines[i] |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
104 |
if i and l.strip(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
105 |
yield prefix |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
106 |
yield l |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
107 |
if i < num_lines - 1 or text.endswith('\n'): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
108 |
yield '\n' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
109 |
return "".join(indenter()) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
110 |
|
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
111 |
def permissions(flags): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
112 |
if "l" in flags: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
113 |
return "lrwxrwxrwx" |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
114 |
if "x" in flags: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
115 |
return "-rwxr-xr-x" |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
116 |
return "-rw-r--r--" |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
117 |
|
6174
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
118 |
def xmlescape(text): |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
119 |
text = (text |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
120 |
.replace('&', '&') |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
121 |
.replace('<', '<') |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
122 |
.replace('>', '>') |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
123 |
.replace('"', '"') |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
124 |
.replace("'", ''')) # ' invalid in HTML |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
125 |
return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
126 |
|
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
127 |
_escapes = [ |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
128 |
('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
129 |
('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
130 |
] |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
131 |
|
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
132 |
def json(obj): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
133 |
if obj is None or obj is False or obj is True: |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
134 |
return {None: 'null', False: 'false', True: 'true'}[obj] |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
135 |
elif isinstance(obj, int) or isinstance(obj, float): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
136 |
return str(obj) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
137 |
elif isinstance(obj, str): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
138 |
for k, v in _escapes: |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
139 |
obj = obj.replace(k, v) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
140 |
return '"%s"' % obj |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
141 |
elif isinstance(obj, unicode): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
142 |
return json(obj.encode('utf-8')) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
143 |
elif hasattr(obj, 'keys'): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
144 |
out = [] |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
145 |
for k, v in obj.iteritems(): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
146 |
s = '%s: %s' % (json(k), json(v)) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
147 |
out.append(s) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
148 |
return '{' + ', '.join(out) + '}' |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
149 |
elif hasattr(obj, '__iter__'): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
150 |
out = [] |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
151 |
for i in obj: |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
152 |
out.append(json(i)) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
153 |
return '[' + ', '.join(out) + ']' |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
154 |
else: |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
155 |
raise TypeError('cannot encode type %s' % obj.__class__.__name__) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
156 |
|
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
157 |
filters = { |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
158 |
"addbreaks": nl2br, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
159 |
"basename": os.path.basename, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
160 |
"age": age, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
161 |
"date": lambda x: util.datestr(x), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
162 |
"domain": domain, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
163 |
"email": util.email, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
164 |
"escape": lambda x: cgi.escape(x, True), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
165 |
"fill68": lambda x: fill(x, width=68), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
166 |
"fill76": lambda x: fill(x, width=76), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
167 |
"firstline": firstline, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
168 |
"tabindent": lambda x: indent(x, '\t'), |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6174
diff
changeset
|
169 |
"hgdate": lambda x: "%d %d" % x, |
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6174
diff
changeset
|
170 |
"isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'), |
6319
8999d1249171
Add an {isodatesec} template, to show seconds too.
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
6229
diff
changeset
|
171 |
"isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'), |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
172 |
"obfuscate": obfuscate, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
173 |
"permissions": permissions, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
174 |
"person": person, |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6174
diff
changeset
|
175 |
"rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"), |
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6174
diff
changeset
|
176 |
"rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"), |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
177 |
"short": lambda x: x[:12], |
6134
7b937b26adf7
Make annotae/grep print short dates with -q/--quiet.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5976
diff
changeset
|
178 |
"shortdate": util.shortdate, |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
179 |
"stringify": templater.stringify, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
180 |
"strip": lambda x: x.strip(), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
181 |
"urlescape": lambda x: urllib.quote(x), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
182 |
"user": lambda x: util.shortuser(x), |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
183 |
"stringescape": lambda x: x.encode('string_escape'), |
6174
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
184 |
"xmlescape": xmlescape, |
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
185 |
"json": json, |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
186 |
} |