Browse Source

change erlydtl filters handle unicode input

Erik Mellegård 11 years ago
parent
commit
a3fa62f33c
1 changed files with 40 additions and 40 deletions
  1. 40 40
      src/erlydtl_filters.erl

+ 40 - 40
src/erlydtl_filters.erl

@@ -159,9 +159,9 @@
 add(LHS, RHS) when is_number(LHS), is_number(RHS) ->
 add(LHS, RHS) when is_number(LHS), is_number(RHS) ->
     LHS + RHS;
     LHS + RHS;
 add(LHS, RHS) when is_binary(LHS) ->
 add(LHS, RHS) when is_binary(LHS) ->
-    add(binary_to_list(LHS), RHS);
+    add(unicode:characters_to_list(LHS), RHS);
 add(LHS, RHS) when is_binary(RHS) ->
 add(LHS, RHS) when is_binary(RHS) ->
-    add(LHS, binary_to_list(RHS));
+    add(LHS, unicode:characters_to_list(RHS));
 add(LHS, RHS) when is_list(LHS), is_list(RHS) ->
 add(LHS, RHS) when is_list(LHS), is_list(RHS) ->
     case {to_numeric(LHS), to_numeric(RHS)} of
     case {to_numeric(LHS), to_numeric(RHS)} of
 	{{number, LHSNum}, {number, RHSNum}} ->
 	{{number, LHSNum}, {number, RHSNum}} ->
@@ -204,7 +204,7 @@ to_numeric(List) ->
  
  
 %% @doc Adds slashes before quotes.
 %% @doc Adds slashes before quotes.
 addslashes(Input) when is_binary(Input) ->
 addslashes(Input) when is_binary(Input) ->
-    addslashes(binary_to_list(Input));
+    addslashes(unicode:characters_to_list(Input));
 addslashes(Input) when is_list(Input) ->
 addslashes(Input) when is_list(Input) ->
     addslashes(Input, []).
     addslashes(Input, []).
  
  
@@ -214,21 +214,21 @@ capfirst([H|T]) when H >= $a andalso H =< $z ->
 capfirst(Other) when is_list(Other) ->
 capfirst(Other) when is_list(Other) ->
     Other;
     Other;
 capfirst(<<Byte:8/integer, Binary/binary>>) when Byte >= $a andalso Byte =< $z ->
 capfirst(<<Byte:8/integer, Binary/binary>>) when Byte >= $a andalso Byte =< $z ->
-    [(Byte + $A - $a)|binary_to_list(Binary)];
+    [(Byte + $A - $a)|unicode:characters_to_list(Binary)];
 capfirst(Other) when is_binary(Other) ->
 capfirst(Other) when is_binary(Other) ->
     Other.
     Other.
  
  
 %% @doc Centers the value in a field of a given width.
 %% @doc Centers the value in a field of a given width.
 center(Input, Number) when is_binary(Input) ->
 center(Input, Number) when is_binary(Input) ->
-    list_to_binary(center(binary_to_list(Input), Number));
+    unicode:characters_to_binary(center(unicode:characters_to_list(Input), Number));
 center(Input, Number) when is_list(Input) ->
 center(Input, Number) when is_list(Input) ->
     string:centre(Input, Number).
     string:centre(Input, Number).
  
  
 %% @doc Removes all values of arg from the given string.
 %% @doc Removes all values of arg from the given string.
 cut(Input, Arg) when is_binary(Arg) ->
 cut(Input, Arg) when is_binary(Arg) ->
-    cut(Input, binary_to_list(Arg));
+    cut(Input, unicode:characters_to_list(Arg));
 cut(Input, Arg) when is_binary(Input) ->
 cut(Input, Arg) when is_binary(Input) ->
-    cut(binary_to_list(Input), Arg);
+    cut(unicode:characters_to_list(Input), Arg);
 cut(Input, [Char]) when is_list(Input) ->
 cut(Input, [Char]) when is_list(Input) ->
     cut(Input, Char, []).
     cut(Input, Char, []).
  
  
@@ -238,7 +238,7 @@ date(Input) ->
 
 
 %% @doc Formats a date according to the given format.
 %% @doc Formats a date according to the given format.
 date(Input, FormatStr) when is_binary(Input) ->
 date(Input, FormatStr) when is_binary(Input) ->
