Show More
@@ -1,228 +1,288 | |||
|
1 | 1 | # template-filters.py - common template expansion filters |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2008 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import cgi, re, os, time, urllib |
|
9 | 9 | import encoding, node, util |
|
10 | 10 | |
|
11 | 11 | def addbreaks(text): |
|
12 | 12 | '''replace raw newlines with xhtml line breaks.''' |
|
13 | 13 | return text.replace('\n', '<br/>\n') |
|
14 | 14 | |
|
15 | 15 | agescales = [("year", 3600 * 24 * 365), |
|
16 | 16 | ("month", 3600 * 24 * 30), |
|
17 | 17 | ("week", 3600 * 24 * 7), |
|
18 | 18 | ("day", 3600 * 24), |
|
19 | 19 | ("hour", 3600), |
|
20 | 20 | ("minute", 60), |
|
21 | 21 | ("second", 1)] |
|
22 | 22 | |
|
23 | 23 | def age(date): |
|
24 | 24 | '''turn a (timestamp, tzoff) tuple into an age string.''' |
|
25 | 25 | |
|
26 | 26 | def plural(t, c): |
|
27 | 27 | if c == 1: |
|
28 | 28 | return t |
|
29 | 29 | return t + "s" |
|
30 | 30 | def fmt(t, c): |
|
31 | 31 | return "%d %s" % (c, plural(t, c)) |
|
32 | 32 | |
|
33 | 33 | now = time.time() |
|
34 | 34 | then = date[0] |
|
35 | 35 | if then > now: |
|
36 | 36 | return 'in the future' |
|
37 | 37 | |
|
38 | 38 | delta = max(1, int(now - then)) |
|
39 | 39 | if delta > agescales[0][1] * 2: |
|
40 | 40 | return util.shortdate(date) |
|
41 | 41 | |
|
42 | 42 | for t, s in agescales: |
|
43 | 43 | n = delta // s |
|
44 | 44 | if n >= 2 or s == 1: |
|
45 | 45 | return '%s ago' % fmt(t, n) |
|
46 | 46 | |
|
47 | def basename(path): | |
|
48 | return os.path.basename(path) | |
|
49 | ||
|
50 | def datefilter(text): | |
|
51 | return util.datestr(text) | |
|
52 | ||
|
47 | 53 | def domain(author): |
|
48 | 54 | '''get domain of author, or empty string if none.''' |
|
49 | 55 | f = author.find('@') |
|
50 | 56 | if f == -1: |
|
51 | 57 | return '' |
|
52 | 58 | author = author[f + 1:] |
|
53 | 59 | f = author.find('>') |
|
54 | 60 | if f >= 0: |
|
55 | 61 | author = author[:f] |
|
56 | 62 | return author |
|
57 | 63 | |
|
64 | def email(text): | |
|
65 | return util.email(text) | |
|
66 | ||
|
67 | def escape(text): | |
|
68 | return cgi.escape(text, True) | |
|
69 | ||
|
58 | 70 | para_re = None |
|
59 | 71 | space_re = None |
|
60 | 72 | |
|
61 | 73 | def fill(text, width): |
|
62 | 74 | '''fill many paragraphs.''' |
|
63 | 75 | global para_re, space_re |
|
64 | 76 | if para_re is None: |
|
65 | 77 | para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) |
|
66 | 78 | space_re = re.compile(r' +') |
|
67 | 79 | |
|
68 | 80 | def findparas(): |
|
69 | 81 | start = 0 |
|
70 | 82 | while True: |
|
71 | 83 | m = para_re.search(text, start) |
|
72 | 84 | if not m: |
|
73 | 85 | uctext = unicode(text[start:], encoding.encoding) |
|
74 | 86 | w = len(uctext) |
|
75 | 87 | while 0 < w and uctext[w - 1].isspace(): |
|
76 | 88 | w -= 1 |
|
77 | 89 | yield (uctext[:w].encode(encoding.encoding), |
|
78 | 90 | uctext[w:].encode(encoding.encoding)) |
|
79 | 91 | break |
|
80 | 92 | yield text[start:m.start(0)], m.group(1) |
|
81 | 93 | start = m.end(1) |
|
82 | 94 | |
|
83 | 95 | return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest |
|
84 | 96 | for para, rest in findparas()]) |
|
85 | 97 | |
|
98 | def fill68(text): | |
|
99 | return fill(text, 68) | |
|
100 | ||
|
101 | def fill76(text): | |
|
102 | return fill(text, 76) | |
|
103 | ||
|
86 | 104 | def firstline(text): |
|
87 | 105 | '''return the first line of text''' |
|
88 | 106 | try: |
|
89 | 107 | return text.splitlines(True)[0].rstrip('\r\n') |
|
90 | 108 | except IndexError: |
|
91 | 109 | return '' |
|
92 | 110 | |
|
111 | def hexfilter(text): | |
|
112 | return node.hex(text) | |
|
113 | ||
|
114 | def hgdate(text): | |
|
115 | return "%d %d" % text | |
|
116 | ||
|
117 | def isodate(text): | |
|
118 | return util.datestr(text, '%Y-%m-%d %H:%M %1%2') | |
|
119 | ||
|
120 | def isodatesec(text): | |
|
121 | return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2') | |
|
122 | ||
|
93 | 123 | def indent(text, prefix): |
|
94 | 124 | '''indent each non-empty line of text after first with prefix.''' |
|
95 | 125 | lines = text.splitlines() |
|
96 | 126 | num_lines = len(lines) |
|
97 | 127 | endswithnewline = text[-1:] == '\n' |
|
98 | 128 | def indenter(): |
|
99 | 129 | for i in xrange(num_lines): |
|
100 | 130 | l = lines[i] |
|
101 | 131 | if i and l.strip(): |
|
102 | 132 | yield prefix |
|
103 | 133 | yield l |
|
104 | 134 | if i < num_lines - 1 or endswithnewline: |
|
105 | 135 | yield '\n' |
|
106 | 136 | return "".join(indenter()) |
|
107 | 137 | |
|
108 | 138 | def json(obj): |
|
109 | 139 | if obj is None or obj is False or obj is True: |
|
110 | 140 | return {None: 'null', False: 'false', True: 'true'}[obj] |
|
111 | 141 | elif isinstance(obj, int) or isinstance(obj, float): |
|
112 | 142 | return str(obj) |
|
113 | 143 | elif isinstance(obj, str): |
|
114 | 144 | u = unicode(obj, encoding.encoding, 'replace') |
|
115 | 145 | return '"%s"' % jsonescape(u) |
|
116 | 146 | elif isinstance(obj, unicode): |
|
117 | 147 | return '"%s"' % jsonescape(obj) |
|
118 | 148 | elif hasattr(obj, 'keys'): |
|
119 | 149 | out = [] |
|
120 | 150 | for k, v in obj.iteritems(): |
|
121 | 151 | s = '%s: %s' % (json(k), json(v)) |
|
122 | 152 | out.append(s) |
|
123 | 153 | return '{' + ', '.join(out) + '}' |
|
124 | 154 | elif hasattr(obj, '__iter__'): |
|
125 | 155 | out = [] |
|
126 | 156 | for i in obj: |
|
127 | 157 | out.append(json(i)) |
|
128 | 158 | return '[' + ', '.join(out) + ']' |
|
129 | 159 | else: |
|
130 | 160 | raise TypeError('cannot encode type %s' % obj.__class__.__name__) |
|
131 | 161 | |
|
132 | 162 | def _uescape(c): |
|
133 | 163 | if ord(c) < 0x80: |
|
134 | 164 | return c |
|
135 | 165 | else: |
|
136 | 166 | return '\\u%04x' % ord(c) |
|
137 | 167 | |
|
138 | 168 | _escapes = [ |
|
139 | 169 | ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), |
|
140 | 170 | ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), |
|
141 | 171 | ] |
|
142 | 172 | |
|
143 | 173 | def jsonescape(s): |
|
144 | 174 | for k, v in _escapes: |
|
145 | 175 | s = s.replace(k, v) |
|
146 | 176 | return ''.join(_uescape(c) for c in s) |
|
147 | 177 | |
|
178 | def localdate(text): | |
|
179 | return (text[0], util.makedate()[1]) | |
|
180 | ||
|
148 | 181 | def nonempty(str): |
|
149 | 182 | return str or "(none)" |
|
150 | 183 | |
|
151 | 184 | def obfuscate(text): |
|
152 | 185 | text = unicode(text, encoding.encoding, 'replace') |
|
153 | 186 | return ''.join(['&#%d;' % ord(c) for c in text]) |
|
154 | 187 | |
|
155 | 188 | def permissions(flags): |
|
156 | 189 | if "l" in flags: |
|
157 | 190 | return "lrwxrwxrwx" |
|
158 | 191 | if "x" in flags: |
|
159 | 192 | return "-rwxr-xr-x" |
|
160 | 193 | return "-rw-r--r--" |
|
161 | 194 | |
|
162 | 195 | def person(author): |
|
163 | 196 | '''get name of author, or else username.''' |
|
164 | 197 | if not '@' in author: |
|
165 | 198 | return author |
|
166 | 199 | f = author.find('<') |
|
167 | 200 | if f == -1: |
|
168 | 201 | return util.shortuser(author) |
|
169 | 202 | return author[:f].rstrip() |
|
170 | 203 | |
|
204 | def rfc3339date(text): | |
|
205 | return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2") | |
|
206 | ||
|
207 | def rfc822date(text): | |
|
208 | return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2") | |
|
209 | ||
|
210 | def short(text): | |
|
211 | return text[:12] | |
|
212 | ||
|
213 | def shortdate(text): | |
|
214 | return util.shortdate(text) | |
|
215 | ||
|
216 | def stringescape(text): | |
|
217 | return text.encode('string_escape') | |
|
218 | ||
|
171 | 219 | def stringify(thing): |
|
172 | 220 | '''turn nested template iterator into string.''' |
|
173 | 221 | if hasattr(thing, '__iter__') and not isinstance(thing, str): |
|
174 | 222 | return "".join([stringify(t) for t in thing if t is not None]) |
|
175 | 223 | return str(thing) |
|
176 | 224 | |
|
225 | def strip(text): | |
|
226 | return text.strip() | |
|
227 | ||
|
177 | 228 | def stripdir(text): |
|
178 | 229 | '''Treat the text as path and strip a directory level, if possible.''' |
|
179 | 230 | dir = os.path.dirname(text) |
|
180 | 231 | if dir == "": |
|
181 | 232 | return os.path.basename(text) |
|
182 | 233 | else: |
|
183 | 234 | return dir |
|
184 | 235 | |
|
236 | def tabindent(text): | |
|
237 | return indent(text, '\t') | |
|
238 | ||
|
239 | def urlescape(text): | |
|
240 | return urllib.quote(text) | |
|
241 | ||
|
242 | def userfilter(text): | |
|
243 | return util.shortuser(text) | |
|
244 | ||
|
185 | 245 | def xmlescape(text): |
|
186 | 246 | text = (text |
|
187 | 247 | .replace('&', '&') |
|
188 | 248 | .replace('<', '<') |
|
189 | 249 | .replace('>', '>') |
|
190 | 250 | .replace('"', '"') |
|
191 | 251 | .replace("'", ''')) # ' invalid in HTML |
|
192 | 252 | return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
|
193 | 253 | |
|
194 | 254 | filters = { |
|
195 | 255 | "addbreaks": addbreaks, |
|
196 | 256 | "age": age, |
|
197 |
"basename": |
|
|
198 |
"date": |
|
|
257 | "basename": basename, | |
|
258 | "date": datefilter, | |
|
199 | 259 | "domain": domain, |
|
200 |
"email": |
|
|
201 |
"escape": |
|
|
202 |
"fill68": |
|
|
203 |
"fill76": |
|
|
260 | "email": email, | |
|
261 | "escape": escape, | |
|
262 | "fill68": fill68, | |
|
263 | "fill76": fill76, | |
|
204 | 264 | "firstline": firstline, |
|
205 |
"hex": |
|
|
206 | "hgdate": lambda x: "%d %d" % x, | |
|
207 | "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'), | |
|
208 | "isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'), | |
|
265 | "hex": hexfilter, | |
|
266 | "hgdate": hgdate, | |
|
267 | "isodate": isodate, | |
|
268 | "isodatesec": isodatesec, | |
|
209 | 269 | "json": json, |
|
210 | 270 | "jsonescape": jsonescape, |
|
211 |
"localdate": l |
|
|
271 | "localdate": localdate, | |
|
212 | 272 | "nonempty": nonempty, |
|
213 | 273 | "obfuscate": obfuscate, |
|
214 | 274 | "permissions": permissions, |
|
215 | 275 | "person": person, |
|
216 | "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"), | |
|
217 | "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"), | |
|
218 | "short": lambda x: x[:12], | |
|
219 |
"shortdate": |
|
|
220 |
"stringescape": |
|
|
276 | "rfc3339date": rfc3339date, | |
|
277 | "rfc822date": rfc822date, | |
|
278 | "short": short, | |
|
279 | "shortdate": shortdate, | |
|
280 | "stringescape": stringescape, | |
|
221 | 281 | "stringify": stringify, |
|
222 |
"strip": |
|
|
282 | "strip": strip, | |
|
223 | 283 | "stripdir": stripdir, |
|
224 |
"tabindent": |
|
|
225 |
"urlescape": |
|
|
226 | "user": lambda x: util.shortuser(x), | |
|
284 | "tabindent": tabindent, | |
|
285 | "urlescape": urlescape, | |
|
286 | "user": userfilter, | |
|
227 | 287 | "xmlescape": xmlescape, |
|
228 | 288 | } |
General Comments 0
You need to be logged in to leave comments.
Login now