##// 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 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('@')
@@ -55,6 +61,12 b' def domain(author):'
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
@@ -83,6 +95,12 b' def fill(text, width):'
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:
@@ -90,6 +108,18 b' def firstline(text):'
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()
@@ -145,6 +175,9 b' def jsonescape(s):'
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
@@ -168,12 +201,30 b' def person(author):'
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)
@@ -182,6 +233,15 b' def stripdir(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('&', '&')
@@ -194,35 +254,35 b' def xmlescape(text):'
194 254 filters = {
195 255 "addbreaks": addbreaks,
196 256 "age": age,
197 "basename": os.path.basename,
198 "date": lambda x: util.datestr(x),
257 "basename": basename,
258 "date": datefilter,
199 259 "domain": domain,
200 "email": util.email,
201 "escape": lambda x: cgi.escape(x, True),
202 "fill68": lambda x: fill(x, width=68),
203 "fill76": lambda x: fill(x, width=76),
260 "email": email,
261 "escape": escape,
262 "fill68": fill68,
263 "fill76": fill76,
204 264 "firstline": firstline,
205 "hex": node.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": lambda x: (x[0], util.makedate()[1]),
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": util.shortdate,
220 "stringescape": lambda x: x.encode('string_escape'),
276 "rfc3339date": rfc3339date,
277 "rfc822date": rfc822date,
278 "short": short,
279 "shortdate": shortdate,
280 "stringescape": stringescape,
221 281 "stringify": stringify,
222 "strip": lambda x: x.strip(),
282 "strip": strip,
223 283 "stripdir": stripdir,
224 "tabindent": lambda x: indent(x, '\t'),
225 "urlescape": lambda x: urllib.quote(x),
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