-    list_to_binary(date(binary_to_list(Input), FormatStr));
+    unicode:characters_to_binary(date(unicode:characters_to_list(Input), FormatStr));
 date({{_,_,_} = Date,{_,_,_} = Time}, FormatStr) ->
 date({{_,_,_} = Date,{_,_,_} = Time}, FormatStr) ->
     erlydtl_dateformat:format({Date, Time}, FormatStr);
     erlydtl_dateformat:format({Date, Time}, FormatStr);
 date({_,_,_} = Date, FormatStr) ->
 date({_,_,_} = Date, FormatStr) ->
@@ -284,11 +284,11 @@ dictsortreversed(DictList, Key) ->
 
 
 %% @doc Returns `true' if the value is divisible by the argument.
 %% @doc Returns `true' if the value is divisible by the argument.
 divisibleby(Input, Divisor) when is_binary(Input) ->
 divisibleby(Input, Divisor) when is_binary(Input) ->
-    divisibleby(binary_to_list(Input), Divisor);
+    divisibleby(unicode:characters_to_list(Input), Divisor);
 divisibleby(Input, Divisor) when is_list(Input) ->
 divisibleby(Input, Divisor) when is_list(Input) ->
     divisibleby(list_to_integer(Input), Divisor);
     divisibleby(list_to_integer(Input), Divisor);
 divisibleby(Input, Divisor) when is_binary(Divisor) ->
 divisibleby(Input, Divisor) when is_binary(Divisor) ->
-    divisibleby(Input, binary_to_list(Divisor));
+    divisibleby(Input, unicode:characters_to_list(Divisor));
 divisibleby(Input, Divisor) when is_list(Divisor) ->
 divisibleby(Input, Divisor) when is_list(Divisor) ->
     divisibleby(Input, list_to_integer(Divisor));
     divisibleby(Input, list_to_integer(Divisor));
 divisibleby(Input, Divisor) when is_integer(Input), is_integer(Divisor) ->
 divisibleby(Input, Divisor) when is_integer(Input), is_integer(Divisor) ->
@@ -296,13 +296,13 @@ divisibleby(Input, Divisor) when is_integer(Input), is_integer(Divisor) ->
 
 
 %% @doc Escapes characters for use in JavaScript strings.
 %% @doc Escapes characters for use in JavaScript strings.
 escapejs(Input) when is_binary(Input) ->
 escapejs(Input) when is_binary(Input) ->
-    escapejs(binary_to_list(Input));
+    escapejs(unicode:characters_to_list(Input));
 escapejs(Input) when is_list(Input) ->
 escapejs(Input) when is_list(Input) ->
     escapejs(Input, []).
     escapejs(Input, []).
 
 
 %% @doc Format the value like a human-readable file size.
 %% @doc Format the value like a human-readable file size.
 filesizeformat(Input) when is_binary(Input) ->
 filesizeformat(Input) when is_binary(Input) ->
-    filesizeformat(binary_to_list(Input));
+    filesizeformat(unicode:characters_to_list(Input));
 filesizeformat(Input) when is_list(Input) ->
 filesizeformat(Input) when is_list(Input) ->
     filesizeformat(list_to_integer(Input));
     filesizeformat(list_to_integer(Input));
 filesizeformat(Bytes) when is_integer(Bytes), Bytes >= ?GIGABYTE->
 filesizeformat(Bytes) when is_integer(Bytes), Bytes >= ?GIGABYTE->
@@ -329,7 +329,7 @@ fix_ampersands(Input) when is_list(Input) ->
 %% @doc When used without an argument, rounds a floating-point number to one decimal place
 %% @doc When used without an argument, rounds a floating-point number to one decimal place
 %% -- but only if there's a decimal part to be displayed
 %% -- but only if there's a decimal part to be displayed
 floatformat(Number, Place) when is_binary(Number) ->
 floatformat(Number, Place) when is_binary(Number) ->
-    floatformat(binary_to_list(Number), Place);
+    floatformat(unicode:characters_to_list(Number), Place);
 floatformat(Number, Place) ->
 floatformat(Number, Place) ->
     floatformat_io(Number, cast_to_integer(Place)).
     floatformat_io(Number, cast_to_integer(Place)).
 
 
