##// END OF EJS Templates
templatefilters: declare input type as date where appropriate...
Yuya Nishihara -
r37244:9bcf096a default
parent child Browse files
Show More
@@ -333,7 +333,7 b' class templatefilter(_templateregistrarb'
333 The first string argument is used also in online help.
333 The first string argument is used also in online help.
334
334
335 Optional argument 'intype' defines the type of the input argument,
335 Optional argument 'intype' defines the type of the input argument,
336 which should be (bytes, int, or None for any.)
336 which should be (bytes, int, templateutil.date, or None for any.)
337
337
338 'templatefilter' instance in example above can be used to
338 'templatefilter' instance in example above can be used to
339 decorate multiple functions.
339 decorate multiple functions.
@@ -55,7 +55,7 b' agescales = [("year", 3600 * 24 * 365, \''
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.
@@ -195,21 +195,21 b' def hexfilter(text):'
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
@@ -303,14 +303,14 b' def revescape(text):'
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".
@@ -335,7 +335,7 b' def shortbisect(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)
@@ -26,6 +26,11 b' class ResourceUnavailable(error.Abort):'
26 class TemplateNotFound(error.Abort):
26 class TemplateNotFound(error.Abort):
27 pass
27 pass
28
28
29 # stub for representing a date type; may be a real date type that can
30 # provide a readable string value
31 class date(object):
32 pass
33
29 class hybrid(object):
34 class hybrid(object):
30 """Wrapper for list or dict to support legacy template
35 """Wrapper for list or dict to support legacy template
31
36
@@ -361,6 +366,7 b' def evalstringliteral(context, mapping, '
361 _unwrapfuncbytype = {
366 _unwrapfuncbytype = {
362 None: _unwrapvalue,
367 None: _unwrapvalue,
363 bytes: stringify,
368 bytes: stringify,
369 date: unwrapdate,
364 int: unwrapinteger,
370 int: unwrapinteger,
365 }
371 }
366
372
@@ -2806,7 +2806,8 b' Behind the scenes, this would throw Type'
2806 Behind the scenes, this will throw a ValueError
2806 Behind the scenes, this will throw a ValueError
2807
2807
2808 $ hg log -l 3 --template 'line: {desc|shortdate}\n'
2808 $ hg log -l 3 --template 'line: {desc|shortdate}\n'
2809 abort: template filter 'shortdate' is not compatible with keyword 'desc'
2809 hg: parse error: invalid date: 'Modify, add, remove, rename'
2810 (template filter 'shortdate' is not compatible with keyword 'desc')
2810 [255]
2811 [255]
2811
2812
2812 Behind the scenes, this would throw AttributeError without intype=bytes
2813 Behind the scenes, this would throw AttributeError without intype=bytes
@@ -2827,11 +2828,13 b' Behind the scenes, this will throw Value'
2827 [255]
2828 [255]
2828
2829
2829 $ hg tip -T '{author|email|shortdate}\n'
2830 $ hg tip -T '{author|email|shortdate}\n'
2830 abort: template filter 'shortdate' is not compatible with keyword 'author'
2831 hg: parse error: invalid date: 'test'
2832 (template filter 'shortdate' is not compatible with keyword 'author')
2831 [255]
2833 [255]
2832
2834
2833 $ hg tip -T '{get(extras, "branch")|shortdate}\n'
2835 $ hg tip -T '{get(extras, "branch")|shortdate}\n'
2834 abort: incompatible use of template filter 'shortdate'
2836 hg: parse error: invalid date: 'default'
2837 (incompatible use of template filter 'shortdate')
2835 [255]
2838 [255]
2836
2839
2837 Error in nested template:
2840 Error in nested template:
General Comments 0
You need to be logged in to leave comments. Login now