comparison mercurial/templatefilters.py @ 37227:9bcf096a2da2

templatefilters: declare input type as date where appropriate I'm not sure if the templateutil.date type can be a thing. Currently it's just a constant.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 18 Mar 2018 16:12:44 +0900
parents 08e042f0a67c
children 05db42732fce
comparison
equal deleted inserted replaced
37226:920589f52be9 37227:9bcf096a2da2
53 ("day", 3600 * 24, 'd'), 53 ("day", 3600 * 24, 'd'),
54 ("hour", 3600, 'h'), 54 ("hour", 3600, 'h'),
55 ("minute", 60, 'm'), 55 ("minute", 60, 'm'),
56 ("second", 1, 's')] 56 ("second", 1, 's')]
57 57
58 @templatefilter('age') 58 @templatefilter('age', intype=templateutil.date)
59 def age(date, abbrev=False): 59 def age(date, abbrev=False):
60 """Date. Returns a human-readable date/time difference between the 60 """Date. Returns a human-readable date/time difference between the
61 given date/time and the current date/time. 61 given date/time and the current date/time.
62 """ 62 """
63 63
193 """Any text. Convert a binary Mercurial node identifier into 193 """Any text. Convert a binary Mercurial node identifier into
194 its long hexadecimal representation. 194 its long hexadecimal representation.
195 """ 195 """
196 return node.hex(text) 196 return node.hex(text)
197 197
198 @templatefilter('hgdate') 198 @templatefilter('hgdate', intype=templateutil.date)
199 def hgdate(text): 199 def hgdate(text):
200 """Date. Returns the date as a pair of numbers: "1157407993 200 """Date. Returns the date as a pair of numbers: "1157407993
201 25200" (Unix timestamp, timezone offset). 201 25200" (Unix timestamp, timezone offset).
202 """ 202 """
203 return "%d %d" % text 203 return "%d %d" % text
204 204
205 @templatefilter('isodate') 205 @templatefilter('isodate', intype=templateutil.date)
206 def isodate(text): 206 def isodate(text):
207 """Date. Returns the date in ISO 8601 format: "2009-08-18 13:00 207 """Date. Returns the date in ISO 8601 format: "2009-08-18 13:00
208 +0200". 208 +0200".
209 """ 209 """
210 return dateutil.datestr(text, '%Y-%m-%d %H:%M %1%2') 210 return dateutil.datestr(text, '%Y-%m-%d %H:%M %1%2')
211 211
212 @templatefilter('isodatesec') 212 @templatefilter('isodatesec', intype=templateutil.date)
213 def isodatesec(text): 213 def isodatesec(text):
214 """Date. Returns the date in ISO 8601 format, including 214 """Date. Returns the date in ISO 8601 format, including
215 seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date 215 seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date
216 filter. 216 filter.
217 """ 217 """
301 Forward slashes are escaped twice to prevent web servers from prematurely 301 Forward slashes are escaped twice to prevent web servers from prematurely
302 unescaping them. For example, "@foo bar/baz" becomes "@foo%20bar%252Fbaz". 302 unescaping them. For example, "@foo bar/baz" becomes "@foo%20bar%252Fbaz".
303 """ 303 """
304 return urlreq.quote(text, safe='/@').replace('/', '%252F') 304 return urlreq.quote(text, safe='/@').replace('/', '%252F')
305 305
306 @templatefilter('rfc3339date') 306 @templatefilter('rfc3339date', intype=templateutil.date)
307 def rfc3339date(text): 307 def rfc3339date(text):
308 """Date. Returns a date using the Internet date format 308 """Date. Returns a date using the Internet date format
309 specified in RFC 3339: "2009-08-18T13:00:13+02:00". 309 specified in RFC 3339: "2009-08-18T13:00:13+02:00".
310 """ 310 """
311 return dateutil.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2") 311 return dateutil.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2")
312 312
313 @templatefilter('rfc822date') 313 @templatefilter('rfc822date', intype=templateutil.date)
314 def rfc822date(text): 314 def rfc822date(text):
315 """Date. Returns a date using the same format used in email 315 """Date. Returns a date using the same format used in email
316 headers: "Tue, 18 Aug 2009 13:00:13 +0200". 316 headers: "Tue, 18 Aug 2009 13:00:13 +0200".
317 """ 317 """
318 return dateutil.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2") 318 return dateutil.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2")
333 """ 333 """
334 if label: 334 if label:
335 return label[0:1].upper() 335 return label[0:1].upper()
336 return ' ' 336 return ' '
337 337
338 @templatefilter('shortdate') 338 @templatefilter('shortdate', intype=templateutil.date)
339 def shortdate(text): 339 def shortdate(text):
340 """Date. Returns a date like "2006-09-18".""" 340 """Date. Returns a date like "2006-09-18"."""
341 return dateutil.shortdate(text) 341 return dateutil.shortdate(text)
342 342
343 @templatefilter('slashpath', intype=bytes) 343 @templatefilter('slashpath', intype=bytes)