##// END OF EJS Templates
templatefilters: wrap all filters in dedicated functions...
Patrick Mezard -
r13590:1a752dcf default
parent child Browse files
Show More
@@ -44,6 +44,12 b' def age(date):'
44 if n >= 2 or s == 1:
44 if n >= 2 or s == 1:
45 return '%s ago' % fmt(t, n)
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 def domain(author):
53 def domain(author):
48 '''get domain of author, or empty string if none.'''
54 '''get domain of author, or empty string if none.'''
49 f = author.find('@')
55 f = author.find('@')
@@ -55,6 +61,12 b' def domain(author):'
55 author = author[:f]
61 author = author[:f]
56 return author
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 para_re = None
70 para_re = None
59 space_re = None
71 space_re = None
60
72
@@ -83,6 +95,12 b' def fill(text, width):'
83 return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest
95 return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest
84 for para, rest in findparas()])
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 def firstline(text):
104 def firstline(text):
87 '''return the first line of text'''
105 '''return the first line of text'''
88 try:
106 try:
@@ -90,6 +108,18 b' def firstline(text):'
90 except IndexError:
108 except IndexError:
91 return ''
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 def indent(text, prefix):
123 def indent(text, prefix):
94 '''indent each non-empty line of text after first with prefix.'''
124 '''indent each non-empty line of text after first with prefix.'''
95 lines = text.splitlines()
125 lines = text.splitlines()
@@ -145,6 +175,9 b' def jsonescape(s):'
145 s = s.replace(k, v)
175 s = s.replace(k, v)
146 return ''.join(_uescape(c) for c in s)
176 return ''.join(_uescape(c) for c in s)
147
177
178 def localdate(text):
179 return (text[0], util.makedate()[1])
180
148 def nonempty(str):
181 def nonempty(str):
149 return str or "(none)"
182 return str or "(none)"
150
183
@@ -168,12 +201,30 b' def person(author):'
168 return util.shortuser(author)
201 return util.shortuser(author)
169 return author[:f].rstrip()
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 def stringify(thing):
219 def stringify(thing):
172 '''turn nested template iterator into string.'''
220 '''turn nested template iterator into string.'''
173 if hasattr(thing, '__iter__') and not isinstance(thing, str):
221 if hasattr(thing, '__iter__') and not isinstance(thing, str):
174 return "".join([stringify(t) for t in thing if t is not None])
222 return "".join([stringify(t) for t in thing if t is not None])
175 return str(thing)
223 return str(thing)
176
224
225 def strip(text):
226 return text.strip()
227
177 def stripdir(text):
228 def stripdir(text):
178 '''Treat the text as path and strip a directory level, if possible.'''
229 '''Treat the text as path and strip a directory level, if possible.'''
179 dir = os.path.dirname(text)
230 dir = os.path.dirname(text)
@@ -182,6 +233,15 b' def stripdir(text):'
182 else:
233 else:
183 return dir
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 def xmlescape(text):
245 def xmlescape(text):
186 text = (text
246 text = (text
187 .replace('&', '&')
247 .replace('&', '&')
@@ -194,35 +254,35 b' def xmlescape(text):'
194 filters = {
254 filters = {
195 "addbreaks": addbreaks,
255 "addbreaks": addbreaks,
196 "age": age,
256 "age": age,
197 "basename": os.path.basename,
257 "basename": basename,
198 "date": lambda x: util.datestr(x),
258 "date": datefilter,
199 "domain": domain,
259 "domain": domain,
200 "email": util.email,
260 "email": email,
201 "escape": lambda x: cgi.escape(x, True),
261 "escape": escape,
202 "fill68": lambda x: fill(x, width=68),
262 "fill68": fill68,
203 "fill76": lambda x: fill(x, width=76),
263 "fill76": fill76,
204 "firstline": firstline,
264 "firstline": firstline,
205 "hex": node.hex,
265 "hex": hexfilter,
206 "hgdate": lambda x: "%d %d" % x,
266 "hgdate": hgdate,
207 "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'),
267 "isodate": isodate,
208 "isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'),
268 "isodatesec": isodatesec,
209 "json": json,
269 "json": json,
210 "jsonescape": jsonescape,
270 "jsonescape": jsonescape,
211 "localdate": lambda x: (x[0], util.makedate()[1]),
271 "localdate": localdate,
212 "nonempty": nonempty,
272 "nonempty": nonempty,
213 "obfuscate": obfuscate,
273 "obfuscate": obfuscate,
214 "permissions": permissions,
274 "permissions": permissions,
215 "person": person,
275 "person": person,
216 "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"),
276 "rfc3339date": rfc3339date,
217 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"),
277 "rfc822date": rfc822date,
218 "short": lambda x: x[:12],
278 "short": short,
219 "shortdate": util.shortdate,
279 "shortdate": shortdate,
220 "stringescape": lambda x: x.encode('string_escape'),
280 "stringescape": stringescape,
221 "stringify": stringify,
281 "stringify": stringify,
222 "strip": lambda x: x.strip(),
282 "strip": strip,
223 "stripdir": stripdir,
283 "stripdir": stripdir,
224 "tabindent": lambda x: indent(x, '\t'),
284 "tabindent": tabindent,
225 "urlescape": lambda x: urllib.quote(x),
285 "urlescape": urlescape,
226 "user": lambda x: util.shortuser(x),
286 "user": userfilter,
227 "xmlescape": xmlescape,
287 "xmlescape": xmlescape,
228 }
288 }
General Comments 0
You need to be logged in to leave comments. Login now