Permalink
Browse files

Fix to #93, this time respecting whitespace

Rejigger of title split to respect whitespace, also capitalize follow-on
hyphenated words.
  • Loading branch information...
1 parent 37bcc69 commit c4b6fd3f282d07a6c27db90e17b0abae8615fb02 @njl njl committed Mar 12, 2012
Showing with 9 additions and 5 deletions.
  1. +5 −5 jinja2/filters.py
  2. +4 −0 jinja2/testsuite/filters.py
View
@@ -177,12 +177,12 @@ def do_title(s):
uppercase letters, all remaining characters are lowercase.
"""
rv = []
- for word in soft_unicode(s).split(' '):
- if not word:
- rv.append(word)
+ for item in re.compile(r'([-\s]+)(?u)').split(s):
+ if not item:
+ rv.append(item)
continue
- rv.append(word[0].upper() + word[1:])
- return ' '.join(rv)
+ rv.append(item[0].upper() + item[1:])
+ return ''.join(rv)
def do_dictsort(value, case_sensitive=False, by='key'):
@@ -199,6 +199,10 @@ def test_title(self):
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(

0 comments on commit c4b6fd3

Please sign in to comment.