annotate mercurial/templatefilters.py @ 13588:b8b881f3f3a7

templatefilters: sort function definitions
author Patrick Mezard <pmezard@gmail.com>
date Sat, 12 Mar 2011 12:46:31 +0100
parents 9fb6850d5d97
children b0a4b05c25e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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 #
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8158
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9722
diff changeset
6 # GNU General Public License version 2 or any later version.
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
7
11297
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
8 import cgi, re, os, time, urllib
12371
48a4acd1ccf1 templater: add hex filter.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11891
diff changeset
9 import encoding, node, util
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
10
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
11 def addbreaks(text):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
12 '''replace raw newlines with xhtml line breaks.'''
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
13 return text.replace('\n', '<br/>\n')
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8234
diff changeset
14
10601
b98b6a7363ae templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
15 agescales = [("year", 3600 * 24 * 365),
b98b6a7363ae templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
16 ("month", 3600 * 24 * 30),
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
17 ("week", 3600 * 24 * 7),
10601
b98b6a7363ae templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
18 ("day", 3600 * 24),
b98b6a7363ae templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
19 ("hour", 3600),
b98b6a7363ae templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
20 ("minute", 60),
10787
5974123d0339 templatefilters: fix check-code warning
Matt Mackall <mpm@selenic.com>
parents: 10601
diff changeset
21 ("second", 1)]
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
22
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
23 def age(date):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
24 '''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
25
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
26 def plural(t, c):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
27 if c == 1:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
28 return t
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
29 return t + "s"
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
30 def fmt(t, c):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
31 return "%d %s" % (c, plural(t, c))
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
32
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
33 now = time.time()
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
34 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
35 if then > now:
9c8bbae02e9c templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6691
diff changeset
36 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
37
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
38 delta = max(1, int(now - then))
9722
4d9dea174b84 templater: readable dates older than 24 months revert to ISO8601 (issue1006)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9721
diff changeset
39 if delta > agescales[0][1] * 2:
4d9dea174b84 templater: readable dates older than 24 months revert to ISO8601 (issue1006)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9721
diff changeset
40 return util.shortdate(date)
4d9dea174b84 templater: readable dates older than 24 months revert to ISO8601 (issue1006)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9721
diff changeset
41
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
42 for t, s in agescales:
9029
0001e49f1c11 compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents: 8697
diff changeset
43 n = delta // s
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
44 if n >= 2 or s == 1:
9721
1d75c683ada1 templater: put 'ago' inside the age template filter
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9387
diff changeset
45 return '%s ago' % fmt(t, n)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
46
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
47 def domain(author):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
48 '''get domain of author, or empty string if none.'''
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
49 f = author.find('@')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
50 if f == -1:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
51 return ''
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
52 author = author[f + 1:]
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
53 f = author.find('>')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
54 if f >= 0:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
55 author = author[:f]
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
56 return author
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
57
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
58 para_re = None
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
59 space_re = None
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
60
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
61 def fill(text, width):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
62 '''fill many paragraphs.'''
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
63 global para_re, space_re
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
64 if para_re is None:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
65 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
66 space_re = re.compile(r' +')
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
67
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
68 def findparas():
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
69 start = 0
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
70 while True:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
71 m = para_re.search(text, start)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
72 if not m:
11297
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
73 uctext = unicode(text[start:], encoding.encoding)
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
74 w = len(uctext)
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
75 while 0 < w and uctext[w - 1].isspace():
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
76 w -= 1
11297
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
77 yield (uctext[:w].encode(encoding.encoding),
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
78 uctext[w:].encode(encoding.encoding))
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
79 break
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
80 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
81 start = m.end(1)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
82
11297
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
83 return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
84 for para, rest in findparas()])
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
85
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
86 def firstline(text):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
87 '''return the first line of text'''
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
88 try:
9136
31177742f54a for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9029
diff changeset
89 return text.splitlines(True)[0].rstrip('\r\n')
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
90 except IndexError:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
91 return ''
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
92
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
93 def indent(text, prefix):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
94 '''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
95 lines = text.splitlines()
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
96 num_lines = len(lines)
9387
20ed9909dbd9 templatefilters: indent: do not compute text.endswith('\n') in each iteration
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9136
diff changeset
97 endswithnewline = text[-1:] == '\n'
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
98 def indenter():
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
99 for i in xrange(num_lines):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
100 l = lines[i]
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
101 if i and l.strip():
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
102 yield prefix
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
103 yield l
9387
20ed9909dbd9 templatefilters: indent: do not compute text.endswith('\n') in each iteration
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9136
diff changeset
104 if i < num_lines - 1 or endswithnewline:
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
105 yield '\n'
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
106 return "".join(indenter())
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
107
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
108 def json(obj):
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
109 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
110 return {None: 'null', False: 'false', True: 'true'}[obj]
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
111 elif isinstance(obj, int) or isinstance(obj, float):
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
112 return str(obj)
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
113 elif isinstance(obj, str):
11765
aff419e260f9 templatefilters: make json filter handle multibyte characters correctly
Yuya Nishihara <yuya@tcha.org>
parents: 11297
diff changeset
114 u = unicode(obj, encoding.encoding, 'replace')
11890
9dac951d0185 templatefilters: use \uxxxx style escape for JSON string
Yuya Nishihara <yuya@tcha.org>
parents: 11765
diff changeset
115 return '"%s"' % jsonescape(u)
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
116 elif isinstance(obj, unicode):
11890
9dac951d0185 templatefilters: use \uxxxx style escape for JSON string
Yuya Nishihara <yuya@tcha.org>
parents: 11765
diff changeset
117 return '"%s"' % jsonescape(obj)
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
118 elif hasattr(obj, 'keys'):
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
119 out = []
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
120 for k, v in obj.iteritems():
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
121 s = '%s: %s' % (json(k), json(v))
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
122 out.append(s)
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
123 return '{' + ', '.join(out) + '}'
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
124 elif hasattr(obj, '__iter__'):
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
125 out = []
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
126 for i in obj:
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
127 out.append(json(i))
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
128 return '[' + ', '.join(out) + ']'
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
129 else:
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
130 raise TypeError('cannot encode type %s' % obj.__class__.__name__)
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
131
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
132 def uescape(c):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
133 if ord(c) < 0x80:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
134 return c
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
135 else:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
136 return '\\u%04x' % ord(c)
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
137
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
138 _escapes = [
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
139 ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'),
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
140 ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'),
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
141 ]
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
142
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
143 def jsonescape(s):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
144 for k, v in _escapes:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
145 s = s.replace(k, v)
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
146 return ''.join(uescape(c) for c in s)
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
147
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
148 def nonempty(str):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
149 return str or "(none)"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
150
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
151 def obfuscate(text):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
152 text = unicode(text, encoding.encoding, 'replace')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
153 return ''.join(['&#%d;' % ord(c) for c in text])
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
154
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
155 def permissions(flags):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
156 if "l" in flags:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
157 return "lrwxrwxrwx"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
158 if "x" in flags:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
159 return "-rwxr-xr-x"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
160 return "-rw-r--r--"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
161
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
162 def person(author):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
163 '''get name of author, or else username.'''
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
164 if not '@' in author:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
165 return author
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
166 f = author.find('<')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
167 if f == -1:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
168 return util.shortuser(author)
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
169 return author[:f].rstrip()
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
170
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
171 def stringify(thing):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
172 '''turn nested template iterator into string.'''
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
173 if hasattr(thing, '__iter__') and not isinstance(thing, str):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
174 return "".join([stringify(t) for t in thing if t is not None])
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
175 return str(thing)
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
176
8158
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
177 def stripdir(text):
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
178 '''Treat the text as path and strip a directory level, if possible.'''
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
179 dir = os.path.dirname(text)
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
180 if dir == "":
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
181 return os.path.basename(text)
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
182 else:
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
183 return dir
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
184
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
185 def xmlescape(text):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
186 text = (text
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
187 .replace('&', '&amp;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
188 .replace('<', '&lt;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
189 .replace('>', '&gt;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
190 .replace('"', '&quot;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
191 .replace("'", '&#39;')) # &apos; invalid in HTML
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
192 return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text)
8234
27dbe534397b templatefilters: add "nonempty" template filter
Rocco Rutte <pdmef@gmx.net>
parents: 8225
diff changeset
193
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
194 filters = {
13587
9fb6850d5d97 templatefilters: match filter keys and function names
Patrick Mezard <pmezard@gmail.com>
parents: 13586
diff changeset
195 "addbreaks": addbreaks,
13586
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
196 "age": age,
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
197 "basename": os.path.basename,
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
198 "date": lambda x: util.datestr(x),
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
199 "domain": domain,
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
200 "email": util.email,
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
201 "escape": lambda x: cgi.escape(x, True),
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
202 "fill68": lambda x: fill(x, width=68),
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
203 "fill76": lambda x: fill(x, width=76),
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
204 "firstline": firstline,
13586
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
205 "hex": node.hex,
6229
c3182eeb70ea dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents: 6174
diff changeset
206 "hgdate": lambda x: "%d %d" % x,
c3182eeb70ea dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents: 6174
diff changeset
207 "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
208 "isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'),
8014
6a77ba181bc6 templatefilters: split out jsonescape() function
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
209 "json": json,
6a77ba181bc6 templatefilters: split out jsonescape() function
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
210 "jsonescape": jsonescape,
8591
08c93b07f5ad templatefilters: add filter to convert date to local date (issue1674)
Henrik Stuart <hg@hstuart.dk>
parents: 8390
diff changeset
211 "localdate": lambda x: (x[0], util.makedate()[1]),
8234
27dbe534397b templatefilters: add "nonempty" template filter
Rocco Rutte <pdmef@gmx.net>
parents: 8225
diff changeset
212 "nonempty": nonempty,
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
213 "obfuscate": obfuscate,
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
214 "permissions": permissions,
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
215 "person": person,
13586
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
216 "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"),
6229
c3182eeb70ea dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents: 6174
diff changeset
217 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"),
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
218 "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
219 "shortdate": util.shortdate,
13586
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
220 "stringescape": lambda x: x.encode('string_escape'),
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8234
diff changeset
221 "stringify": stringify,
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
222 "strip": lambda x: x.strip(),
13586
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
223 "stripdir": stripdir,
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
224 "tabindent": lambda x: indent(x, '\t'),
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
225 "urlescape": lambda x: urllib.quote(x),
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
226 "user": lambda x: util.shortuser(x),
6174
434139080ed4 Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents: 6134
diff changeset
227 "xmlescape": xmlescape,
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
228 }