Skip to content

Commit

Permalink
Merge pull request #62 from soranoba/feature/61
Browse files Browse the repository at this point in the history
unicode value support
  • Loading branch information
soranoba authored Nov 11, 2021
2 parents 7631da9 + 1137103 commit 07e5f46
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
40 changes: 38 additions & 2 deletions ct/bbmustache_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

variables_ct/1, sections_ct1/1, sections_ct2/1, sections_ct3/1, sections_ct4/1,
lambdas_ct/1, comments_ct/1, partials_ct/1, delimiter_ct/1, dot_ct/1, dot_unescape_ct/1,
indent_partials_ct/1, not_found_partials_ct1/1, not_found_partials_ct2/1, not_found_partials_ct3/1
indent_partials_ct/1, not_found_partials_ct1/1, not_found_partials_ct2/1, not_found_partials_ct3/1,
context_stack_ct/1, context_stack_ct2/1, partial_custom_reader_ct/1,
unicode_render_ct/1
]).
-define(ALL_TEST, [variables_ct, sections_ct1, sections_ct2, sections_ct3, sections_ct4,
lambdas_ct, comments_ct, partials_ct, delimiter_ct, dot_ct, dot_unescape_ct,
indent_partials_ct, not_found_partials_ct1, not_found_partials_ct2, not_found_partials_ct3,
context_stack_ct, context_stack_ct2, partial_custom_reader_ct]).
context_stack_ct, context_stack_ct2, partial_custom_reader_ct, unicode_render_ct]).

-define(config2, proplists:get_value).
-define(debug(X), begin io:format("~p", [X]), X end).
Expand Down Expand Up @@ -204,6 +206,40 @@ partial_custom_reader_ct(Config) ->
?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))([])),
?config2(options, Config, []) ++ [{partial_file_reader, fun(_, Key) -> Key end}])).

-ifdef(unicode_supported).
unicode_render_ct(Config) ->
Template = bbmustache:parse_file(filename:join([?config(data_dir, Config), <<"unicode.mustache">>])),
{ok, File} = file:read_file(filename:join([?config(data_dir, Config), <<"unicode.result">>])),

Data1 = [
{"whoami", ""},
{"name", "まだない"}
],
?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data1)), ?config2(options, Config, []))),

Data2 = [
{"whoami", <<""/utf8>>},
{"name", <<"まだない"/utf8>>}
],
?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data2)), ?config2(options, Config, []))),

Data3 = [
{"whoami", '猫'},
{"name", 'まだない'}
],
?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data3)), ?config2(options, Config, []))).
-else.
unicode_render_ct(Config) ->
Template = bbmustache:parse_file(filename:join([?config(data_dir, Config), <<"unicode.mustache">>])),
{ok, File} = file:read_file(filename:join([?config(data_dir, Config), <<"unicode.result">>])),

Data1 = [
{"whoami", ""},
{"name", "まだない"}
],
?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data1)), ?config2(options, Config, []))).
-endif.

%%----------------------------------------------------------------------------------------------------------------------
%% Internal Functions
%%----------------------------------------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions ct/bbmustache_SUITE_data/unicode.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
吾輩は{{whoami}}である名前は{{name}}
1 change: 1 addition & 0 deletions ct/bbmustache_SUITE_data/unicode.result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
吾輩は猫である名前はまだない
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{erl_opts, [
{platform_define, "^[0-9]+", namespaced_types},
{platform_define, "^20", unicode_supported},
warnings_as_errors,
warn_export_all,
warn_untyped_record
Expand Down
4 changes: 2 additions & 2 deletions src/bbmustache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ default_value_serializer(X) when is_map(X); is_tuple(X) ->
default_value_serializer(X) when X =:= null; X =:= nil ->
[];
default_value_serializer(X) when is_atom(X) ->
list_to_binary(atom_to_list(X));
unicode:characters_to_binary(atom_to_list(X));
default_value_serializer(X) ->
X.

Expand All @@ -275,7 +275,7 @@ compile_impl([], _, Result, _) ->
Result;
compile_impl([{n, Keys} | T], Data, Result, State) ->
ValueSerializer = proplists:get_value(value_serializer, State#?MODULE.options, fun default_value_serializer/1),
Value = iolist_to_binary(ValueSerializer(get_data_recursive(Keys, Data, <<>>, State))),
Value = unicode:characters_to_binary(ValueSerializer(get_data_recursive(Keys, Data, <<>>, State))),
EscapeFun = proplists:get_value(escape_fun, State#?MODULE.options, fun escape/1),
compile_impl(T, Data, ?ADD(EscapeFun(Value), Result), State);
compile_impl([{'&', Keys} | T], Data, Result, State) ->
Expand Down

0 comments on commit 07e5f46

Please sign in to comment.