From 8f0c8ee591fff99d7782525bed4a3e1b24c9088a Mon Sep 17 00:00:00 2001 From: Ned Jackson Lovely Date: Mon, 12 Mar 2012 20:38:06 -0400 Subject: [PATCH] Fix to #93, this time respecting whitespace Rejigger of title split to respect whitespace, also capitalize follow-on hyphenated words. --- jinja2/filters.py | 7 ++++++- jinja2/testsuite/filters.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/jinja2/filters.py b/jinja2/filters.py index 8dd6ff0..570263c 100644 --- a/jinja2/filters.py +++ b/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'): diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index 94bc02a..b037e24 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -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( -- 2.26.2