@@ -380,11 +380,11 @@ format_number(Input) ->
 
 
 %% @doc Given a whole number, returns the requested digit, where 1 is the right-most digit.
 %% @doc Given a whole number, returns the requested digit, where 1 is the right-most digit.
 get_digit(Input, Digit) when is_binary(Input) ->
 get_digit(Input, Digit) when is_binary(Input) ->
-    get_digit(binary_to_list(Input), Digit);
+    get_digit(unicode:characters_to_list(Input), Digit);
 get_digit(Input, Digit) when is_integer(Input) ->
 get_digit(Input, Digit) when is_integer(Input) ->
     get_digit(integer_to_list(Input), Digit);
     get_digit(integer_to_list(Input), Digit);
 get_digit(Input, Digit) when is_binary(Digit) ->
 get_digit(Input, Digit) when is_binary(Digit) ->
-    get_digit(Input, binary_to_list(Digit));
+    get_digit(Input, unicode:characters_to_list(Digit));
 get_digit(Input, Digit) when is_list(Digit) ->
 get_digit(Input, Digit) when is_list(Digit) ->
     get_digit(Input, list_to_integer(Digit));
     get_digit(Input, list_to_integer(Digit));
 get_digit(Input, Digit) when Digit > erlang:length(Input) ->
 get_digit(Input, Digit) when Digit > erlang:length(Input) ->
@@ -427,7 +427,7 @@ length_is(Input, Number) when is_list(Input), is_list(Number) ->
 
 
 %% @doc Replaces line breaks in plain text with appropriate HTML
 %% @doc Replaces line breaks in plain text with appropriate HTML
 linebreaks(Input) when is_binary(Input) ->
 linebreaks(Input) when is_binary(Input) ->
-    linebreaks(binary_to_list(Input),[]);
+    linebreaks(unicode:characters_to_list(Input),[]);
 linebreaks(Input) ->
 linebreaks(Input) ->
     linebreaks(Input,[]).
     linebreaks(Input,[]).
 
 
@@ -456,7 +456,7 @@ linebreaksbr(Input) ->
 
 
 %% @doc Displays text with line numbers.
 %% @doc Displays text with line numbers.
 linenumbers(Input) when is_binary(Input) ->
 linenumbers(Input) when is_binary(Input) ->
-    linenumbers(binary_to_list(Input));
+    linenumbers(unicode:characters_to_list(Input));
 linenumbers(Input) when is_list(Input) ->
 linenumbers(Input) when is_list(Input) ->
     linenumbers_io(Input, [], 1).
     linenumbers_io(Input, [], 1).
 
 
@@ -471,7 +471,7 @@ linenumbers_io([H|T], Acc, LineNumber) ->
 
 
 %% @doc Left-aligns the value in a field of a given width.
 %% @doc Left-aligns the value in a field of a given width.
 ljust(Input, Number) when is_binary(Input) ->
 ljust(Input, Number) when is_binary(Input) ->
-    list_to_binary(ljust(binary_to_list(Input), Number));
+    unicode:characters_to_binary(ljust(unicode:characters_to_list(Input), Number));
 ljust(Input, Number) when is_list(Input) ->
 ljust(Input, Number) when is_list(Input) ->
     string:left(Input, Number).
     string:left(Input, Number).
 
 
@@ -485,19 +485,19 @@ lower(Input) ->
 %% For a string, it's a list of characters.
 %% For a string, it's a list of characters.
 %% Added this for DTL compatibility, but since strings are lists in Erlang, no need for this.
 %% Added this for DTL compatibility, but since strings are lists in Erlang, no need for this.
 make_list(Input) when is_binary(Input) ->
 make_list(Input) when is_binary(Input) ->
-    make_list(binary_to_list(Input));
+    make_list(unicode:characters_to_list(Input));
 make_list(Input) ->
 make_list(Input) ->
     unjoin(Input,"").
     unjoin(Input,"").
 
 
 %% @doc Converts a phone number (possibly containing letters) to its numerical equivalent.
 %% @doc Converts a phone number (possibly containing letters) to its numerical equivalent.
 phone2numeric(Input) when is_binary(Input) ->
 phone2numeric(Input) when is_binary(Input) ->
