Browse Source

Merge pull request #20 from nyaray/master

truncatechars template filter from django dev.
Evan Miller 13 years ago
parent
commit
3330835354
3 changed files with 26 additions and 1 deletions
  1. 1 1
      README.markdown
  2. 16 0
      src/erlydtl_filters.erl
  3. 9 0
      tests/src/erlydtl_unittests.erl

+ 1 - 1
README.markdown

@@ -7,7 +7,7 @@ ErlyDTL compiles Django Template Language to Erlang bytecode.
 
 
 _Unsupported tags_: csrf_token, ifchanged, regroup, url
 _Unsupported tags_: csrf_token, ifchanged, regroup, url
 
 
-*Supported filters*: add, addslashes, capfirst, center, cut, date, default, default_if_none, dictsort, dictsortreversed, divisibleby, escape, escapejs, filesizeformat, first, fix_ampersands, floatformat, force_escape, format_integer, format_number, get_digit, iriencode, join, last, length, length_is, linebreaks, linebreaksbr, linenumbers, ljust, lower, make_list, phonenumeric, pluralize, pprint, random, random_num, random_range, removetags, rjust, safe, safeseq, slice, slugify, stringformat, striptags, time, timesince, timeuntil, title, truncatewords, truncatewords_html, unordered_list, upper, urlencode, urlize, urlizetrunc, wordcount, wordwrap, yesno
+*Supported filters*: add, addslashes, capfirst, center, cut, date, default, default_if_none, dictsort, dictsortreversed, divisibleby, escape, escapejs, filesizeformat, first, fix_ampersands, floatformat, force_escape, format_integer, format_number, get_digit, iriencode, join, last, length, length_is, linebreaks, linebreaksbr, linenumbers, ljust, lower, make_list, phonenumeric, pluralize, pprint, random, random_num, random_range, removetags, rjust, safe, safeseq, slice, slugify, stringformat, striptags, time, timesince, timeuntil, title, truncatechars, truncatewords, truncatewords_html, unordered_list, upper, urlencode, urlize, urlizetrunc, wordcount, wordwrap, yesno
 
 
 _Unsupported filters_: _none_
 _Unsupported filters_: _none_
 
 

+ 16 - 0
src/erlydtl_filters.erl

@@ -101,6 +101,7 @@
         timeuntil/1,
         timeuntil/1,
         timeuntil/2,
         timeuntil/2,
         title/1,
         title/1,
+        truncatechars/2,
         truncatewords/2,
         truncatewords/2,
         truncatewords_html/2,
         truncatewords_html/2,
         unordered_list/1,
         unordered_list/1,
@@ -748,6 +749,14 @@ title(Input) when is_binary(Input) ->
 title(Input) when is_list(Input) ->
 title(Input) when is_list(Input) ->
     title(Input, []).
     title(Input, []).
 
 
+%% @doc Truncates a string after a certain number of characters.
+truncatechars(_Input, Max) when Max =< 0 ->
+    "";
+truncatechars(Input, Max) when is_binary(Input) ->
+    list_to_binary(truncatechars(binary_to_list(Input), Max));
+truncatechars(Input, Max) ->
+    truncatechars(Input, Max, []).
+
 %% @doc Truncates a string after a certain number of words.
 %% @doc Truncates a string after a certain number of words.
 truncatewords(_Input, Max) when Max =< 0 ->
 truncatewords(_Input, Max) when Max =< 0 ->
     "";
     "";
@@ -975,6 +984,13 @@ title([Char | Rest], [$\  |_] = Acc) when Char >= $a, Char =< $z ->
 title([Char | Rest], Acc) ->
 title([Char | Rest], Acc) ->
     title(Rest, [Char | Acc]).
     title(Rest, [Char | Acc]).
 
 
+truncatechars([], _CharsLeft, Acc) ->
+    lists:reverse(Acc);
+truncatechars(_Input, 0, Acc) ->
+    lists:reverse("..." ++ Acc);
+truncatechars([C|Rest], CharsLeft, Acc) ->
+    truncatechars(Rest, CharsLeft - 1, [C|Acc]).
+
 truncatewords([], _WordsLeft, Acc) ->
 truncatewords([], _WordsLeft, Acc) ->
     lists:reverse(Acc);
     lists:reverse(Acc);
 truncatewords(_Input, 0, Acc) ->
 truncatewords(_Input, 0, Acc) ->

+ 9 - 0
tests/src/erlydtl_unittests.erl

@@ -766,6 +766,15 @@ tests() ->
                 {"|title (pre-formatted)",
                 {"|title (pre-formatted)",
                     <<"{{ \"My Title Case\"|title }}">>, [],
                     <<"{{ \"My Title Case\"|title }}">>, [],
                     <<"My Title Case">>},
                     <<"My Title Case">>},
+                {"|truncatechars:0",
+                    <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
+                    <<"">>},
+                {"|truncatechars:11",
+                    <<"{{ var1|truncatechars:11 }}">>, [{var1, "Truncate Me Please"}],
+                    <<"Truncate Me...">>},
+                {"|truncatechars:17",
+                    <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
+                    <<"Don't Truncate Me">>},
                 {"|truncatewords:0",
                 {"|truncatewords:0",
                     <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
                     <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
                     <<"">>},
                     <<"">>},