Fix to #93, this time respecting whitespace
authorNed Jackson Lovely <njl@njl.us>
Tue, 13 Mar 2012 00:38:06 +0000 (20:38 -0400)
committerNed Jackson Lovely <njl@njl.us>
Tue, 13 Mar 2012 00:38:06 +0000 (20:38 -0400)
Rejigger of title split to respect whitespace, also capitalize follow-on
hyphenated words.

jinja2/filters.py
jinja2/testsuite/filters.py

index 8dd6ff07fb9bb60d851db6d9c6717ca6f59c4ac2..570263c5d1f527438a7c43693ee2bf654cda8f1c 100644 (file)
@@ -176,7 +176,12 @@ def do_title(s):
     """Return a titlecased version of the value. I.e. words will start with
     uppercase letters, all remaining characters are lowercase.
     """
-    return soft_unicode(s).title()
+    rv = []
+    for item in re.compile(r'([-\s]+)(?u)').split(s):
+        if not item:
+            continue
+        rv.append(item[0].upper() + item[1:])
+    return ''.join(rv)
 
 
 def do_dictsort(value, case_sensitive=False, by='key'):
index 94bc02af65a09714e729f9ad2fa866884b9e92d7..b037e24006b35a61e16b765ea4d6ef94a324becc 100644 (file)
@@ -193,6 +193,16 @@ class FilterTestCase(JinjaTestCase):
     def test_title(self):
         tmpl = env.from_string('''{{ "foo bar"|title }}''')
         assert tmpl.render() == "Foo Bar"
+        tmpl = env.from_string('''{{ "foo's bar"|title }}''')
+        assert tmpl.render() == "Foo's Bar"
+        tmpl = env.from_string('''{{ "foo   bar"|title }}''')
+        assert tmpl.render() == "Foo   Bar"
+        tmpl = env.from_string('''{{ "f bar f"|title }}''')
+        assert tmpl.render() == "F Bar F"
+        tmpl = env.from_string('''{{ "foo-bar"|title }}''')
+        assert tmpl.render() == "Foo-Bar"
+        tmpl = env.from_string('''{{ "foo\tbar"|title }}''')
+        assert tmpl.render() == "Foo\tBar"
 
     def test_truncate(self):
         tmpl = env.from_string(