-    phone2numeric(binary_to_list(Input));
+    phone2numeric(unicode:characters_to_list(Input));
 phone2numeric(Input) when is_list(Input) ->
 phone2numeric(Input) when is_list(Input) ->
     phone2numeric(Input, []).
     phone2numeric(Input, []).
 
 
 %% @doc Returns a plural suffix if the value is not 1. By default, this suffix is 's'.
 %% @doc Returns a plural suffix if the value is not 1. By default, this suffix is 's'.
 pluralize(Number, Suffix) when is_binary(Suffix) ->
 pluralize(Number, Suffix) when is_binary(Suffix) ->
-    pluralize_io(Number, binary_to_list(Suffix) );
+    pluralize_io(Number, unicode:characters_to_list(Suffix) );
 pluralize(Number, Suffix) when is_list(Suffix) ->
 pluralize(Number, Suffix) when is_list(Suffix) ->
     pluralize_io(Number, Suffix).
     pluralize_io(Number, Suffix).
 
 
@@ -551,9 +551,9 @@ random_range(Start, End) when End >= Start ->
     lists:flatten(io_lib:format("~B",[Num])).
     lists:flatten(io_lib:format("~B",[Num])).
 
 
 removetags(Input, Tags) when is_binary(Input) ->
 removetags(Input, Tags) when is_binary(Input) ->
-    removetags(binary_to_list(Input), Tags);
+    removetags(unicode:characters_to_list(Input), Tags);
 removetags(Input, Tags) when is_binary(Tags) ->
 removetags(Input, Tags) when is_binary(Tags) ->
-    removetags(Input, binary_to_list(Tags));
+    removetags(Input, unicode:characters_to_list(Tags));
 removetags(Input, Tags) ->
 removetags(Input, Tags) ->
     TagList = string:tokens(Tags," "),
     TagList = string:tokens(Tags," "),
     TagListString = string:join(TagList,"|"),
     TagListString = string:join(TagList,"|"),
@@ -563,13 +563,13 @@ removetags(Input, Tags) ->
 
 
 %% @doc Right-aligns the value in a field of a given width.
 %% @doc Right-aligns the value in a field of a given width.
 rjust(Input, Number) when is_binary(Input) ->
 rjust(Input, Number) when is_binary(Input) ->
-    list_to_binary(rjust(binary_to_list(Input), Number));
+    unicode:characters_to_binary(rjust(unicode:characters_to_list(Input), Number));
 rjust(Input, Number) ->
 rjust(Input, Number) ->
     string:right(Input, Number).
     string:right(Input, Number).
 
 
 %% @doc Returns a slice of the list.
 %% @doc Returns a slice of the list.
 slice(Input, Index) when is_binary(Input) ->
 slice(Input, Index) when is_binary(Input) ->
-    erlydtl_slice:slice(binary_to_list(Input), Index);
+    erlydtl_slice:slice(unicode:characters_to_list(Input), Index);
 slice(Input, Index) when is_list(Input) ->
 slice(Input, Index) when is_list(Input) ->
     erlydtl_slice:slice(Input, Index).
     erlydtl_slice:slice(Input, Index).
 
 
