diff options
author | Fred Hebert <mononcqc@ferd.ca> | 2015-05-05 21:18:04 -0400 |
---|---|---|
committer | Fred Hebert <mononcqc@ferd.ca> | 2015-05-05 21:18:04 -0400 |
commit | 5cf005d3a63608ff4a9a7151d8461f360c86cfbb (patch) | |
tree | b32082ac8557d2208676507be4a2697f205c2431 | |
parent | 0883a6eca754f5442b8c9d273d7fc0be1b768dec (diff) | |
parent | 275bdd6c04089353e879527e273b4a36cdff0218 (diff) |
Merge pull request #392 from tsloughter/mustache2
switch mustache imlementation to https://github.com/soranoba/mustache
-rw-r--r-- | src/rebar_mustache.erl | 506 | ||||
-rw-r--r-- | src/rebar_templater.erl | 6 |
2 files changed, 287 insertions, 225 deletions
diff --git a/src/rebar_mustache.erl b/src/rebar_mustache.erl index 3fc21b5..af8a342 100644 --- a/src/rebar_mustache.erl +++ b/src/rebar_mustache.erl @@ -1,6 +1,6 @@ -%% The MIT License +%% The MIT License (MIT) %% -%% Copyright (c) 2009 Tom Preston-Werner <tom@mojombo.com> +%% Copyright (c) 2015 Hinagiku Soranoba %% %% Permission is hereby granted, free of charge, to any person obtaining a copy %% of this software and associated documentation files (the "Software"), to deal @@ -9,222 +9,288 @@ %% copies of the Software, and to permit persons to whom the Software is %% furnished to do so, subject to the following conditions: %% -%% The above copyright notice and this permission notice shall be included in -%% all copies or substantial portions of the Software. +%% The above copyright notice and this permission notice shall be included in all +%% copies or substantial portions of the Software. %% -%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -%% THE SOFTWARE. - -%% See the README at http://github.com/mojombo/mustache.erl for additional -%% documentation and usage examples. - --module(rebar_mustache). %% v0.1.0 --author("Tom Preston-Werner"). --export([compile/1, compile/2, render/1, render/2, render/3, get/2, get/3, escape/1, start/1]). - --record(mstate, {mod = undefined, - section_re = undefined, - tag_re = undefined}). - --define(MUSTACHE_STR, "rebar_mustache"). - -compile(Body) when is_list(Body) -> - State = #mstate{}, - CompiledTemplate = pre_compile(Body, State), - % io:format("~p~n~n", [CompiledTemplate]), - % io:format(CompiledTemplate ++ "~n", []), - {ok, Tokens, _} = erl_scan:string(CompiledTemplate), - {ok, [Form]} = erl_parse:parse_exprs(Tokens), - Bindings = erl_eval:new_bindings(), - {value, Fun, _} = erl_eval:expr(Form, Bindings), - Fun; -compile(Mod) -> - TemplatePath = template_path(Mod), - compile(Mod, TemplatePath). - -compile(Mod, File) -> - code:purge(Mod), - {module, _} = code:load_file(Mod), - {ok, TemplateBin} = file:read_file(File), - Template = re:replace(TemplateBin, "\"", "\\\\\"", [global, {return,list}]), - State = #mstate{mod = Mod}, - CompiledTemplate = pre_compile(Template, State), - % io:format("~p~n~n", [CompiledTemplate]), - % io:format(CompiledTemplate ++ "~n", []), - {ok, Tokens, _} = erl_scan:string(CompiledTemplate), - {ok, [Form]} = erl_parse:parse_exprs(Tokens), - Bindings = erl_eval:new_bindings(), - {value, Fun, _} = erl_eval:expr(Form, Bindings), - Fun. - -render(Mod) -> - TemplatePath = template_path(Mod), - render(Mod, TemplatePath). - -render(Body, Ctx) when is_list(Body) -> - TFun = compile(Body), - render(undefined, TFun, Ctx); -render(Mod, File) when is_list(File) -> - render(Mod, File, dict:new()); -render(Mod, CompiledTemplate) -> - render(Mod, CompiledTemplate, dict:new()). - -render(Mod, File, Ctx) when is_list(File) -> - CompiledTemplate = compile(Mod, File), - render(Mod, CompiledTemplate, Ctx); -render(Mod, CompiledTemplate, Ctx) -> - Ctx2 = dict:store('__mod__', Mod, Ctx), - lists:flatten(CompiledTemplate(Ctx2)). - -pre_compile(T, State) -> - SectionRE = "\{\{\#([^\}]*)}}\s*(.+?){{\/\\1\}\}\s*", - {ok, CompiledSectionRE} = re:compile(SectionRE, [dotall]), - TagRE = "\{\{(#|=|!|<|>|\{)?(.+?)\\1?\}\}", - {ok, CompiledTagRE} = re:compile(TagRE, [dotall]), - State2 = State#mstate{section_re = CompiledSectionRE, tag_re = CompiledTagRE}, - "fun(Ctx) -> " ++ - "CFun = fun(A, B) -> A end, " ++ - compiler(T, State2) ++ " end.". - -compiler(T, State) -> - Res = re:run(T, State#mstate.section_re), - case Res of - {match, [{M0, M1}, {N0, N1}, {C0, C1}]} -> - Front = string:substr(T, 1, M0), - Back = string:substr(T, M0 + M1 + 1), - Name = string:substr(T, N0 + 1, N1), - Content = string:substr(T, C0 + 1, C1), - "[" ++ compile_tags(Front, State) ++ - " | [" ++ compile_section(Name, Content, State) ++ - " | [" ++ compiler(Back, State) ++ "]]]"; - nomatch -> - compile_tags(T, State) - end. - -compile_section(Name, Content, State) -> - Mod = State#mstate.mod, - Result = compiler(Content, State), - "fun() -> " ++ - "case " ++ ?MUSTACHE_STR ++ ":get(" ++ Name ++ ", Ctx, " ++ atom_to_list(Mod) ++ ") of " ++ - "\"true\" -> " ++ - Result ++ "; " ++ - "\"false\" -> " ++ - "[]; " ++ - "List when is_list(List) -> " ++ - "[fun(Ctx) -> " ++ Result ++ " end(dict:merge(CFun, SubCtx, Ctx)) || SubCtx <- List]; " ++ - "Else -> " ++ - "throw({template, io_lib:format(\"Bad context for ~p: ~p\", [" ++ Name ++ ", Else])}) " ++ - "end " ++ - "end()". - -compile_tags(T, State) -> - Res = re:run(T, State#mstate.tag_re), - case Res of - {match, [{M0, M1}, K, {C0, C1}]} -> - Front = string:substr(T, 1, M0), - Back = string:substr(T, M0 + M1 + 1), - Content = string:substr(T, C0 + 1, C1), - Kind = tag_kind(T, K), - Result = compile_tag(Kind, Content, State), - "[\"" ++ Front ++ - "\" | [" ++ Result ++ - " | " ++ compile_tags(Back, State) ++ "]]"; - nomatch -> - "[\"" ++ T ++ "\"]" - end. - -tag_kind(_T, {-1, 0}) -> - none; -tag_kind(T, {K0, K1}) -> - string:substr(T, K0 + 1, K1). - -compile_tag(none, Content, State) -> - Mod = State#mstate.mod, - ?MUSTACHE_STR ++ ":escape(" ++ ?MUSTACHE_STR ++ ":get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ "))"; -compile_tag("{", Content, State) -> - Mod = State#mstate.mod, - ?MUSTACHE_STR ++ ":get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ ")"; -compile_tag("!", _Content, _State) -> - "[]". - -template_dir(Mod) -> - DefaultDirPath = filename:dirname(code:which(Mod)), - case application:get_env(mustache, templates_dir) of - {ok, DirPath} when is_list(DirPath) -> - case filelib:ensure_dir(DirPath) of - ok -> DirPath; - _ -> DefaultDirPath - end; - _ -> - DefaultDirPath - end. -template_path(Mod) -> - DirPath = template_dir(Mod), - Basename = atom_to_list(Mod), - filename:join(DirPath, Basename ++ ".mustache"). - -get(Key, Ctx) when is_list(Key) -> - {ok, Mod} = dict:find('__mod__', Ctx), - get(list_to_atom(Key), Ctx, Mod); -get(Key, Ctx) -> - {ok, Mod} = dict:find('__mod__', Ctx), - get(Key, Ctx, Mod). - -get(Key, Ctx, Mod) when is_list(Key) -> - get(list_to_atom(Key), Ctx, Mod); -get(Key, Ctx, Mod) -> - case dict:find(Key, Ctx) of - {ok, Val} -> - % io:format("From Ctx {~p, ~p}~n", [Key, Val]), - to_s(Val); - error -> - case erlang:function_exported(Mod, Key, 1) of - true -> - Val = to_s(Mod:Key(Ctx)), - % io:format("From Mod/1 {~p, ~p}~n", [Key, Val]), - Val; - false -> - case erlang:function_exported(Mod, Key, 0) of - true -> - Val = to_s(Mod:Key()), - % io:format("From Mod/0 {~p, ~p}~n", [Key, Val]), - Val; - false -> - [] - end - end - end. - -to_s(Val) when is_integer(Val) -> - integer_to_list(Val); -to_s(Val) when is_float(Val) -> - io_lib:format("~.2f", [Val]); -to_s(Val) when is_atom(Val) -> - atom_to_list(Val); -to_s(Val) -> - Val. - -escape(HTML) -> - escape(HTML, []). - -escape([], Acc) -> - lists:reverse(Acc); -escape(["<" | Rest], Acc) -> - escape(Rest, lists:reverse("<", Acc)); -escape([">" | Rest], Acc) -> - escape(Rest, lists:reverse(">", Acc)); -escape(["&" | Rest], Acc) -> - escape(Rest, lists:reverse("&", Acc)); -escape([X | Rest], Acc) -> - escape(Rest, [X | Acc]). - -%%--------------------------------------------------------------------------- - -start([T]) -> - Out = render(list_to_atom(T)), - io:format(Out ++ "~n", []). +%% @doc Mustache template engine for Erlang/OTP. +-module(rebar_mustache). + +%%---------------------------------------------------------------------------------------------------------------------- +%% Exported API +%%---------------------------------------------------------------------------------------------------------------------- +-export([ + render/2, + parse_binary/1, + parse_file/1, + compile/2 + ]). + +-export_type([ + template/0, + data/0 + ]). + +%%---------------------------------------------------------------------------------------------------------------------- +%% Defines & Records & Types +%%---------------------------------------------------------------------------------------------------------------------- + +-define(PARSE_ERROR, incorrect_format). +-define(FILE_ERROR, file_not_found). +-define(COND(Cond, TValue, FValue), + case Cond of true -> TValue; false -> FValue end). + +-type key() :: binary(). +-type tag() :: {n, key()} | + {'&', key()} | + {'#', key(), [tag()], Source :: binary()} | + {'^', key(), [tag()]} | + binary(). + +-record(state, + { + dirname = <<>> :: file:filename_all(), + start = <<"{{">> :: binary(), + stop = <<"}}">> :: binary() + }). +-type state() :: #state{}. + +-record(?MODULE, + { + data :: [tag()] + }). + +-opaque template() :: #?MODULE{}. +%% @see parse_binary/1 +%% @see parse_file/1 +-ifdef(namespaced_types). +-type data() :: #{string() => data() | iodata() | fun((data(), function()) -> iodata())}. +-else. +-type data() :: dict(). +-endif. +%% @see render/2 +%% @see compile/2 +-type partial() :: {partial, {state(), EndTag :: binary(), LastTagSize :: non_neg_integer(), Rest :: binary(), [tag()]}}. + +%%---------------------------------------------------------------------------------------------------------------------- +%% Exported Functions +%%---------------------------------------------------------------------------------------------------------------------- + +%% @equiv compile(parse_binary(Bin), Map) +-spec render(binary(), data()) -> binary(). +render(Bin, Map) -> + compile(parse_binary(Bin), Map). + +%% @doc Create a {@link template/0} from a binary. +-spec parse_binary(binary()) -> template(). +parse_binary(Bin) when is_binary(Bin) -> + parse_binary_impl(#state{}, Bin). + +%% @doc Create a {@link template/0} from a file. +-spec parse_file(file:filename()) -> template(). +parse_file(Filename) -> + case file:read_file(Filename) of + {ok, Bin} -> parse_binary_impl(#state{dirname = filename:dirname(Filename)}, Bin); + _ -> error(?FILE_ERROR, [Filename]) + end. + +%% @doc Embed the data in the template. +-spec compile(template(), data()) -> binary(). +compile(#?MODULE{data = Tags}, Map) -> + ec_cnv:to_binary(lists:reverse(compile_impl(Tags, Map, []))). + +%%---------------------------------------------------------------------------------------------------------------------- +%% Internal Function +%%---------------------------------------------------------------------------------------------------------------------- + +%% @doc {@link compile/2} +%% +%% ATTENTION: The result is a list that is inverted. +-spec compile_impl(Template :: [tag()], data(), Result :: iodata()) -> iodata(). +compile_impl([], _, Result) -> + Result; +compile_impl([{n, Key} | T], Map, Result) -> + compile_impl(T, Map, [escape(to_binary(dict_get(binary_to_list(Key), Map, <<>>))) | Result]); +compile_impl([{'&', Key} | T], Map, Result) -> + compile_impl(T, Map, [to_binary(dict_get(binary_to_list(Key), Map, <<>>)) | Result]); +compile_impl([{'#', Key, Tags, Source} | T], Map, Result) -> + Value = dict_get(binary_to_list(Key), Map, undefined), + if + is_list(Value) -> compile_impl(T, Map, lists:foldl(fun(X, Acc) -> compile_impl(Tags, X, Acc) end, + Result, Value)); + Value =:= false; Value =:= undefined -> compile_impl(T, Map, Result); + is_function(Value, 2) -> compile_impl(T, Map, [Value(Source, fun(Text) -> render(Text, Map) end) | Result]); + %is_dict(Value) -> compile_impl(T, Map, compile_impl(Tags, Value, Result)); + true -> compile_impl(T, Map, compile_impl(Tags, Map, Result)) + end; +compile_impl([{'^', Key, Tags} | T], Map, Result) -> + Value = dict_get(binary_to_list(Key), Map, undefined), + case Value =:= undefined orelse Value =:= [] orelse Value =:= false of + true -> compile_impl(T, Map, compile_impl(Tags, Map, Result)); + false -> compile_impl(T, Map, Result) + end; +compile_impl([Bin | T], Map, Result) -> + compile_impl(T, Map, [Bin | Result]). + +%% @see parse_binary/1 +-spec parse_binary_impl(state(), Input :: binary()) -> template(). +parse_binary_impl(State, Input) -> + #?MODULE{data = parse(State, Input)}. + +%% @doc Analyze the syntax of the mustache. +-spec parse(state(), binary()) -> [tag()]. +parse(State, Bin) -> + case parse1(State, Bin, []) of + {partial, _} -> error(?PARSE_ERROR); + {_, Tags} -> lists:reverse(Tags) + end. + +%% @doc Part of the `parse/1' +%% +%% ATTENTION: The result is a list that is inverted. +-spec parse1(state(), Input :: binary(), Result :: [tag()]) -> {state(), [tag()]} | partial(). +parse1(#state{start = Start, stop = Stop} = State, Bin, Result) -> + case binary:split(Bin, Start) of + [] -> {State, Result}; + [B1] -> {State, [B1 | Result]}; + [B1, <<"{", B2/binary>>] -> parse2(State, binary:split(B2, <<"}", Stop/binary>>), [B1 | Result]); + [B1, B2] -> parse3(State, binary:split(B2, Stop), [B1 | Result]) + end. + +%% @doc Part of the `parse/1' +%% +%% ATTENTION: The result is a list that is inverted. +parse2(State, [B1, B2], Result) -> + parse1(State, B2, [{'&', remove_space_from_edge(B1)} | Result]); +parse2(_, _, _) -> + error(?PARSE_ERROR). + +%% @doc Part of the `parse/1' +%% +%% ATTENTION: The result is a list that is inverted. +parse3(State, [B1, B2], Result) -> + case remove_space_from_head(B1) of + <<"&", Tag/binary>> -> + parse1(State, B2, [{'&', remove_space_from_edge(Tag)} | Result]); + <<T, Tag/binary>> when T =:= $#; T =:= $^ -> + parse_loop(State, ?COND(T =:= $#, '#', '^'), remove_space_from_edge(Tag), B2, Result); + <<"=", Tag0/binary>> -> + Tag1 = remove_space_from_tail(Tag0), + Size = byte_size(Tag1) - 1, + case Size >= 0 andalso Tag1 of + <<Tag2:Size/binary, "=">> -> parse_delimiter(State, Tag2, B2, Result); + _ -> error(?PARSE_ERROR) + end; + <<"!", _/binary>> -> + parse1(State, B2, Result); + <<"/", Tag/binary>> -> + {partial, {State, remove_space_from_edge(Tag), byte_size(B1) + 4, B2, Result}}; + <<">", Tag/binary>> -> + parse_jump(State, remove_space_from_edge(Tag), B2, Result); + Tag -> + parse1(State, B2, [{n, remove_space_from_tail(Tag)} | Result]) + end; +parse3(_, _, _) -> + error(?PARSE_ERROR). + +%% @doc Loop processing part of the `parse/1' +%% +%% `{{# Tag}}' or `{{^ Tag}}' corresponds to this. +-spec parse_loop(state(), '#' | '^', Tag :: binary(), Input :: binary(), Result :: [tag()]) -> [tag()] | partial(). +parse_loop(State0, Mark, Tag, Input, Result0) -> + case parse1(State0, Input, []) of + {partial, {State, Tag, LastTagSize, Rest, Result1}} when is_list(Result1) -> + case Mark of + '#' -> Source = binary:part(Input, 0, byte_size(Input) - byte_size(Rest) - LastTagSize), + parse1(State, Rest, [{'#', Tag, lists:reverse(Result1), Source} | Result0]); + '^' -> parse1(State, Rest, [{'^', Tag, lists:reverse(Result1)} | Result0]) + end; + _ -> + error(?PARSE_ERROR) + end. + +%% @doc Partial part of the `parse/1' +-spec parse_jump(state(), Tag :: binary(), NextBin :: binary(), Result :: [tag()]) -> [tag()] | partial(). +parse_jump(#state{dirname = Dirname} = State0, Tag, NextBin, Result0) -> + Filename0 = <<Tag/binary, ".mustache">>, + Filename = filename:join(?COND(Dirname =:= <<>>, [Filename0], [Dirname, Filename0])), + case file:read_file(Filename) of + {ok, Bin} -> + case parse1(State0, Bin, Result0) of + {partial, _} -> error(?PARSE_ERROR); + {State, Result} -> parse1(State, NextBin, Result) + end; + _ -> + error(?FILE_ERROR, [Filename]) + end. + +%% @doc Update delimiter part of the `parse/1' +%% +%% Parse_BinaryDelimiterBin :: e.g. `{{=%% %%=}}' -> `%% %%' +-spec parse_delimiter(state(), Parse_BinaryDelimiterBin :: binary(), NextBin :: binary(), Result :: [tag()]) -> [tag()] | partial(). +parse_delimiter(State0, Parse_BinaryDelimiterBin, NextBin, Result) -> + case binary:match(Parse_BinaryDelimiterBin, <<"=">>) of + nomatch -> + case [X || X <- binary:split(Parse_BinaryDelimiterBin, <<" ">>, [global]), X =/= <<>>] of + [Start, Stop] -> parse1(State0#state{start = Start, stop = Stop}, NextBin, Result); + _ -> error(?PARSE_ERROR) + end; + _ -> + error(?PARSE_ERROR) + end. + +%% @doc Remove the space from the edge. +-spec remove_space_from_edge(binary()) -> binary(). +remove_space_from_edge(Bin) -> + remove_space_from_tail(remove_space_from_head(Bin)). + +%% @doc Remove the space from the head. +-spec remove_space_from_head(binary()) -> binary(). +remove_space_from_head(<<" ", Rest/binary>>) -> remove_space_from_head(Rest); +remove_space_from_head(Bin) -> Bin. + +%% @doc Remove the space from the tail. +-spec remove_space_from_tail(binary()) -> binary(). +remove_space_from_tail(<<>>) -> <<>>; +remove_space_from_tail(Bin) -> + PosList = binary:matches(Bin, <<" ">>), + LastPos = remove_space_from_tail_impl(lists:reverse(PosList), byte_size(Bin)), + binary:part(Bin, 0, LastPos). + +%% @see remove_space_from_tail/1 +-spec remove_space_from_tail_impl([{non_neg_integer(), pos_integer()}], non_neg_integer()) -> non_neg_integer(). +remove_space_from_tail_impl([{X, Y} | T], Size) when Size =:= X + Y -> + remove_space_from_tail_impl(T, X); +remove_space_from_tail_impl(_, Size) -> + Size. + +%% @doc Number to binary +-spec to_binary(number() | binary() | string()) -> binary() | string(). +to_binary(Integer) when is_integer(Integer) -> + ec_cnv:to_binary(Integer); +to_binary(Float) when is_float(Float) -> + io_lib:format("~p", [Float]); +to_binary(X) -> + X. + +%% @doc HTML Escape +-spec escape(iodata()) -> binary(). +escape(IoData) -> + Bin = ec_cnv:to_binary(IoData), + << <<(escape_char(X))/binary>> || <<X:8>> <= Bin >>. + +%% @see escape/1 +-spec escape_char(0..16#FFFF) -> binary(). +escape_char($<) -> <<"<">>; +escape_char($>) -> <<">">>; +escape_char($&) -> <<"&">>; +escape_char($") -> <<""">>; +escape_char($') -> <<"'">>; +escape_char(C) -> <<C:8>>. + +dict_get(Key, Dict, Default) -> + case dict:find(ec_cnv:to_atom(Key), Dict) of + {ok, Value} -> + Value; + error -> + Default + end. diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 0a9a07c..1bd4e09 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -380,8 +380,4 @@ write_file(Output, Data, Force) -> %% Render a binary to a string, using mustache and the specified context %% render(Bin, Context) -> - %% Be sure to escape any double-quotes before rendering... - ReOpts = [global, {return, list}], - Str0 = re:replace(Bin, "\\\\", "\\\\\\", ReOpts), - Str1 = re:replace(Str0, "\"", "\\\\\"", ReOpts), - rebar_mustache:render(Str1, dict:from_list(Context)). + rebar_mustache:render(ec_cnv:to_binary(Bin), dict:from_list(Context)). |