Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 8f0c8ee591fff99d7782525bed4a3e1b24c9088a 1 parent 1305cfd
@njl authored
Showing with 16 additions and 1 deletion.
  1. +6 −1 jinja2/filters.py
  2. +10 −0 jinja2/testsuite/filters.py
View
7 jinja2/filters.py
@@ -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'):
View
10 jinja2/testsuite/filters.py
@@ -193,6 +193,16 @@ def test_string(self):
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(
Please sign in to comment.
Something went wrong with that request. Please try again.