Show More
@@ -1,228 +1,228 | |||
|
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 | 47 | def domain(author): |
|
48 | 48 | '''get domain of author, or empty string if none.''' |
|
49 | 49 | f = author.find('@') |
|
50 | 50 | if f == -1: |
|
51 | 51 | return '' |
|
52 | 52 | author = author[f + 1:] |
|
53 | 53 | f = author.find('>') |
|
54 | 54 | if f >= 0: |
|
55 | 55 | author = author[:f] |
|
56 | 56 | return author |
|
57 | 57 | |
|
58 | 58 | para_re = None |
|
59 | 59 | space_re = None |
|
60 | 60 | |
|
61 | 61 | def fill(text, width): |
|
62 | 62 | '''fill many paragraphs.''' |
|
63 | 63 | global para_re, space_re |
|
64 | 64 | if para_re is None: |
|
65 | 65 | para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) |
|
66 | 66 | space_re = re.compile(r' +') |
|
67 | 67 | |
|
68 | 68 | def findparas(): |
|
69 | 69 | start = 0 |
|
70 | 70 | while True: |
|
71 | 71 | m = para_re.search(text, start) |
|
72 | 72 | if not m: |
|
73 | 73 | uctext = unicode(text[start:], encoding.encoding) |
|
74 | 74 | w = len(uctext) |
|
75 | 75 | while 0 < w and uctext[w - 1].isspace(): |
|
76 | 76 | w -= 1 |
|
77 | 77 | yield (uctext[:w].encode(encoding.encoding), |
|
78 | 78 | uctext[w:].encode(encoding.encoding)) |
|
79 | 79 | break |
|
80 | 80 | yield text[start:m.start(0)], m.group(1) |
|
81 | 81 | start = m.end(1) |
|
82 | 82 | |
|
83 | 83 | return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest |
|
84 | 84 | for para, rest in findparas()]) |
|
85 | 85 | |
|
86 | 86 | def firstline(text): |
|
87 | 87 | '''return the first line of text''' |
|
88 | 88 | try: |
|
89 | 89 | return text.splitlines(True)[0].rstrip('\r\n') |
|
90 | 90 | except IndexError: |
|
91 | 91 | return '' |
|
92 | 92 | |
|
93 | 93 | def indent(text, prefix): |
|
94 | 94 | '''indent each non-empty line of text after first with prefix.''' |
|
95 | 95 | lines = text.splitlines() |
|
96 | 96 | num_lines = len(lines) |
|
97 | 97 | endswithnewline = text[-1:] == '\n' |
|
98 | 98 | def indenter(): |
|
99 | 99 | for i in xrange(num_lines): |
|
100 | 100 | l = lines[i] |
|
101 | 101 | if i and l.strip(): |
|
102 | 102 | yield prefix |
|
103 | 103 | yield l |
|
104 | 104 | if i < num_lines - 1 or endswithnewline: |
|
105 | 105 | yield '\n' |
|
106 | 106 | return "".join(indenter()) |
|
107 | 107 | |
|
108 | 108 | def json(obj): |
|
109 | 109 | if obj is None or obj is False or obj is True: |
|
110 | 110 | return {None: 'null', False: 'false', True: 'true'}[obj] |
|
111 | 111 | elif isinstance(obj, int) or isinstance(obj, float): |
|
112 | 112 | return str(obj) |
|
113 | 113 | elif isinstance(obj, str): |
|
114 | 114 | u = unicode(obj, encoding.encoding, 'replace') |
|
115 | 115 | return '"%s"' % jsonescape(u) |
|
116 | 116 | elif isinstance(obj, unicode): |
|
117 | 117 | return '"%s"' % jsonescape(obj) |
|
118 | 118 | elif hasattr(obj, 'keys'): |
|
119 | 119 | out = [] |
|
120 | 120 | for k, v in obj.iteritems(): |
|
121 | 121 | s = '%s: %s' % (json(k), json(v)) |
|
122 | 122 | out.append(s) |
|
123 | 123 | return '{' + ', '.join(out) + '}' |
|
124 | 124 | elif hasattr(obj, '__iter__'): |
|
125 | 125 | out = [] |
|
126 | 126 | for i in obj: |
|
127 | 127 | out.append(json(i)) |
|
128 | 128 | return '[' + ', '.join(out) + ']' |
|
129 | 129 | else: |
|
130 | 130 | raise TypeError('cannot encode type %s' % obj.__class__.__name__) |
|
131 | 131 | |
|
132 | def uescape(c): | |
|
132 | def _uescape(c): | |
|
133 | 133 | if ord(c) < 0x80: |
|
134 | 134 | return c |
|
135 | 135 | else: |
|
136 | 136 | return '\\u%04x' % ord(c) |
|
137 | 137 | |
|
138 | 138 | _escapes = [ |
|
139 | 139 | ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), |
|
140 | 140 | ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), |
|
141 | 141 | ] |
|
142 | 142 | |
|
143 | 143 | def jsonescape(s): |
|
144 | 144 | for k, v in _escapes: |
|
145 | 145 | s = s.replace(k, v) |
|
146 | return ''.join(uescape(c) for c in s) | |
|
146 | return ''.join(_uescape(c) for c in s) | |
|
147 | 147 | |
|
148 | 148 | def nonempty(str): |
|
149 | 149 | return str or "(none)" |
|
150 | 150 | |
|
151 | 151 | def obfuscate(text): |
|
152 | 152 | text = unicode(text, encoding.encoding, 'replace') |
|
153 | 153 | return ''.join(['&#%d;' % ord(c) for c in text]) |
|
154 | 154 | |
|
155 | 155 | def permissions(flags): |
|
156 | 156 | if "l" in flags: |
|
157 | 157 | return "lrwxrwxrwx" |
|
158 | 158 | if "x" in flags: |
|
159 | 159 | return "-rwxr-xr-x" |
|
160 | 160 | return "-rw-r--r--" |
|
161 | 161 | |
|
162 | 162 | def person(author): |
|
163 | 163 | '''get name of author, or else username.''' |
|
164 | 164 | if not '@' in author: |
|
165 | 165 | return author |
|
166 | 166 | f = author.find('<') |
|
167 | 167 | if f == -1: |
|
168 | 168 | return util.shortuser(author) |
|
169 | 169 | return author[:f].rstrip() |
|
170 | 170 | |
|
171 | 171 | def stringify(thing): |
|
172 | 172 | '''turn nested template iterator into string.''' |
|
173 | 173 | if hasattr(thing, '__iter__') and not isinstance(thing, str): |
|
174 | 174 | return "".join([stringify(t) for t in thing if t is not None]) |
|
175 | 175 | return str(thing) |
|
176 | 176 | |
|
177 | 177 | def stripdir(text): |
|
178 | 178 | '''Treat the text as path and strip a directory level, if possible.''' |
|
179 | 179 | dir = os.path.dirname(text) |
|
180 | 180 | if dir == "": |
|
181 | 181 | return os.path.basename(text) |
|
182 | 182 | else: |
|
183 | 183 | return dir |
|
184 | 184 | |
|
185 | 185 | def xmlescape(text): |
|
186 | 186 | text = (text |
|
187 | 187 | .replace('&', '&') |
|
188 | 188 | .replace('<', '<') |
|
189 | 189 | .replace('>', '>') |
|
190 | 190 | .replace('"', '"') |
|
191 | 191 | .replace("'", ''')) # ' invalid in HTML |
|
192 | 192 | return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
|
193 | 193 | |
|
194 | 194 | filters = { |
|
195 | 195 | "addbreaks": addbreaks, |
|
196 | 196 | "age": age, |
|
197 | 197 | "basename": os.path.basename, |
|
198 | 198 | "date": lambda x: util.datestr(x), |
|
199 | 199 | "domain": domain, |
|
200 | 200 | "email": util.email, |
|
201 | 201 | "escape": lambda x: cgi.escape(x, True), |
|
202 | 202 | "fill68": lambda x: fill(x, width=68), |
|
203 | 203 | "fill76": lambda x: fill(x, width=76), |
|
204 | 204 | "firstline": firstline, |
|
205 | 205 | "hex": node.hex, |
|
206 | 206 | "hgdate": lambda x: "%d %d" % x, |
|
207 | 207 | "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'), |
|
208 | 208 | "isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'), |
|
209 | 209 | "json": json, |
|
210 | 210 | "jsonescape": jsonescape, |
|
211 | 211 | "localdate": lambda x: (x[0], util.makedate()[1]), |
|
212 | 212 | "nonempty": nonempty, |
|
213 | 213 | "obfuscate": obfuscate, |
|
214 | 214 | "permissions": permissions, |
|
215 | 215 | "person": person, |
|
216 | 216 | "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"), |
|
217 | 217 | "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"), |
|
218 | 218 | "short": lambda x: x[:12], |
|
219 | 219 | "shortdate": util.shortdate, |
|
220 | 220 | "stringescape": lambda x: x.encode('string_escape'), |
|
221 | 221 | "stringify": stringify, |
|
222 | 222 | "strip": lambda x: x.strip(), |
|
223 | 223 | "stripdir": stripdir, |
|
224 | 224 | "tabindent": lambda x: indent(x, '\t'), |
|
225 | 225 | "urlescape": lambda x: urllib.quote(x), |
|
226 | 226 | "user": lambda x: util.shortuser(x), |
|
227 | 227 | "xmlescape": xmlescape, |
|
228 | 228 | } |
General Comments 0
You need to be logged in to leave comments.
Login now