Commits

David Chambers committed 5a63d85

Further refactored `display_date_or_time`.

Comments (0)

Files changed (1)

templatetags/mango_extras.py

     Python does not provide a directive for outputting ordinal dates (1st, 2nd, 3rd, etc.).
     The display_date and display_time filters support ordinal dates via the `%o` directive.
     '''
-    format = re.sub(r'(?<!%)%p', '{{%p|lower}}', format)
-    format = re.sub(r'(?<!%)%P', '{{%p|upper}}', format)
-    format = re.sub(r'(?<!%)%i', '{{%I|unpad}}', format)
-    format = re.sub(r'(?<!%)%d', '{{%d|unpad}}', format)
-    format = re.sub(r'(?<!%)%D', '%d', format) # default
-    format = re.sub(r'(?<!%)%o', '{{%d|ordinal}}', format)
+    replacements = (
+        (r'(?<!%)%p', '{{%p|lower}}'),
+        (r'(?<!%)%P', '{{%p|upper}}'),
+        (r'(?<!%)%i', '{{%I|unpad}}'),
+        (r'(?<!%)%d', '{{%d|unpad}}'),
+        (r'(?<!%)%D', '%d'), # default
+        (r'(?<!%)%o', '{{%d|ordinal}}'),
+    )
+    for pattern, repl in replacements:
+        format = re.sub(pattern, repl, format)
 
     def ordinal(n):
         if 10 < n < 14: return u'%sth' % n