Browse Source

Merge pull request #63 from runejuhl/issue/62

title/1 even more Django-y
Evan Miller 12 years ago
parent
commit
b17769422c
2 changed files with 13 additions and 3 deletions
  1. 7 3
      src/erlydtl_filters.erl
  2. 6 0
      tests/src/erlydtl_unittests.erl

+ 7 - 3
src/erlydtl_filters.erl

@@ -797,7 +797,7 @@ timeuntil(Date,Comparison) ->
 title(Input) when is_binary(Input) ->
 title(Input) when is_binary(Input) ->
     title(binary_to_list(Input));
     title(binary_to_list(Input));
 title(Input) when is_list(Input) ->
 title(Input) when is_list(Input) ->
-    title(Input, []).
+    title(lower(Input), []).
 
 
 %% @doc Truncates a string after a certain number of characters.
 %% @doc Truncates a string after a certain number of characters.
 truncatechars(_Input, Max) when Max =< 0 ->
 truncatechars(_Input, Max) when Max =< 0 ->
@@ -1029,8 +1029,12 @@ title([], Acc) ->
     lists:reverse(Acc);
     lists:reverse(Acc);
 title([Char | Rest], [] = Acc) when Char >= $a, Char =< $z ->
 title([Char | Rest], [] = Acc) when Char >= $a, Char =< $z ->
     title(Rest, [Char + ($A - $a) | Acc]);
     title(Rest, [Char + ($A - $a) | Acc]);
-title([Char | Rest], [Sep|_] = Acc) when Char >= $a, Char =< $z, not (Sep >= $a andalso Sep =< $z),
-                                         not (Sep >= $A andalso Sep =< $Z) ->
+title([Char | Rest], [Sep|[Sep2|_Other]] = Acc)
+  when Char >= $a, Char =< $z,
+       not (Sep >= $a andalso Sep =< $z),
+       not (Sep >= $A andalso Sep =< $Z),
+       not (Sep >= $0 andalso Sep =< $9),
+       not (Sep =:= $' andalso (Sep2 >= $a andalso Sep2 =< $z)) ->
     title(Rest, [Char + ($A - $a) | Acc]);
     title(Rest, [Char + ($A - $a) | Acc]);
 title([Char | Rest], Acc) ->
 title([Char | Rest], Acc) ->
     title(Rest, [Char | Acc]).
     title(Rest, [Char | Acc]).

+ 6 - 0
tests/src/erlydtl_unittests.erl

@@ -826,6 +826,12 @@ tests() ->
                 {"|title (wacky separators)",
                 {"|title (wacky separators)",
                     <<"{{ \"my-title!case\"|title }}">>, [],
                     <<"{{ \"my-title!case\"|title }}">>, [],
                     <<"My-Title!Case">>},
                     <<"My-Title!Case">>},
+                {"|title (numbers)",
+                    <<"{{ \"my-title123CaSe\"|title }}">>, [],
+                    <<"My-Title123case">>},
+                {"|title (Irish names)",
+                    <<"{{ \"who's o'malley?\"|title }}">>, [],
+                    <<"Who's O'Malley?">>},
                 {"|truncatechars:0",
                 {"|truncatechars:0",
                     <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
                     <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
                     <<"">>},
                     <<"">>},