Permalink
Browse files

change erlydtl filters handle unicode input

  • Loading branch information...
1 parent ce9bc28 commit a3fa62f33ce955c7a1984d5f0e1a8ae193c29f05 @mellegar mellegar committed Feb 26, 2014
Showing with 40 additions and 40 deletions.
  1. +40 −40 src/erlydtl_filters.erl
View
80 src/erlydtl_filters.erl
@@ -159,9 +159,9 @@
add(LHS, RHS) when is_number(LHS), is_number(RHS) ->
LHS + RHS;
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, binary_to_list(RHS));
+ add(LHS, unicode:characters_to_list(RHS));
add(LHS, RHS) when is_list(LHS), is_list(RHS) ->
case {to_numeric(LHS), to_numeric(RHS)} of
{{number, LHSNum}, {number, RHSNum}} ->
@@ -204,7 +204,7 @@ to_numeric(List) ->
%% @doc Adds slashes before quotes.
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, []).
@@ -214,21 +214,21 @@ capfirst([H|T]) when H >= $a andalso H =< $z ->
capfirst(Other) when is_list(Other) ->
Other;
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) ->
Other.
%% @doc Centers the value in a field of a given width.
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) ->
string:centre(Input, Number).
%% @doc Removes all values of arg from the given string.
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(binary_to_list(Input), Arg);
+ cut(unicode:characters_to_list(Input), Arg);
cut(Input, [Char]) when is_list(Input) ->
cut(Input, Char, []).
@@ -238,7 +238,7 @@ date(Input) ->
%% @doc Formats a date according to the given format.
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) ->
erlydtl_dateformat:format({Date, Time}, FormatStr);
date({_,_,_} = Date, FormatStr) ->
@@ -284,25 +284,25 @@ dictsortreversed(DictList, Key) ->
%% @doc Returns `true' if the value is divisible by the argument.
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(list_to_integer(Input), 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, list_to_integer(Divisor));
divisibleby(Input, Divisor) when is_integer(Input), is_integer(Divisor) ->
Input rem Divisor =:= 0.
%% @doc Escapes characters for use in JavaScript strings.
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, []).
%% @doc Format the value like a human-readable file size.
filesizeformat(Input) when is_binary(Input) ->
- filesizeformat(binary_to_list(Input));
+ filesizeformat(unicode:characters_to_list(Input));
filesizeformat(Input) when is_list(Input) ->
filesizeformat(list_to_integer(Input));
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
%% -- but only if there's a decimal part to be displayed
floatformat(Number, Place) when is_binary(Number) ->
- floatformat(binary_to_list(Number), Place);
+ floatformat(unicode:characters_to_list(Number), Place);
floatformat(Number, 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.
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(integer_to_list(Input), 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, list_to_integer(Digit));
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
linebreaks(Input) when is_binary(Input) ->
- linebreaks(binary_to_list(Input),[]);
+ linebreaks(unicode:characters_to_list(Input),[]);
linebreaks(Input) ->
linebreaks(Input,[]).
@@ -456,7 +456,7 @@ linebreaksbr(Input) ->
%% @doc Displays text with line numbers.
linenumbers(Input) when is_binary(Input) ->
- linenumbers(binary_to_list(Input));
+ linenumbers(unicode:characters_to_list(Input));
linenumbers(Input) when is_list(Input) ->
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.
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) ->
string:left(Input, Number).
@@ -485,19 +485,19 @@ lower(Input) ->
%% 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.
make_list(Input) when is_binary(Input) ->
- make_list(binary_to_list(Input));
+ make_list(unicode:characters_to_list(Input));
make_list(Input) ->
unjoin(Input,"").
%% @doc Converts a phone number (possibly containing letters) to its numerical equivalent.
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, []).
%% @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_io(Number, binary_to_list(Suffix) );
+ pluralize_io(Number, unicode:characters_to_list(Suffix) );
pluralize(Number, Suffix) when is_list(Suffix) ->
pluralize_io(Number, Suffix).
@@ -551,9 +551,9 @@ random_range(Start, End) when End >= Start ->
lists:flatten(io_lib:format("~B",[Num])).
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, binary_to_list(Tags));
+ removetags(Input, unicode:characters_to_list(Tags));
removetags(Input, Tags) ->
TagList = string:tokens(Tags," "),
TagListString = string:join(TagList,"|"),
@@ -563,23 +563,23 @@ removetags(Input, Tags) ->
%% @doc Right-aligns the value in a field of a given width.
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) ->
string:right(Input, Number).
%% @doc Returns a slice of the list.
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) ->
erlydtl_slice:slice(Input, Index).
%% regex " ^([#0-\s+].)([0-9\*]+)(\.[0-9]+)([diouxXeEfFgGcrs]) " matches ALL of "-10.0f"
%% ([#0-\s+]?)([0-9\*]+)?(\.?)([0-9]?)([diouxXeEfFgGcrs])
%% @doc Returns a formatted string
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, binary_to_list(Conversion));
+ stringformat(Input, unicode:characters_to_list(Conversion));
stringformat(Input, Conversion) ->
ParsedConversion = re:replace(Conversion, "([\-#\+ ]?)([0-9\*]+)?(\.?)([0-9]?)([diouxXeEfFgGcrs])", "\\1 ,\\2 ,\\3 ,\\4 ,\\5 ", [{return,list}]),
?debugFmt("ParsedConversion: ~p~n", [ParsedConversion]),
@@ -708,7 +708,7 @@ stringformat_io(Input, Conversion, ConversionFlag, MinFieldWidth,
%% @doc Strips all [X]HTML tags.
striptags(Input) when is_binary(Input) ->
- striptags(binary_to_list(Input));
+ striptags(unicode:characters_to_list(Input));
striptags(Input) ->
Regex = "(<[^>]+>)",
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) ->
erlang:round(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)->
case lists:member($., Input) of
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.
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, []).
@@ -798,7 +798,7 @@ timeuntil(Date,Comparison) ->
%% @doc Converts a string into titlecase.
title(Input) when is_binary(Input) ->
- title(binary_to_list(Input));
+ title(unicode:characters_to_list(Input));
title(Input) when is_list(Input) ->
title(lower(Input), []).
@@ -814,15 +814,15 @@ truncatechars(Input, Max) ->
truncatewords(_Input, Max) when Max =< 0 ->
"";
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, []).
%% @doc Similar to truncatewords, except that it is aware of HTML tags.
truncatewords_html(_Input, Max) when Max =< 0 ->
"";
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, [], [], text).
@@ -844,7 +844,7 @@ unordered_list([First|Rest], Acc) when is_list(First) ->
%% @doc Converts a string into all uppercase.
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) ->
string:to_upper(Input).
@@ -856,21 +856,21 @@ urlencode(Input) when is_list(Input) ->
%% @doc Returns the number of words.
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, 0).
%% @doc Wraps words at specified line length, uses `<BR/>' html tag to delimit lines
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, [], [], 0, Number).
%% @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_io(binary_to_list(Bool), Choices);
+ yesno_io(unicode:characters_to_list(Bool), 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_io(Bool, Choices).
@@ -1152,12 +1152,12 @@ urlencode([C | Rest], Acc) ->
%% @doc Converts URLs in text into clickable links.
%%TODO: Autoescape not yet implemented
urlize(Input) when is_binary(Input) ->
- urlize(binary_to_list(Input),0);
+ urlize(unicode:characters_to_list(Input),0);
urlize(Input) ->
urlize(Input,0).
urlize(Input, Trunc) when is_binary(Input) ->
- urlize(binary_to_list(Input),Trunc);
+ urlize(unicode:characters_to_list(Input),Trunc);
urlize(Input, Trunc) ->
{ok,RE} = re:compile("(([[:alpha:]]+://|www\.)[^<>[:space:]]+[[:alnum:]/])"),
RegexResult = re:run(Input,RE,[global]),

0 comments on commit a3fa62f

Please sign in to comment.