Skip to content
This repository

Fix #93 by implementing a title algorithm, instead of relying on stdlib's lame version... #102

Closed
wants to merge 3 commits into from

2 participants

Ned Jackson Lovely Armin Ronacher
Ned Jackson Lovely
njl commented March 12, 2012

The stdlib title function provides a fundamentally correct and
completely useless version of the title function, that defines
words as things seperated by not letters. This changes the semantics to
make words things that are seperated by spaces.

Ned Jackson Lovely Fixes Issue #93 by implementing a title algorithm
The stdlib title function provides a fundamentally correct and
completely useless version of the title function, that defines
words as things seperated by not letters. This changes the semantics to
make words things that are seperated by spaces.
7fe70fd
Armin Ronacher
Owner

This will destroy different kinds of whitespace. This is better solved with unicode regular expressions.

Armin Ronacher
Owner

Eg:

for item in re.compile(r'(\s+)(?u)').split(...):
  #item is either whitespace or item
  ...
added some commits March 12, 2012
Ned Jackson Lovely Fix #77 by adding note to switching document
Explaining that {% empty %} in Django loops is {% else %} in Jinja2.
37bcc69
Ned Jackson Lovely Fix to #93, this time respecting whitespace
Rejigger of title split to respect whitespace, also capitalize follow-on
hyphenated words.
c4b6fd3
Ned Jackson Lovely njl closed this March 12, 2012
Ned Jackson Lovely
njl commented March 12, 2012

Closed, separate, cleaner pull is #103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Mar 12, 2012
Ned Jackson Lovely Fixes Issue #93 by implementing a title algorithm
The stdlib title function provides a fundamentally correct and
completely useless version of the title function, that defines
words as things seperated by not letters. This changes the semantics to
make words things that are seperated by spaces.
7fe70fd
Ned Jackson Lovely Fix #77 by adding note to switching document
Explaining that {% empty %} in Django loops is {% else %} in Jinja2.
37bcc69
Ned Jackson Lovely Fix to #93, this time respecting whitespace
Rejigger of title split to respect whitespace, also capitalize follow-on
hyphenated words.
c4b6fd3
This page is out of date. Refresh to see the latest.
22  docs/switching.rst
Source Rendered
@@ -177,9 +177,25 @@ operator.  Here are some examples::
177 177
 Loops
178 178
 ~~~~~
179 179
 
180  
-For loops work very similar to Django, the only incompatibility is that in
181  
-Jinja2 the special variable for the loop context is called `loop` and not
182  
-`forloop` like in Django.
  180
+For loops work very similar to Django. Notably, in Jinja2 the special variable for 
  181
+the loop context is called `loop` and not `forloop` like in Django.
  182
+
  183
+In addition, the Django `empty` argument is called `else` in Jinja2. For example, the
  184
+Django template::
  185
+
  186
+    {% for item in items %}
  187
+        {{item}}
  188
+    {% empty %}
  189
+        No items!
  190
+    {% endfor %}
  191
+
  192
+would be handled in Flask as::
  193
+
  194
+    {% for item in items %}
  195
+        {{item}}
  196
+    {% else %}
  197
+        No items!
  198
+    {% endfor %}
183 199
 
184 200
 Cycle
185 201
 ~~~~~
8  jinja2/filters.py
@@ -176,7 +176,13 @@ def do_title(s):
176 176
     """Return a titlecased version of the value. I.e. words will start with
177 177
     uppercase letters, all remaining characters are lowercase.
178 178
     """
179  
-    return soft_unicode(s).title()
  179
+    rv = []
  180
+    for item in re.compile(r'([-\s]+)(?u)').split(s):
  181
+        if not item:
  182
+            rv.append(item)
  183
+            continue
  184
+        rv.append(item[0].upper() + item[1:])
  185
+    return ''.join(rv)
180 186
 
181 187
 
182 188
 def do_dictsort(value, case_sensitive=False, by='key'):
10  jinja2/testsuite/filters.py
@@ -193,6 +193,16 @@ def test_string(self):
193 193
     def test_title(self):
194 194
         tmpl = env.from_string('''{{ "foo bar"|title }}''')
195 195
         assert tmpl.render() == "Foo Bar"
  196
+        tmpl = env.from_string('''{{ "foo's bar"|title }}''')
  197
+        assert tmpl.render() == "Foo's Bar"
  198
+        tmpl = env.from_string('''{{ "foo   bar"|title }}''')
  199
+        assert tmpl.render() == "Foo   Bar"
  200
+        tmpl = env.from_string('''{{ "f bar f"|title }}''')
  201
+        assert tmpl.render() == "F Bar F"
  202
+        tmpl = env.from_string('''{{ "foo-bar"|title }}''')
  203
+        assert tmpl.render() == "Foo-Bar"
  204
+        tmpl = env.from_string('''{{ "foo\tbar"|title }}''')
  205
+        assert tmpl.render() == "Foo\tBar"
196 206
 
197 207
     def test_truncate(self):
198 208
         tmpl = env.from_string(
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.