@@ -577,9 +577,9 @@ slice(Input, Index) when is_list(Input) ->
 %% ([#0-\s+]?)([0-9\*]+)?(\.?)([0-9]?)([diouxXeEfFgGcrs])
 %% ([#0-\s+]?)([0-9\*]+)?(\.?)([0-9]?)([diouxXeEfFgGcrs])
 %% @doc Returns a formatted string
 %% @doc Returns a formatted string
 stringformat(Input, Conversion) when is_binary(Input) ->
 stringformat(Input, Conversion) when is_binary(Input) ->
-    stringformat(binary_to_list(Input), Conversion);
+    stringformat(unicode:characters_to_list(Input), Conversion);
 stringformat(Input, Conversion) when is_binary(Conversion) ->
 stringformat(Input, Conversion) when is_binary(Conversion) ->
-    stringformat(Input, binary_to_list(Conversion));
+    stringformat(Input, unicode:characters_to_list(Conversion));
 stringformat(Input, Conversion) ->
 stringformat(Input, Conversion) ->
     ParsedConversion = re:replace(Conversion, "([\-#\+ ]?)([0-9\*]+)?(\.?)([0-9]?)([diouxXeEfFgGcrs])", "\\1 ,\\2 ,\\3 ,\\4 ,\\5 ", [{return,list}]),
     ParsedConversion = re:replace(Conversion, "([\-#\+ ]?)([0-9\*]+)?(\.?)([0-9]?)([diouxXeEfFgGcrs])", "\\1 ,\\2 ,\\3 ,\\4 ,\\5 ", [{return,list}]),
     ?debugFmt("ParsedConversion: ~p~n", [ParsedConversion]),
     ?debugFmt("ParsedConversion: ~p~n", [ParsedConversion]),
@@ -708,7 +708,7 @@ stringformat_io(Input, Conversion, ConversionFlag, MinFieldWidth,
 
 
 %% @doc Strips all [X]HTML tags.
 %% @doc Strips all [X]HTML tags.
 striptags(Input) when is_binary(Input) ->
 striptags(Input) when is_binary(Input) ->
-    striptags(binary_to_list(Input));
+    striptags(unicode:characters_to_list(Input));
 striptags(Input) ->
 striptags(Input) ->
     Regex = "(<[^>]+>)",
     Regex = "(<[^>]+>)",
     Result = re:replace(Input,Regex,"", [global,{return,list}]),
     Result = re:replace(Input,Regex,"", [global,{return,list}]),
@@ -734,7 +734,7 @@ cast_to_integer(Input) when is_integer(Input) ->
 cast_to_integer(Input) when is_float(Input) ->
 cast_to_integer(Input) when is_float(Input) ->
     erlang:round(Input);
     erlang:round(Input);
 cast_to_integer(Input) when is_binary(Input) ->
 cast_to_integer(Input) when is_binary(Input) ->
-    cast_to_integer(binary_to_list(Input));
+    cast_to_integer(unicode:characters_to_list(Input));
 cast_to_integer(Input) when is_list(Input)->
 cast_to_integer(Input) when is_list(Input)->
     case lists:member($., Input) of
     case lists:member($., Input) of
         true ->
         true ->
@@ -745,7 +745,7 @@ cast_to_integer(Input) when is_list(Input)->
 
 
 %% @doc Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens.
 %% @doc Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens.
 slugify(Input) when is_binary(Input) ->
 slugify(Input) when is_binary(Input) ->
-    slugify(binary_to_list(Input));
+    slugify(unicode:characters_to_list(Input));
 slugify(Input) when is_list(Input) ->
 slugify(Input) when is_list(Input) ->
     slugify(Input, []).
     slugify(Input, []).
 
 
@@ -798,7 +798,7 @@ timeuntil(Date,Comparison) ->
 
 
 %% @doc Converts a string into titlecase.
 %% @doc Converts a string into titlecase.
 title(Input) when is_binary(Input) ->
 title(Input) when is_binary(Input) ->
-    title(binary_to_list(Input));
+    title(unicode:characters_to_list(Input));
 title(Input) when is_list(Input) ->
 title(Input) when is_list(Input) ->
     title(lower(Input), []).
     title(lower(Input), []).
 
 
@@ -814,7 +814,7 @@ truncatechars(Input, Max) ->
 truncatewords(_Input, Max) when Max =< 0 ->
 truncatewords(_Input, Max) when Max =< 0 ->
     "";
     "";
 truncatewords(Input, Max) when is_binary(Input) ->
 truncatewords(Input, Max) when is_binary(Input) ->
-    list_to_binary(truncatewords(binary_to_list(Input), Max));
+    unicode:characters_to_binary(truncatewords(unicode:characters_to_list(Input), Max));
 truncatewords(Input, Max) ->
 truncatewords(Input, Max) ->
     truncatewords(Input, Max, []).
     truncatewords(Input, Max, []).
 
 
@@ -822,7 +822,7 @@ truncatewords(Input, Max) ->
 truncatewords_html(_Input, Max) when Max =< 0 ->
 truncatewords_html(_Input, Max) when Max =< 0 ->
     "";
     "";
 truncatewords_html(Input, Max) when is_binary(Input) ->
 truncatewords_html(Input, Max) when is_binary(Input) ->
-    truncatewords_html(binary_to_list(Input), Max);
+    truncatewords_html(unicode:characters_to_list(Input), Max);
 truncatewords_html(Input, Max) ->
 truncatewords_html(Input, Max) ->
     truncatewords_html(Input, Max, [], [], text).
     truncatewords_html(Input, Max, [], [], text).
 
 
@@ -844,7 +844,7 @@ unordered_list([First|Rest], Acc) when is_list(First) ->
 
 
 %% @doc Converts a string into all uppercase.
 %% @doc Converts a string into all uppercase.
 upper(Input) when is_binary(Input) ->
 upper(Input) when is_binary(Input) ->
-    list_to_binary(upper(binary_to_list(Input)));
+    unicode:characters_to_binary(upper(unicode:characters_to_list(Input)));
 upper(Input) ->
 upper(Input) ->
     string:to_upper(Input).
     string:to_upper(Input).
 
 
@@ -856,21 +856,21 @@ urlencode(Input) when is_list(Input) ->
 
 
 %% @doc Returns the number of words.
 %% @doc Returns the number of words.
 wordcount(Input) when is_binary(Input) ->
 wordcount(Input) when is_binary(Input) ->
-    wordcount(binary_to_list(Input));
+    wordcount(unicode:characters_to_list(Input));
 wordcount(Input) when is_list(Input) ->
 wordcount(Input) when is_list(Input) ->
     wordcount(Input, 0).
     wordcount(Input, 0).
 
 
 %% @doc Wraps words at specified line length, uses `<BR/>' html tag to delimit lines
 %% @doc Wraps words at specified line length, uses `<BR/>' html tag to delimit lines
 wordwrap(Input, Number) when is_binary(Input) ->
 wordwrap(Input, Number) when is_binary(Input) ->
-    wordwrap(binary_to_list(Input), Number);
+    wordwrap(unicode:characters_to_list(Input), Number);
 wordwrap(Input, Number) when is_list(Input) ->
 wordwrap(Input, Number) when is_list(Input) ->
     wordwrap(Input, [], [], 0, Number).
     wordwrap(Input, [], [], 0, Number).
 
 
 %% @doc Given a string mapping values for true, false and (optionally) undefined, returns one of those strings according to the value.
 %% @doc Given a string mapping values for true, false and (optionally) undefined, returns one of those strings according to the value.
 yesno(Bool, Choices) when is_binary(Bool) ->
 yesno(Bool, Choices) when is_binary(Bool) ->
-    yesno_io(binary_to_list(Bool), Choices);
+    yesno_io(unicode:characters_to_list(Bool), Choices);
 yesno(Bool, Choices) when is_binary(Choices) ->
 yesno(Bool, Choices) when is_binary(Choices) ->
-    yesno_io(Bool, binary_to_list(Choices));
+    yesno_io(Bool, unicode:characters_to_list(Choices));
 yesno(Bool, Choices) when is_list(Choices) ->
 yesno(Bool, Choices) when is_list(Choices) ->
     yesno_io(Bool, Choices).
     yesno_io(Bool, Choices).
 
 
@@ -1152,12 +1152,12 @@ urlencode([C | Rest], Acc) ->
 %% @doc Converts URLs in text into clickable links.
 %% @doc Converts URLs in text into clickable links.
 %%TODO: Autoescape not yet implemented
 %%TODO: Autoescape not yet implemented
 urlize(Input) when is_binary(Input) ->
 urlize(Input) when is_binary(Input) ->
-    urlize(binary_to_list(Input),0);
+    urlize(unicode:characters_to_list(Input),0);
 urlize(Input) ->
 urlize(Input) ->
     urlize(Input,0).
     urlize(Input,0).
 
 
 urlize(Input, Trunc) when is_binary(Input) ->
 urlize(Input, Trunc) when is_binary(Input) ->
-    urlize(binary_to_list(Input),Trunc);
+    urlize(unicode:characters_to_list(Input),Trunc);
 urlize(Input, Trunc) ->
 urlize(Input, Trunc) ->
     {ok,RE} = re:compile("(([[:alpha:]]+://|www\.)[^<>[:space:]]+[[:alnum:]/])"),
     {ok,RE} = re:compile("(([[:alpha:]]+://|www\.)[^<>[:space:]]+[[:alnum:]/])"),
     RegexResult = re:run(Input,RE,[global]),
     RegexResult = re:run(Input,RE,[global]),