Mercurial > hg
annotate mercurial/templatefilters.py @ 35954:386c1e45e671
logcmdutil: drop default arguments from changesetdisplayer/templater() calls
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 21 Jan 2018 14:28:03 +0900 |
parents | 6d65cef5b038 |
children | 9ee10b3284da |
rev | line source |
---|---|
32740
ae0ebe93ac70
templatefilers: correct filename in header comment
Yuya Nishihara <yuya@tcha.org>
parents:
32128
diff
changeset
|
1 # templatefilters.py - common template expansion filters |
5976
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 | 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 |
25983
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
8 from __future__ import absolute_import |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
9 |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
10 import os |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
11 import re |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
12 import time |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
13 |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
14 from . import ( |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
15 encoding, |
34837
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
16 error, |
25983
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
17 hbisect, |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
18 node, |
32126
e37fd5be0fed
py3: alias long to int on Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32039
diff
changeset
|
19 pycompat, |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
20 registrar, |
25983
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
21 templatekw, |
34695
e178fcaa3933
python3: use our bytes-only version of cgi.escape everywhere
Augie Fackler <augie@google.com>
parents:
34131
diff
changeset
|
22 url, |
25983
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
23 util, |
1245049da5f3
templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25778
diff
changeset
|
24 ) |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28693
diff
changeset
|
26 urlerr = util.urlerr |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28693
diff
changeset
|
27 urlreq = util.urlreq |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28693
diff
changeset
|
28 |
32126
e37fd5be0fed
py3: alias long to int on Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32039
diff
changeset
|
29 if pycompat.ispy3: |
e37fd5be0fed
py3: alias long to int on Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32039
diff
changeset
|
30 long = int |
e37fd5be0fed
py3: alias long to int on Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32039
diff
changeset
|
31 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
32 # filters are callables like: |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
33 # fn(obj) |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
34 # with: |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
35 # obj - object to be filtered (text, date, list and so on) |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
36 filters = {} |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
37 |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
38 templatefilter = registrar.templatefilter(filters) |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
39 |
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
40 @templatefilter('addbreaks') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
41 def addbreaks(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
42 """Any text. Add an XHTML "<br />" tag before the end of |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
43 every line except the last. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
44 """ |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
45 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
|
46 |
19736
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
47 agescales = [("year", 3600 * 24 * 365, 'Y'), |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
48 ("month", 3600 * 24 * 30, 'M'), |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
49 ("week", 3600 * 24 * 7, 'W'), |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
50 ("day", 3600 * 24, 'd'), |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
51 ("hour", 3600, 'h'), |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
52 ("minute", 60, 'm'), |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
53 ("second", 1, 's')] |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
55 @templatefilter('age') |
19736
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
56 def age(date, abbrev=False): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
57 """Date. Returns a human-readable date/time difference between the |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
58 given date/time and the current date/time. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
59 """ |
5976
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 plural(t, c): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 if c == 1: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 return t |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
64 return t + "s" |
19736
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
65 def fmt(t, c, a): |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
66 if abbrev: |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
67 return "%d%s" % (c, a) |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 return "%d %s" % (c, plural(t, c)) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 now = time.time() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 then = date[0] |
13666
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
72 future = False |
7682
9c8bbae02e9c
templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6691
diff
changeset
|
73 if then > now: |
13666
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
74 future = True |
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
75 delta = max(1, int(then - now)) |
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
76 if delta > agescales[0][1] * 30: |
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
77 return 'in the distant future' |
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
78 else: |
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
79 delta = max(1, int(now - then)) |
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
80 if delta > agescales[0][1] * 2: |
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
81 return util.shortdate(date) |
9722
4d9dea174b84
templater: readable dates older than 24 months revert to ISO8601 (issue1006)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9721
diff
changeset
|
82 |
19736
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
83 for t, s, a in agescales: |
9029
0001e49f1c11
compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents:
8697
diff
changeset
|
84 n = delta // s |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 if n >= 2 or s == 1: |
13666
c49cddce0a81
templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents:
13593
diff
changeset
|
86 if future: |
19736
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
87 return '%s from now' % fmt(t, n, a) |
f08e542ce918
templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents:
19467
diff
changeset
|
88 return '%s ago' % fmt(t, n, a) |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
90 @templatefilter('basename') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
91 def basename(path): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
92 """Any text. Treats the text as a path, and returns the last |
35539
d1aae6d4efc5
templatefilters: fix doc of basename()
Yuya Nishihara <yuya@tcha.org>
parents:
34837
diff
changeset
|
93 component of the path after splitting by the path separator. |
d1aae6d4efc5
templatefilters: fix doc of basename()
Yuya Nishihara <yuya@tcha.org>
parents:
34837
diff
changeset
|
94 For example, "foo/bar/baz" becomes "baz" and "foo/bar//" becomes "". |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
95 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
96 return os.path.basename(path) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
97 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
98 @templatefilter('count') |
22668
13e3f07d74a3
templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents:
21873
diff
changeset
|
99 def count(i): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
100 """List or text. Returns the length as an integer.""" |
22668
13e3f07d74a3
templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents:
21873
diff
changeset
|
101 return len(i) |
13e3f07d74a3
templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents:
21873
diff
changeset
|
102 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
103 @templatefilter('domain') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
104 def domain(author): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
105 """Any text. Finds the first string that looks like an email |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
106 address, and extracts just the domain component. Example: ``User |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
107 <user@example.com>`` becomes ``example.com``. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
108 """ |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
109 f = author.find('@') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
110 if f == -1: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
111 return '' |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
112 author = author[f + 1:] |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
113 f = author.find('>') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
114 if f >= 0: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
115 author = author[:f] |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
116 return author |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
117 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
118 @templatefilter('email') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
119 def email(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
120 """Any text. Extracts the first string that looks like an email |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
121 address. Example: ``User <user@example.com>`` becomes |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
122 ``user@example.com``. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
123 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
124 return util.email(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
125 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
126 @templatefilter('escape') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
127 def escape(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
128 """Any text. Replaces the special XML/XHTML characters "&", "<" |
17772
823a7d79ef82
hgweb: make the escape filter remove null characters (issue2567)
Siddharth Agarwal <sid0@fb.com>
parents:
17755
diff
changeset
|
129 and ">" with XML entities, and filters out NUL characters. |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
130 """ |
34695
e178fcaa3933
python3: use our bytes-only version of cgi.escape everywhere
Augie Fackler <augie@google.com>
parents:
34131
diff
changeset
|
131 return url.escape(text.replace('\0', ''), True) |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
132 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
133 para_re = None |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
134 space_re = None |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
135 |
19872
681f7b9213a4
check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents:
19736
diff
changeset
|
136 def fill(text, width, initindent='', hangindent=''): |
19228
889807c79384
templater: add indentation arguments to the fill function
Sean Farley <sean.michael.farley@gmail.com>
parents:
19227
diff
changeset
|
137 '''fill many paragraphs with optional indentation.''' |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
138 global para_re, space_re |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
139 if para_re is None: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
140 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
|
141 space_re = re.compile(r' +') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
142 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
143 def findparas(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
144 start = 0 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
145 while True: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
146 m = para_re.search(text, start) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 uctext[w:].encode(encoding.encoding)) |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
154 break |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
155 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
|
156 start = m.end(1) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
157 |
19228
889807c79384
templater: add indentation arguments to the fill function
Sean Farley <sean.michael.farley@gmail.com>
parents:
19227
diff
changeset
|
158 return "".join([util.wrap(space_re.sub(' ', util.wrap(para, width)), |
889807c79384
templater: add indentation arguments to the fill function
Sean Farley <sean.michael.farley@gmail.com>
parents:
19227
diff
changeset
|
159 width, initindent, hangindent) + rest |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
160 for para, rest in findparas()]) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
161 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
162 @templatefilter('fill68') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
163 def fill68(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
164 """Any text. Wraps the text to fit in 68 columns.""" |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
165 return fill(text, 68) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
166 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
167 @templatefilter('fill76') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
168 def fill76(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
169 """Any text. Wraps the text to fit in 76 columns.""" |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
170 return fill(text, 76) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
171 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
172 @templatefilter('firstline') |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
173 def firstline(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
174 """Any text. Returns the first line of text.""" |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
175 try: |
9136
31177742f54a
for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9029
diff
changeset
|
176 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
|
177 except IndexError: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
178 return '' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
179 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
180 @templatefilter('hex') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
181 def hexfilter(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
182 """Any text. Convert a binary Mercurial node identifier into |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
183 its long hexadecimal representation. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
184 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
185 return node.hex(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
186 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
187 @templatefilter('hgdate') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
188 def hgdate(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
189 """Date. Returns the date as a pair of numbers: "1157407993 |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
190 25200" (Unix timestamp, timezone offset). |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
191 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
192 return "%d %d" % text |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
193 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
194 @templatefilter('isodate') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
195 def isodate(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
196 """Date. Returns the date in ISO 8601 format: "2009-08-18 13:00 |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
197 +0200". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
198 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
199 return util.datestr(text, '%Y-%m-%d %H:%M %1%2') |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
200 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
201 @templatefilter('isodatesec') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
202 def isodatesec(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
203 """Date. Returns the date in ISO 8601 format, including |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
204 seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
205 filter. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
206 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
207 return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2') |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
208 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
209 def indent(text, prefix): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
210 '''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
|
211 lines = text.splitlines() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
212 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
|
213 endswithnewline = text[-1:] == '\n' |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
214 def indenter(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
215 for i in xrange(num_lines): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
216 l = lines[i] |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
217 if i and l.strip(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
218 yield prefix |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
219 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
|
220 if i < num_lines - 1 or endswithnewline: |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
221 yield '\n' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
222 return "".join(indenter()) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
223 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
224 @templatefilter('json') |
31782
654e9a1c8a6c
formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents:
31781
diff
changeset
|
225 def json(obj, paranoid=True): |
31780
8d9eafe01111
templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents:
31779
diff
changeset
|
226 if obj is None: |
8d9eafe01111
templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents:
31779
diff
changeset
|
227 return 'null' |
8d9eafe01111
templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents:
31779
diff
changeset
|
228 elif obj is False: |
8d9eafe01111
templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents:
31779
diff
changeset
|
229 return 'false' |
8d9eafe01111
templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents:
31779
diff
changeset
|
230 elif obj is True: |
8d9eafe01111
templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents:
31779
diff
changeset
|
231 return 'true' |
31728
35eb8f112c88
templatefilter: add support for 'long' to json()
Matt Harbison <matt_harbison@yahoo.com>
parents:
31451
diff
changeset
|
232 elif isinstance(obj, (int, long, float)): |
32127
964e7427a691
py3: use pycompat.bytestr() instead of str()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32126
diff
changeset
|
233 return pycompat.bytestr(obj) |
32128
c3342c177211
py3: replace str with bytes in isinstance()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32127
diff
changeset
|
234 elif isinstance(obj, bytes): |
31782
654e9a1c8a6c
formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents:
31781
diff
changeset
|
235 return '"%s"' % encoding.jsonescape(obj, paranoid=paranoid) |
34837
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
236 elif isinstance(obj, str): |
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
237 # This branch is unreachable on Python 2, because bytes == str |
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
238 # and we'll return in the next-earlier block in the elif |
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
239 # ladder. On Python 3, this helps us catch bugs before they |
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
240 # hurt someone. |
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
241 raise error.ProgrammingError( |
4fdc4adbc838
templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents:
34695
diff
changeset
|
242 'Mercurial only does output with bytes on Python 3: %r' % obj) |
14967
376091a4ad23
templatefilters: use safehasattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14944
diff
changeset
|
243 elif util.safehasattr(obj, 'keys'): |
32743
f924dd043974
json: pass formatting options recursively
Yuya Nishihara <yuya@tcha.org>
parents:
32742
diff
changeset
|
244 out = ['"%s": %s' % (encoding.jsonescape(k, paranoid=paranoid), |
f924dd043974
json: pass formatting options recursively
Yuya Nishihara <yuya@tcha.org>
parents:
32742
diff
changeset
|
245 json(v, paranoid)) |
31781
47925b63be70
templatefilters: use list comprehension in json()
Yuya Nishihara <yuya@tcha.org>
parents:
31780
diff
changeset
|
246 for k, v in sorted(obj.iteritems())] |
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
247 return '{' + ', '.join(out) + '}' |
14944
e2c413bde8a5
globally: use safehasattr(x, '__iter__') instead of hasattr(x, '__iter__')
Augie Fackler <durin42@gmail.com>
parents:
14318
diff
changeset
|
248 elif util.safehasattr(obj, '__iter__'): |
32743
f924dd043974
json: pass formatting options recursively
Yuya Nishihara <yuya@tcha.org>
parents:
32742
diff
changeset
|
249 out = [json(i, paranoid) for i in obj] |
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
250 return '[' + ', '.join(out) + ']' |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
251 else: |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
252 raise TypeError('cannot encode type %s' % obj.__class__.__name__) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
253 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
254 @templatefilter('lower') |
24566
6abce80e6cbf
templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents:
23708
diff
changeset
|
255 def lower(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
256 """Any text. Converts the text to lowercase.""" |
24566
6abce80e6cbf
templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents:
23708
diff
changeset
|
257 return encoding.lower(text) |
6abce80e6cbf
templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents:
23708
diff
changeset
|
258 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
259 @templatefilter('nonempty') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
260 def nonempty(str): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
261 """Any text. Returns '(none)' if the string is empty.""" |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
262 return str or "(none)" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
263 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
264 @templatefilter('obfuscate') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
265 def obfuscate(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
266 """Any text. Returns the input text rendered as a sequence of |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
267 XML entities. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
268 """ |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
269 text = unicode(text, encoding.encoding, 'replace') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
270 return ''.join(['&#%d;' % ord(c) for c in text]) |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
271 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
272 @templatefilter('permissions') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
273 def permissions(flags): |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
274 if "l" in flags: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
275 return "lrwxrwxrwx" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
276 if "x" in flags: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
277 return "-rwxr-xr-x" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
278 return "-rw-r--r--" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
279 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
280 @templatefilter('person') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
281 def person(author): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
282 """Any text. Returns the name before an email address, |
16235
eb39bbda167b
templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
15155
diff
changeset
|
283 interpreting it as per RFC 5322. |
16251
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
284 |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
32743
diff
changeset
|
285 >>> person(b'foo@bar') |
16251
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
286 'foo' |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
32743
diff
changeset
|
287 >>> person(b'Foo Bar <foo@bar>') |
16251
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
288 'Foo Bar' |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
32743
diff
changeset
|
289 >>> person(b'"Foo Bar" <foo@bar>') |
16251
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
290 'Foo Bar' |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
32743
diff
changeset
|
291 >>> person(b'"Foo \"buz\" Bar" <foo@bar>') |
16251
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
292 'Foo "buz" Bar' |
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
293 >>> # The following are invalid, but do exist in real-life |
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
294 ... |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
32743
diff
changeset
|
295 >>> person(b'Foo "buz" Bar <foo@bar>') |
16251
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
296 'Foo "buz" Bar' |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
32743
diff
changeset
|
297 >>> person(b'"Foo Bar <foo@bar>') |
16251
db68ee3289b6
templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
16235
diff
changeset
|
298 'Foo Bar' |
16235
eb39bbda167b
templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
15155
diff
changeset
|
299 """ |
16686
67964cda8701
cleanup: "not x in y" -> "x not in y"
Brodie Rao <brodie@sf.io>
parents:
16360
diff
changeset
|
300 if '@' not in author: |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
301 return author |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
302 f = author.find('<') |
13951
7a6a8a069aac
templatefilters: improve person() for john.doe@example.com
Adrian Buehlmann <adrian@cadifra.com>
parents:
13666
diff
changeset
|
303 if f != -1: |
16235
eb39bbda167b
templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents:
15155
diff
changeset
|
304 return author[:f].strip(' "').replace('\\"', '"') |
13951
7a6a8a069aac
templatefilters: improve person() for john.doe@example.com
Adrian Buehlmann <adrian@cadifra.com>
parents:
13666
diff
changeset
|
305 f = author.find('@') |
7a6a8a069aac
templatefilters: improve person() for john.doe@example.com
Adrian Buehlmann <adrian@cadifra.com>
parents:
13666
diff
changeset
|
306 return author[:f].replace('.', ' ') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
307 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
308 @templatefilter('revescape') |
25778
3a33412792f1
templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents:
25000
diff
changeset
|
309 def revescape(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
310 """Any text. Escapes all "special" characters, except @. |
25778
3a33412792f1
templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents:
25000
diff
changeset
|
311 Forward slashes are escaped twice to prevent web servers from prematurely |
3a33412792f1
templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents:
25000
diff
changeset
|
312 unescaping them. For example, "@foo bar/baz" becomes "@foo%20bar%252Fbaz". |
3a33412792f1
templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents:
25000
diff
changeset
|
313 """ |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28693
diff
changeset
|
314 return urlreq.quote(text, safe='/@').replace('/', '%252F') |
25778
3a33412792f1
templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents:
25000
diff
changeset
|
315 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
316 @templatefilter('rfc3339date') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
317 def rfc3339date(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
318 """Date. Returns a date using the Internet date format |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
319 specified in RFC 3339: "2009-08-18T13:00:13+02:00". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
320 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
321 return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2") |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
322 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
323 @templatefilter('rfc822date') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
324 def rfc822date(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
325 """Date. Returns a date using the same format used in email |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
326 headers: "Tue, 18 Aug 2009 13:00:13 +0200". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
327 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
328 return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2") |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
329 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
330 @templatefilter('short') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
331 def short(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
332 """Changeset hash. Returns the short form of a changeset hash, |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
333 i.e. a 12 hexadecimal digit string. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
334 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
335 return text[:12] |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
336 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
337 @templatefilter('shortbisect') |
15155
f4a8d754cd0a
templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents:
14967
diff
changeset
|
338 def shortbisect(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
339 """Any text. Treats `text` as a bisection status, and |
15155
f4a8d754cd0a
templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents:
14967
diff
changeset
|
340 returns a single-character representing the status (G: good, B: bad, |
f4a8d754cd0a
templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents:
14967
diff
changeset
|
341 S: skipped, U: untested, I: ignored). Returns single space if `text` |
f4a8d754cd0a
templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents:
14967
diff
changeset
|
342 is not a valid bisection status. |
f4a8d754cd0a
templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents:
14967
diff
changeset
|
343 """ |
f4a8d754cd0a
templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents:
14967
diff
changeset
|
344 return hbisect.shortlabel(text) or ' ' |
f4a8d754cd0a
templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents:
14967
diff
changeset
|
345 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
346 @templatefilter('shortdate') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
347 def shortdate(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
348 """Date. Returns a date like "2006-09-18".""" |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
349 return util.shortdate(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
350 |
35444
dad8a5071b0a
templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents:
34837
diff
changeset
|
351 @templatefilter('slashpath') |
dad8a5071b0a
templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents:
34837
diff
changeset
|
352 def slashpath(path): |
dad8a5071b0a
templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents:
34837
diff
changeset
|
353 """Any text. Replaces the native path separator with slash.""" |
dad8a5071b0a
templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents:
34837
diff
changeset
|
354 return util.pconvert(path) |
dad8a5071b0a
templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents:
34837
diff
changeset
|
355 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
356 @templatefilter('splitlines') |
21820
cce404b0c918
templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents:
19886
diff
changeset
|
357 def splitlines(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
358 """Any text. Split text into a list of lines.""" |
32039
2ab7578e685b
templatefilters: fix crash by string formatting of '{x|splitlines}'
Yuya Nishihara <yuya@tcha.org>
parents:
32037
diff
changeset
|
359 return templatekw.hybridlist(text.splitlines(), name='line') |
21820
cce404b0c918
templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents:
19886
diff
changeset
|
360 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
361 @templatefilter('stringescape') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
362 def stringescape(text): |
31451
53865692a354
util: wrap s.encode('string_escape') call for future py3 compatibility
Yuya Nishihara <yuya@tcha.org>
parents:
28883
diff
changeset
|
363 return util.escapestr(text) |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
364 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
365 @templatefilter('stringify') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
366 def stringify(thing): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
367 """Any type. Turns the value into text by converting values into |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
368 text and concatenating them. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
369 """ |
31880
a0f2d83f8083
templater: remove __iter__() from _hybrid, resolve it explicitly
Yuya Nishihara <yuya@tcha.org>
parents:
31782
diff
changeset
|
370 thing = templatekw.unwraphybrid(thing) |
32128
c3342c177211
py3: replace str with bytes in isinstance()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32127
diff
changeset
|
371 if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes): |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
372 return "".join([stringify(t) for t in thing if t is not None]) |
25000
c54248bbe023
templatefilters: don't stringify None into "None"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
24566
diff
changeset
|
373 if thing is None: |
c54248bbe023
templatefilters: don't stringify None into "None"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
24566
diff
changeset
|
374 return "" |
32127
964e7427a691
py3: use pycompat.bytestr() instead of str()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32126
diff
changeset
|
375 return pycompat.bytestr(thing) |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
376 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
377 @templatefilter('stripdir') |
8158
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
378 def stripdir(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
379 """Treat the text as path and strip a directory level, if |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
380 possible. For example, "foo" and "foo/bar" becomes "foo". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
381 """ |
8158
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
382 dir = os.path.dirname(text) |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
383 if dir == "": |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
384 return os.path.basename(text) |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
385 else: |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
386 return dir |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
387 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
388 @templatefilter('tabindent') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
389 def tabindent(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
390 """Any text. Returns the text, with every non-empty line |
19467
1afe5d3939db
template: fix tabindent docstring (issue2880)
Matt Mackall <mpm@selenic.com>
parents:
19228
diff
changeset
|
391 except the first starting with a tab character. |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
392 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
393 return indent(text, '\t') |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
394 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
395 @templatefilter('upper') |
24566
6abce80e6cbf
templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents:
23708
diff
changeset
|
396 def upper(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
397 """Any text. Converts the text to uppercase.""" |
24566
6abce80e6cbf
templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents:
23708
diff
changeset
|
398 return encoding.upper(text) |
6abce80e6cbf
templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents:
23708
diff
changeset
|
399 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
400 @templatefilter('urlescape') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
401 def urlescape(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
402 """Any text. Escapes all "special" characters. For example, |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
403 "foo bar" becomes "foo%20bar". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
404 """ |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28693
diff
changeset
|
405 return urlreq.quote(text) |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
406 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
407 @templatefilter('user') |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
408 def userfilter(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
409 """Any text. Returns a short representation of a user name or email |
16360
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
16251
diff
changeset
|
410 address.""" |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
411 return util.shortuser(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
412 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
413 @templatefilter('emailuser') |
16360
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
16251
diff
changeset
|
414 def emailuser(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
415 """Any text. Returns the user portion of an email address.""" |
16360
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
16251
diff
changeset
|
416 return util.emailuser(text) |
e5788269741a
templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents:
16251
diff
changeset
|
417 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
418 @templatefilter('utf8') |
28209
8ddf893560fa
templatefilters: add "utf8" to get utf-8 bytes from local-encoding text
Yuya Nishihara <yuya@tcha.org>
parents:
28208
diff
changeset
|
419 def utf8(text): |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
420 """Any text. Converts from the local character encoding to UTF-8.""" |
28209
8ddf893560fa
templatefilters: add "utf8" to get utf-8 bytes from local-encoding text
Yuya Nishihara <yuya@tcha.org>
parents:
28208
diff
changeset
|
421 return encoding.fromlocal(text) |
8ddf893560fa
templatefilters: add "utf8" to get utf-8 bytes from local-encoding text
Yuya Nishihara <yuya@tcha.org>
parents:
28208
diff
changeset
|
422 |
28693
11f623b5668f
templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28692
diff
changeset
|
423 @templatefilter('xmlescape') |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
424 def xmlescape(text): |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
425 text = (text |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
426 .replace('&', '&') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
427 .replace('<', '<') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
428 .replace('>', '>') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
429 .replace('"', '"') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
430 .replace("'", ''')) # ' invalid in HTML |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
431 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
|
432 |
18627
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
433 def websub(text, websubtable): |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
434 """:websub: Any text. Only applies to hgweb. Applies the regular |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
435 expression replacements defined in the websub section. |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
436 """ |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
437 if websubtable: |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
438 for regexp, format in websubtable: |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
439 text = regexp.sub(format, text) |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
440 return text |
4e949b8e0930
hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
17772
diff
changeset
|
441 |
28692
6b3b958daf03
registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28213
diff
changeset
|
442 def loadfilter(ui, extname, registrarobj): |
6b3b958daf03
registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28213
diff
changeset
|
443 """Load template filter from specified registrarobj |
6b3b958daf03
registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28213
diff
changeset
|
444 """ |
6b3b958daf03
registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28213
diff
changeset
|
445 for name, func in registrarobj._table.iteritems(): |
6b3b958daf03
registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28213
diff
changeset
|
446 filters[name] = func |
6b3b958daf03
registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28213
diff
changeset
|
447 |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
448 # tell hggettext to extract docstrings from these functions: |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
449 i18nfunctions = filters.values() |