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