app/django/template/defaultfilters.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
    60     """Capitalizes the first character of the value."""
    60     """Capitalizes the first character of the value."""
    61     return value and value[0].upper() + value[1:]
    61     return value and value[0].upper() + value[1:]
    62 capfirst.is_safe=True
    62 capfirst.is_safe=True
    63 capfirst = stringfilter(capfirst)
    63 capfirst = stringfilter(capfirst)
    64 
    64 
    65 _js_escapes = (
    65 _base_js_escapes = (
    66     ('\\', '\\\\'),
    66     ('\\', r'\x5C'),
    67     ('"', '\\"'),
    67     ('\'', r'\x27'),
    68     ("'", "\\'"),
    68     ('"', r'\x22'),
    69     ('\n', '\\n'),
    69     ('>', r'\x3E'),
    70     ('\r', '\\r'),
    70     ('<', r'\x3C'),
    71     ('\b', '\\b'),
    71     ('&', r'\x26'),
    72     ('\f', '\\f'),
    72     ('=', r'\x3D'),
    73     ('\t', '\\t'),
    73     ('-', r'\x2D'),
    74     ('\v', '\\v'),
    74     (';', r'\x3B')
    75     ('</', '<\\/'),
       
    76 )
    75 )
       
    76 
       
    77 # Escape every ASCII character with a value less than 32.
       
    78 _js_escapes = (_base_js_escapes +
       
    79                tuple([('%c' % z, '\\x%02X' % z) for z in range(32)]))
       
    80 
    77 def escapejs(value):
    81 def escapejs(value):
    78     """Backslash-escapes characters for use in JavaScript strings."""
    82     """Hex encodes characters for use in JavaScript strings."""
    79     for bad, good in _js_escapes:
    83     for bad, good in _js_escapes:
    80         value = value.replace(bad, good)
    84         value = value.replace(bad, good)
    81     return value
    85     return value
    82 escapejs = stringfilter(escapejs)
    86 escapejs = stringfilter(escapejs)
    83 
    87 
   640 def timesince(value, arg=None):
   644 def timesince(value, arg=None):
   641     """Formats a date as the time since that date (i.e. "4 days, 6 hours")."""
   645     """Formats a date as the time since that date (i.e. "4 days, 6 hours")."""
   642     from django.utils.timesince import timesince
   646     from django.utils.timesince import timesince
   643     if not value:
   647     if not value:
   644         return u''
   648         return u''
   645     if arg:
   649     try:
   646         return timesince(arg, value)
   650         if arg:
   647     return timesince(value)
   651             return timesince(value, arg)
       
   652         return timesince(value)
       
   653     except (ValueError, TypeError):
       
   654         return u''
   648 timesince.is_safe = False
   655 timesince.is_safe = False
   649 
   656 
   650 def timeuntil(value, arg=None):
   657 def timeuntil(value, arg=None):
   651     """Formats a date as the time until that date (i.e. "4 days, 6 hours")."""
   658     """Formats a date as the time until that date (i.e. "4 days, 6 hours")."""
   652     from django.utils.timesince import timesince
   659     from django.utils.timesince import timeuntil
   653     from datetime import datetime
   660     from datetime import datetime
   654     if not value:
   661     if not value:
   655         return u''
   662         return u''
   656     if arg:
   663     try:
   657         return timesince(arg, value)
   664         return timeuntil(value, arg)
   658     return timesince(datetime.now(), value)
   665     except (ValueError, TypeError):
       
   666         return u''
   659 timeuntil.is_safe = False
   667 timeuntil.is_safe = False
   660 
   668 
   661 ###################
   669 ###################
   662 # LOGIC           #
   670 # LOGIC           #
   663 ###################
   671 ###################