Skip to content

Commit

Permalink
Merge branch 'feature/customize_escape'
Browse files Browse the repository at this point in the history
  • Loading branch information
soranoba committed Aug 16, 2017
2 parents 8d37efe + 2a2ba0b commit b82fa25
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

|Library|Time |
|:------|:-----|
|bbmustache | 70308 |
|mustache.erl | 946352 |
|bbmustache | 64638 |
|mustache.erl | 932479 |

# Check the reference implementation
:warning: For libraries other than bbmustache, there is a possibility that there is a miss.
Expand Down
10 changes: 7 additions & 3 deletions doc/bbmustache.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ data_value() = <a href="#type-data">data()</a> | iodata() | number() | atom() |


<pre><code>
option() = {key_type, atom | binary | string} | raise_on_context_miss
option() = {key_type, atom | binary | string} | raise_on_context_miss | {escape_fun, fun((binary()) -&gt; binary())}
</code></pre>

- key_type: Specify the type of the key in [`data/0`](#data-0). Default value is `string`.
- raise_on_contex_miss: If key exists in template does not exist in data, it will throw an exception (error).
- key_type:
-- Specify the type of the key in [`data/0`](#data-0). Default value is `string`.
- raise_on_contex_miss:
-- If key exists in template does not exist in data, it will throw an exception (error).
- escape_fun:
-- Specify your own escape function.



Expand Down
20 changes: 13 additions & 7 deletions src/bbmustache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,14 @@
-type assoc_data() :: [{atom(), data_value()}] | [{binary(), data_value()}] | [{string(), data_value()}].

-type option() :: {key_type, atom | binary | string}
| raise_on_context_miss.
%% - key_type: Specify the type of the key in {@link data/0}. Default value is `string'.
%% - raise_on_contex_miss: If key exists in template does not exist in data, it will throw an exception (error).
| raise_on_context_miss
| {escape_fun, fun((binary()) -> binary())}.
%% - key_type:
%% -- Specify the type of the key in {@link data/0}. Default value is `string'.
%% - raise_on_contex_miss:
%% -- If key exists in template does not exist in data, it will throw an exception (error).
%% - escape_fun:
%% -- Specify your own escape function.

-ifdef(namespaced_types).
-type maps_data() :: #{atom() => data_value()} | #{binary() => data_value()} | #{string() => data_value()}.
Expand Down Expand Up @@ -191,7 +196,9 @@ compile(#?MODULE{data = Tags} = T, Data, Options) ->
compile_impl([], _, Result, _) ->
Result;
compile_impl([{n, Keys} | T], Map, Result, State) ->
compile_impl(T, Map, ?ADD(escape(to_iodata(get_data_recursive(Keys, Map, <<>>, State))), Result), State);
Value = iolist_to_binary(to_iodata(get_data_recursive(Keys, Map, <<>>, State))),
EscapeFun = proplists:get_value(escape_fun, State#?MODULE.options, fun escape/1),
compile_impl(T, Map, ?ADD(EscapeFun(Value), Result), State);
compile_impl([{'&', Keys} | T], Map, Result, State) ->
compile_impl(T, Map, ?ADD(to_iodata(get_data_recursive(Keys, Map, <<>>, State)), Result), State);
compile_impl([{'#', Keys, Tags, Source} | T], Map, Result, State) ->
Expand Down Expand Up @@ -534,9 +541,8 @@ to_binary(Str) when is_list(Str) ->
list_to_binary(Str).

%% @doc HTML Escape
-spec escape(iodata()) -> binary().
escape(IoData) ->
Bin = iolist_to_binary(IoData),
-spec escape(binary()) -> binary().
escape(Bin) ->
<< <<(escape_char(X))/binary>> || <<X:8>> <= Bin >>.

%% @doc escape a character if needed.
Expand Down
9 changes: 9 additions & 0 deletions test/bbmustache_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,12 @@ context_stack_test_() ->
[{"parent", []}],
[raise_on_context_miss]))}
].

escape_fun_test_() ->
[
{"It is able to specified own escape function",
?_assertEqual(<<"==>value<==">>,
bbmustache:render(<<"{{tag}}">>,
[{"tag", "value"}],
[{escape_fun, fun(X) -> <<"==>", X/binary, "<==">> end}]))}
].

0 comments on commit b82fa25

Please sign in to comment.