Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/templatefilters.py @ 13591:264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
author | Patrick Mezard <pmezard@gmail.com> |
---|---|
date | Sat, 12 Mar 2011 12:46:31 +0100 |
parents | 1a752dcfe062 |
children | cc4721ed7a2a |
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 | 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 |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
10 from i18n import gettext |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
12 def addbreaks(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
13 """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
14 every line except the last. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
15 """ |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
16 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
|
17 |
10601
b98b6a7363ae
templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
18 agescales = [("year", 3600 * 24 * 365), |
b98b6a7363ae
templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
19 ("month", 3600 * 24 * 30), |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
20 ("week", 3600 * 24 * 7), |
10601
b98b6a7363ae
templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
21 ("day", 3600 * 24), |
b98b6a7363ae
templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
22 ("hour", 3600), |
b98b6a7363ae
templatefilters: store the agescales in reverseorder directly
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
23 ("minute", 60), |
10787
5974123d0339
templatefilters: fix check-code warning
Matt Mackall <mpm@selenic.com>
parents:
10601
diff
changeset
|
24 ("second", 1)] |
5976
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 age(date): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
27 """:age: Date. Returns a human-readable date/time difference between the |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
28 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
|
29 """ |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
31 def plural(t, c): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
32 if c == 1: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
33 return t |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
34 return t + "s" |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
35 def fmt(t, c): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
36 return "%d %s" % (c, plural(t, c)) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 now = time.time() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 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
|
40 if then > now: |
9c8bbae02e9c
templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6691
diff
changeset
|
41 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
|
42 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 for t, s in agescales: |
9029
0001e49f1c11
compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents:
8697
diff
changeset
|
48 n = delta // s |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 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
|
50 return '%s ago' % fmt(t, n) |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
52 def basename(path): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
53 """:basename: Any text. Treats the text as a path, and returns the last |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
54 component of the path after splitting by the path separator |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
55 (ignoring trailing separators). For example, "foo/bar/baz" becomes |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
56 "baz" and "foo/bar//" becomes "bar". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
57 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
58 return os.path.basename(path) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
59 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
60 def datefilter(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
61 """:date: Date. Returns a date in a Unix date format, including the |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
62 timezone: "Mon Sep 04 15:13:13 2006 0700". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
63 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
64 return util.datestr(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
65 |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
66 def domain(author): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
67 """:domain: Any text. Finds the first string that looks like an email |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
68 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
|
69 <user@example.com>`` becomes ``example.com``. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
70 """ |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
71 f = author.find('@') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
72 if f == -1: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
73 return '' |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
74 author = author[f + 1:] |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
75 f = author.find('>') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
76 if f >= 0: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
77 author = author[:f] |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
78 return author |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
79 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
80 def email(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
81 """:email: Any text. Extracts the first string that looks like an email |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
82 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
|
83 ``user@example.com``. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
84 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
85 return util.email(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
86 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
87 def escape(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
88 """:escape: Any text. Replaces the special XML/XHTML characters "&", "<" |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
89 and ">" with XML entities. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
90 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
91 return cgi.escape(text, True) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
92 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 para_re = None |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
94 space_re = None |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
95 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
96 def fill(text, width): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
97 '''fill many paragraphs.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
98 global para_re, space_re |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
99 if para_re is None: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
100 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
|
101 space_re = re.compile(r' +') |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
102 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
103 def findparas(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
104 start = 0 |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
105 while True: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
106 m = para_re.search(text, start) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 uctext[w:].encode(encoding.encoding)) |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
114 break |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
115 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
|
116 start = m.end(1) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
117 |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
10787
diff
changeset
|
118 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
|
119 for para, rest in findparas()]) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
120 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
121 def fill68(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
122 """:fill68: 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
|
123 return fill(text, 68) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
124 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
125 def fill76(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
126 """:fill76: 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
|
127 return fill(text, 76) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
128 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
129 def firstline(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
130 """:firstline: 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
|
131 try: |
9136
31177742f54a
for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9029
diff
changeset
|
132 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
|
133 except IndexError: |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
134 return '' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
135 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
136 def hexfilter(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
137 """:hex: Any text. Convert a binary Mercurial node identifier into |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
138 its long hexadecimal representation. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
139 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
140 return node.hex(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
141 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
142 def hgdate(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
143 """:hgdate: Date. Returns the date as a pair of numbers: "1157407993 |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
144 25200" (Unix timestamp, timezone offset). |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
145 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
146 return "%d %d" % text |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
147 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
148 def isodate(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
149 """:isodate: Date. Returns the date in ISO 8601 format: "2009-08-18 13:00 |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
150 +0200". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
151 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
152 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
|
153 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
154 def isodatesec(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
155 """:isodatesec: Date. Returns the date in ISO 8601 format, including |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
156 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
|
157 filter. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
158 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
159 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
|
160 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
161 def indent(text, prefix): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
162 '''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
|
163 lines = text.splitlines() |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
164 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
|
165 endswithnewline = text[-1:] == '\n' |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
166 def indenter(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
167 for i in xrange(num_lines): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
168 l = lines[i] |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
169 if i and l.strip(): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
170 yield prefix |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
171 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
|
172 if i < num_lines - 1 or endswithnewline: |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
173 yield '\n' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
174 return "".join(indenter()) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
175 |
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
176 def json(obj): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
177 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
|
178 return {None: 'null', False: 'false', True: 'true'}[obj] |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
179 elif isinstance(obj, int) or isinstance(obj, float): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
180 return str(obj) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
181 elif isinstance(obj, str): |
11765
aff419e260f9
templatefilters: make json filter handle multibyte characters correctly
Yuya Nishihara <yuya@tcha.org>
parents:
11297
diff
changeset
|
182 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
|
183 return '"%s"' % jsonescape(u) |
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
184 elif isinstance(obj, unicode): |
11890
9dac951d0185
templatefilters: use \uxxxx style escape for JSON string
Yuya Nishihara <yuya@tcha.org>
parents:
11765
diff
changeset
|
185 return '"%s"' % jsonescape(obj) |
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
186 elif hasattr(obj, 'keys'): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
187 out = [] |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
188 for k, v in obj.iteritems(): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
189 s = '%s: %s' % (json(k), json(v)) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
190 out.append(s) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
191 return '{' + ', '.join(out) + '}' |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
192 elif hasattr(obj, '__iter__'): |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
193 out = [] |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
194 for i in obj: |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
195 out.append(json(i)) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
196 return '[' + ', '.join(out) + ']' |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
197 else: |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
198 raise TypeError('cannot encode type %s' % obj.__class__.__name__) |
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
199 |
13589
b0a4b05c25e2
templatefilters: prefix helper functions
Patrick Mezard <pmezard@gmail.com>
parents:
13588
diff
changeset
|
200 def _uescape(c): |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
201 if ord(c) < 0x80: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
202 return c |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
203 else: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
204 return '\\u%04x' % ord(c) |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
205 |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
206 _escapes = [ |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
207 ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
208 ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
209 ] |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
210 |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
211 def jsonescape(s): |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
212 for k, v in _escapes: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
213 s = s.replace(k, v) |
13589
b0a4b05c25e2
templatefilters: prefix helper functions
Patrick Mezard <pmezard@gmail.com>
parents:
13588
diff
changeset
|
214 return ''.join(_uescape(c) for c in s) |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
215 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
216 def localdate(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
217 """:localdate: Date. Converts a date to local date.""" |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
218 return (text[0], util.makedate()[1]) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
219 |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
220 def nonempty(str): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
221 """:nonempty: Any text. Returns '(none)' if the string is empty.""" |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
222 return str or "(none)" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
223 |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
224 def obfuscate(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
225 """:obfuscate: Any text. Returns the input text rendered as a sequence of |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
226 XML entities. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
227 """ |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
228 text = unicode(text, encoding.encoding, 'replace') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
229 return ''.join(['&#%d;' % ord(c) for c in text]) |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
230 |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
231 def permissions(flags): |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
232 if "l" in flags: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
233 return "lrwxrwxrwx" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
234 if "x" in flags: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
235 return "-rwxr-xr-x" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
236 return "-rw-r--r--" |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
237 |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
238 def person(author): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
239 """:person: Any text. Returns the text before an email address.""" |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
240 if not '@' in author: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
241 return author |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
242 f = author.find('<') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
243 if f == -1: |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
244 return util.shortuser(author) |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
245 return author[:f].rstrip() |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
246 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
247 def rfc3339date(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
248 """:rfc3339date: Date. Returns a date using the Internet date format |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
249 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
|
250 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
251 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
|
252 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
253 def rfc822date(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
254 """:rfc822date: Date. Returns a date using the same format used in email |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
255 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
|
256 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
257 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
|
258 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
259 def short(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
260 """:short: Changeset hash. Returns the short form of a changeset hash, |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
261 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
|
262 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
263 return text[:12] |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
264 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
265 def shortdate(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
266 """:shortdate: 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
|
267 return util.shortdate(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
268 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
269 def stringescape(text): |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
270 return text.encode('string_escape') |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
271 |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
272 def stringify(thing): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
273 """:stringify: Any type. Turns the value into text by converting values into |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
274 text and concatenating them. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
275 """ |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
276 if hasattr(thing, '__iter__') and not isinstance(thing, str): |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
277 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
|
278 return str(thing) |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
279 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
280 def strip(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
281 """:strip: Any text. Strips all leading and trailing whitespace.""" |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
282 return text.strip() |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
283 |
8158
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
284 def stripdir(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
285 """:stripdir: Treat the text as path and strip a directory level, if |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
286 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
|
287 """ |
8158
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
288 dir = os.path.dirname(text) |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
289 if dir == "": |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
290 return os.path.basename(text) |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
291 else: |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
292 return dir |
1bef3656d9fe
templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents:
8014
diff
changeset
|
293 |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
294 def tabindent(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
295 """:tabindent: Any text. Returns the text, with every line except the |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
296 first starting with a tab character. |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
297 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
298 return indent(text, '\t') |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
299 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
300 def urlescape(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
301 """:urlescape: Any text. Escapes all "special" characters. For example, |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
302 "foo bar" becomes "foo%20bar". |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
303 """ |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
304 return urllib.quote(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
305 |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
306 def userfilter(text): |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
307 """:user: Any text. Returns the user portion of an email address.""" |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
308 return util.shortuser(text) |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
309 |
13588
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
310 def xmlescape(text): |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
311 text = (text |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
312 .replace('&', '&') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
313 .replace('<', '<') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
314 .replace('>', '>') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
315 .replace('"', '"') |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
316 .replace("'", ''')) # ' invalid in HTML |
b8b881f3f3a7
templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents:
13587
diff
changeset
|
317 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
|
318 |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
319 filters = { |
13587
9fb6850d5d97
templatefilters: match filter keys and function names
Patrick Mezard <pmezard@gmail.com>
parents:
13586
diff
changeset
|
320 "addbreaks": addbreaks, |
13586
57150dc5a9c7
templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents:
12371
diff
changeset
|
321 "age": age, |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
322 "basename": basename, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
323 "date": datefilter, |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
324 "domain": domain, |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
325 "email": email, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
326 "escape": escape, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
327 "fill68": fill68, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
328 "fill76": fill76, |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
329 "firstline": firstline, |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
330 "hex": hexfilter, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
331 "hgdate": hgdate, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
332 "isodate": isodate, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
333 "isodatesec": isodatesec, |
8014
6a77ba181bc6
templatefilters: split out jsonescape() function
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7948
diff
changeset
|
334 "json": json, |
6a77ba181bc6
templatefilters: split out jsonescape() function
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7948
diff
changeset
|
335 "jsonescape": jsonescape, |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
336 "localdate": localdate, |
8234
27dbe534397b
templatefilters: add "nonempty" template filter
Rocco Rutte <pdmef@gmx.net>
parents:
8225
diff
changeset
|
337 "nonempty": nonempty, |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
338 "obfuscate": obfuscate, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
339 "permissions": permissions, |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
340 "person": person, |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
341 "rfc3339date": rfc3339date, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
342 "rfc822date": rfc822date, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
343 "short": short, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
344 "shortdate": shortdate, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
345 "stringescape": stringescape, |
8360
acc202b71619
templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8234
diff
changeset
|
346 "stringify": stringify, |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
347 "strip": strip, |
13586
57150dc5a9c7
templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents:
12371
diff
changeset
|
348 "stripdir": stripdir, |
13590
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
349 "tabindent": tabindent, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
350 "urlescape": urlescape, |
1a752dcfe062
templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents:
13589
diff
changeset
|
351 "user": userfilter, |
6174
434139080ed4
Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents:
6134
diff
changeset
|
352 "xmlescape": xmlescape, |
6691
0dba955c2636
add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6319
diff
changeset
|
353 } |
13591
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
354 |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
355 def makedoc(topic, doc): |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
356 """Generate and include templatefilters help in templating topic.""" |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
357 entries = [] |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
358 for name in sorted(filters): |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
359 text = (filters[name].__doc__ or '').rstrip() |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
360 if not text: |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
361 continue |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
362 text = gettext(text) |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
363 lines = text.splitlines() |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
364 lines[1:] = [(' ' + l.strip()) for l in lines[1:]] |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
365 entries.append('\n'.join(lines)) |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
366 entries = '\n\n'.join(entries) |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
367 doc = doc.replace('.. filtersmarker', entries) |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
368 return doc |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
369 |
264f292a0c6f
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents:
13590
diff
changeset
|
370 # 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
|
371 i18nfunctions = filters.values() |