Show More
@@ -1,153 +1,154 | |||||
1 | # template-filters.py - common template expansion filters |
|
1 | # template-filters.py - common template expansion filters | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2008 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005-2008 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | import cgi, re, os, time, urllib, textwrap |
|
8 | import cgi, re, os, time, urllib, textwrap | |
9 | import util, templater |
|
9 | import util, templater | |
10 |
|
10 | |||
11 | agescales = [("second", 1), |
|
11 | agescales = [("second", 1), | |
12 | ("minute", 60), |
|
12 | ("minute", 60), | |
13 | ("hour", 3600), |
|
13 | ("hour", 3600), | |
14 | ("day", 3600 * 24), |
|
14 | ("day", 3600 * 24), | |
15 | ("week", 3600 * 24 * 7), |
|
15 | ("week", 3600 * 24 * 7), | |
16 | ("month", 3600 * 24 * 30), |
|
16 | ("month", 3600 * 24 * 30), | |
17 | ("year", 3600 * 24 * 365)] |
|
17 | ("year", 3600 * 24 * 365)] | |
18 |
|
18 | |||
19 | agescales.reverse() |
|
19 | agescales.reverse() | |
20 |
|
20 | |||
21 | def age(date): |
|
21 | def age(date): | |
22 | '''turn a (timestamp, tzoff) tuple into an age string.''' |
|
22 | '''turn a (timestamp, tzoff) tuple into an age string.''' | |
23 |
|
23 | |||
24 | def plural(t, c): |
|
24 | def plural(t, c): | |
25 | if c == 1: |
|
25 | if c == 1: | |
26 | return t |
|
26 | return t | |
27 | return t + "s" |
|
27 | return t + "s" | |
28 | def fmt(t, c): |
|
28 | def fmt(t, c): | |
29 | return "%d %s" % (c, plural(t, c)) |
|
29 | return "%d %s" % (c, plural(t, c)) | |
30 |
|
30 | |||
31 | now = time.time() |
|
31 | now = time.time() | |
32 | then = date[0] |
|
32 | then = date[0] | |
33 | delta = max(1, int(now - then)) |
|
33 | delta = max(1, int(now - then)) | |
34 |
|
34 | |||
35 | for t, s in agescales: |
|
35 | for t, s in agescales: | |
36 | n = delta / s |
|
36 | n = delta / s | |
37 | if n >= 2 or s == 1: |
|
37 | if n >= 2 or s == 1: | |
38 | return fmt(t, n) |
|
38 | return fmt(t, n) | |
39 |
|
39 | |||
40 | para_re = None |
|
40 | para_re = None | |
41 | space_re = None |
|
41 | space_re = None | |
42 |
|
42 | |||
43 | def fill(text, width): |
|
43 | def fill(text, width): | |
44 | '''fill many paragraphs.''' |
|
44 | '''fill many paragraphs.''' | |
45 | global para_re, space_re |
|
45 | global para_re, space_re | |
46 | if para_re is None: |
|
46 | if para_re is None: | |
47 | para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) |
|
47 | para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) | |
48 | space_re = re.compile(r' +') |
|
48 | space_re = re.compile(r' +') | |
49 |
|
49 | |||
50 | def findparas(): |
|
50 | def findparas(): | |
51 | start = 0 |
|
51 | start = 0 | |
52 | while True: |
|
52 | while True: | |
53 | m = para_re.search(text, start) |
|
53 | m = para_re.search(text, start) | |
54 | if not m: |
|
54 | if not m: | |
55 | w = len(text) |
|
55 | w = len(text) | |
56 | while w > start and text[w-1].isspace(): w -= 1 |
|
56 | while w > start and text[w-1].isspace(): w -= 1 | |
57 | yield text[start:w], text[w:] |
|
57 | yield text[start:w], text[w:] | |
58 | break |
|
58 | break | |
59 | yield text[start:m.start(0)], m.group(1) |
|
59 | yield text[start:m.start(0)], m.group(1) | |
60 | start = m.end(1) |
|
60 | start = m.end(1) | |
61 |
|
61 | |||
62 | return "".join([space_re.sub(' ', textwrap.fill(para, width)) + rest |
|
62 | return "".join([space_re.sub(' ', textwrap.fill(para, width)) + rest | |
63 | for para, rest in findparas()]) |
|
63 | for para, rest in findparas()]) | |
64 |
|
64 | |||
65 | def firstline(text): |
|
65 | def firstline(text): | |
66 | '''return the first line of text''' |
|
66 | '''return the first line of text''' | |
67 | try: |
|
67 | try: | |
68 | return text.splitlines(1)[0].rstrip('\r\n') |
|
68 | return text.splitlines(1)[0].rstrip('\r\n') | |
69 | except IndexError: |
|
69 | except IndexError: | |
70 | return '' |
|
70 | return '' | |
71 |
|
71 | |||
72 | def nl2br(text): |
|
72 | def nl2br(text): | |
73 | '''replace raw newlines with xhtml line breaks.''' |
|
73 | '''replace raw newlines with xhtml line breaks.''' | |
74 | return text.replace('\n', '<br/>\n') |
|
74 | return text.replace('\n', '<br/>\n') | |
75 |
|
75 | |||
76 | def obfuscate(text): |
|
76 | def obfuscate(text): | |
77 | text = unicode(text, util._encoding, 'replace') |
|
77 | text = unicode(text, util._encoding, 'replace') | |
78 | return ''.join(['&#%d;' % ord(c) for c in text]) |
|
78 | return ''.join(['&#%d;' % ord(c) for c in text]) | |
79 |
|
79 | |||
80 | def domain(author): |
|
80 | def domain(author): | |
81 | '''get domain of author, or empty string if none.''' |
|
81 | '''get domain of author, or empty string if none.''' | |
82 | f = author.find('@') |
|
82 | f = author.find('@') | |
83 | if f == -1: return '' |
|
83 | if f == -1: return '' | |
84 | author = author[f+1:] |
|
84 | author = author[f+1:] | |
85 | f = author.find('>') |
|
85 | f = author.find('>') | |
86 | if f >= 0: author = author[:f] |
|
86 | if f >= 0: author = author[:f] | |
87 | return author |
|
87 | return author | |
88 |
|
88 | |||
89 | def person(author): |
|
89 | def person(author): | |
90 | '''get name of author, or else username.''' |
|
90 | '''get name of author, or else username.''' | |
91 | f = author.find('<') |
|
91 | f = author.find('<') | |
92 | if f == -1: return util.shortuser(author) |
|
92 | if f == -1: return util.shortuser(author) | |
93 | return author[:f].rstrip() |
|
93 | return author[:f].rstrip() | |
94 |
|
94 | |||
95 | def indent(text, prefix): |
|
95 | def indent(text, prefix): | |
96 | '''indent each non-empty line of text after first with prefix.''' |
|
96 | '''indent each non-empty line of text after first with prefix.''' | |
97 | lines = text.splitlines() |
|
97 | lines = text.splitlines() | |
98 | num_lines = len(lines) |
|
98 | num_lines = len(lines) | |
99 | def indenter(): |
|
99 | def indenter(): | |
100 | for i in xrange(num_lines): |
|
100 | for i in xrange(num_lines): | |
101 | l = lines[i] |
|
101 | l = lines[i] | |
102 | if i and l.strip(): |
|
102 | if i and l.strip(): | |
103 | yield prefix |
|
103 | yield prefix | |
104 | yield l |
|
104 | yield l | |
105 | if i < num_lines - 1 or text.endswith('\n'): |
|
105 | if i < num_lines - 1 or text.endswith('\n'): | |
106 | yield '\n' |
|
106 | yield '\n' | |
107 | return "".join(indenter()) |
|
107 | return "".join(indenter()) | |
108 |
|
108 | |||
109 | def permissions(flags): |
|
109 | def permissions(flags): | |
110 | if "l" in flags: |
|
110 | if "l" in flags: | |
111 | return "lrwxrwxrwx" |
|
111 | return "lrwxrwxrwx" | |
112 | if "x" in flags: |
|
112 | if "x" in flags: | |
113 | return "-rwxr-xr-x" |
|
113 | return "-rwxr-xr-x" | |
114 | return "-rw-r--r--" |
|
114 | return "-rw-r--r--" | |
115 |
|
115 | |||
116 | def xmlescape(text): |
|
116 | def xmlescape(text): | |
117 | text = (text |
|
117 | text = (text | |
118 | .replace('&', '&') |
|
118 | .replace('&', '&') | |
119 | .replace('<', '<') |
|
119 | .replace('<', '<') | |
120 | .replace('>', '>') |
|
120 | .replace('>', '>') | |
121 | .replace('"', '"') |
|
121 | .replace('"', '"') | |
122 | .replace("'", ''')) # ' invalid in HTML |
|
122 | .replace("'", ''')) # ' invalid in HTML | |
123 | return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
|
123 | return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) | |
124 |
|
124 | |||
125 | filters = { |
|
125 | filters = { | |
126 | "addbreaks": nl2br, |
|
126 | "addbreaks": nl2br, | |
127 | "basename": os.path.basename, |
|
127 | "basename": os.path.basename, | |
128 | "age": age, |
|
128 | "age": age, | |
129 | "date": lambda x: util.datestr(x), |
|
129 | "date": lambda x: util.datestr(x), | |
130 | "domain": domain, |
|
130 | "domain": domain, | |
131 | "email": util.email, |
|
131 | "email": util.email, | |
132 | "escape": lambda x: cgi.escape(x, True), |
|
132 | "escape": lambda x: cgi.escape(x, True), | |
133 | "fill68": lambda x: fill(x, width=68), |
|
133 | "fill68": lambda x: fill(x, width=68), | |
134 | "fill76": lambda x: fill(x, width=76), |
|
134 | "fill76": lambda x: fill(x, width=76), | |
135 | "firstline": firstline, |
|
135 | "firstline": firstline, | |
136 | "tabindent": lambda x: indent(x, '\t'), |
|
136 | "tabindent": lambda x: indent(x, '\t'), | |
137 | "hgdate": lambda x: "%d %d" % x, |
|
137 | "hgdate": lambda x: "%d %d" % x, | |
138 | "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'), |
|
138 | "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'), | |
|
139 | "isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'), | |||
139 | "obfuscate": obfuscate, |
|
140 | "obfuscate": obfuscate, | |
140 | "permissions": permissions, |
|
141 | "permissions": permissions, | |
141 | "person": person, |
|
142 | "person": person, | |
142 | "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"), |
|
143 | "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"), | |
143 | "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"), |
|
144 | "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"), | |
144 | "short": lambda x: x[:12], |
|
145 | "short": lambda x: x[:12], | |
145 | "shortdate": util.shortdate, |
|
146 | "shortdate": util.shortdate, | |
146 | "stringify": templater.stringify, |
|
147 | "stringify": templater.stringify, | |
147 | "strip": lambda x: x.strip(), |
|
148 | "strip": lambda x: x.strip(), | |
148 | "urlescape": lambda x: urllib.quote(x), |
|
149 | "urlescape": lambda x: urllib.quote(x), | |
149 | "user": lambda x: util.shortuser(x), |
|
150 | "user": lambda x: util.shortuser(x), | |
150 | "stringescape": lambda x: x.encode('string_escape'), |
|
151 | "stringescape": lambda x: x.encode('string_escape'), | |
151 | "xmlescape": xmlescape, |
|
152 | "xmlescape": xmlescape, | |
152 | } |
|
153 | } | |
153 |
|
154 |
General Comments 0
You need to be logged in to leave comments.
Login now