Skip to content

Commit

Permalink
bump version 0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAlphaFox committed Feb 20, 2019
1 parent 19c6cd3 commit f00511e
Showing 1 changed file with 38 additions and 39 deletions.
77 changes: 38 additions & 39 deletions src/ai_mustache_runner.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,73 @@
-export([render/2,render/3]).

-spec render({[term()],map()},map())-> binary().
render({IR,Partials},Ctx)->run(IR,<<>>,[],Partials,Ctx).
render({IR,Partials},Ctx)->run(<<>>,IR,[],Partials,Ctx).

-spec render([term()],map(),map())-> binary().
render(IR,Partials,Ctx)-> run(IR,<<>>,[],Partials,Ctx).
render(IR,Partials,Ctx)-> run(<<>>,IR,[],Partials,Ctx).

run([],Acc,[],_Partials,_Ctx)-> Acc;
run([],Acc,[{H,OldCtx}|Stack],Partials,_Ctx)->
run(H,Acc,Stack,Partials,OldCtx);
run([{binary,Value}|IR],Acc,Stack,Partials,Ctx)->
run(IR,<<Acc/binary,Value/binary>>,Stack,Partials,Ctx);
run([{tag,none,Name}|IR],Acc,Stack,Partials,Ctx)->
run(Acc,[],[],_Partials,_Ctx)-> Acc;
run(Acc,[],[{H,OldCtx}|Stack],Partials,_Ctx)->
run(Acc,H,Stack,Partials,OldCtx);
run(Acc,[{binary,Value}|IR],Stack,Partials,Ctx)->
run(<<Acc/binary,Value/binary>>,IR,Stack,Partials,Ctx);
run(Acc,[{tag,none,Name}|IR],Stack,Partials,Ctx)->
Value = ai_string:html_escape(ai_maps:get(Name,Ctx,<<"">>)),
run(IR,<<Acc/binary,Value/binary>>,Stack,Partials,Ctx);
run([{tag,raw,Name}|IR],Acc,Stack,Partials,Ctx)->
run(<<Acc/binary,Value/binary>>,IR,Stack,Partials,Ctx);
run(Acc,[{tag,raw,Name}|IR],Stack,Partials,Ctx)->
Value = ai_string:to_string(ai_maps:get(Name,Ctx,<<"">>)),
run(IR,<<Acc/binary,Value/binary>>,Stack,Partials,Ctx);
run([{tag,partial,Name }|IR],Acc,Stack,Partials,Ctx)->
run(<<Acc/binary,Value/binary>>,IR,Stack,Partials,Ctx);
run(Acc,[{tag,partial,Name }|IR],Stack,Partials,Ctx)->
Paths = binary:split(Name,<<"/">>,[global]),
[H|T] = Paths,
Key = [<<".",H/binary>>|T],
case ai_maps:get(Paths,Partials,undefined) of
undefined ->
run(IR,Acc,Stack,Partials,Ctx);
undefined -> run(Acc,IR,Stack,Partials,Ctx);
NewIR ->
case ai_maps:get(Key,Ctx,undefined) of
undefined ->
run(NewIR,Acc,[{IR,Ctx}|Stack],Partials,Ctx);
run(Acc,NewIR,[{IR,Ctx}|Stack],Partials,Ctx);
NewCtx ->
NewCtx0 = maps:merge(Ctx,NewCtx),
run(NewIR,Acc,[{IR,Ctx}|Stack],Partials,NewCtx0)
run(Acc,NewIR,[{IR,Ctx}|Stack],Partials,NewCtx0)
end
end;
run([{section,Name,SectionIR,false}|IR],Acc,Stack,Partials,Ctx)->
case ai_maps:get(Name,Ctx,undefined) of
undefined -> run(SectionIR,Acc,[{IR,Ctx}|Stack],Partials,Ctx);
false -> run(SectionIR,Acc,[{IR,Ctx}|Stack],Partials,Ctx);
true -> run(IR,Acc,Stack,Partials,Ctx);
run(Acc,[{section,Name,SectionIR,false}|IR],Stack,Partials,Ctx)->
case ai_maps:get(Name,Ctx,undefined) of
undefined -> run(Acc,SectionIR,[{IR,Ctx}|Stack],Partials,Ctx);
false -> run(Acc,SectionIR,[{IR,Ctx}|Stack],Partials,Ctx);
true -> run(Acc,IR,Stack,Partials,Ctx);
L when erlang:is_list(L)->
case L of
[] -> run(SectionIR,Acc,[{IR,Ctx}|Stack],Partials,Ctx);
_ -> run(IR,Acc,Stack,Partials,Ctx)
[] -> run(Acc,SectionIR,[{IR,Ctx}|Stack],Partials,Ctx);
_ -> run(Acc,IR,Stack,Partials,Ctx)
end;
_ ->
run(IR,Acc,Stack,Partials,Ctx)
run(Acc,IR,Stack,Partials,Ctx)
end;
run([{section,Name,SectionIR,true}|IR],Acc,Stack,Partials,Ctx)->
run(Acc,[{section,Name,SectionIR,true}|IR],Stack,Partials,Ctx)->
case ai_maps:get(Name,Ctx,undefined) of
undefined -> run(IR,Acc,Stack,Partials,Ctx);
false -> run(IR,Acc,Stack,Partials,Ctx);
true -> run(SectionIR,Acc,[{IR,Ctx}|Stack],Partials,Ctx);
M when erlang:is_map(M)-> run(SectionIR,Acc,[{IR,Ctx}|Stack],Partials,Ctx);
undefined -> run(Acc,IR,Stack,Partials,Ctx);
false -> run(Acc,IR,Stack,Partials,Ctx);
true -> run(Acc,SectionIR,[{IR,Ctx}|Stack],Partials,Ctx);
M when erlang:is_map(M)-> run(Acc,SectionIR,[{IR,Ctx}|Stack],Partials,Ctx);
L when erlang:is_list(L)->
case L of
[] -> run(IR,Acc,Stack,Partials,Ctx);
[] -> run(Acc,IR,Stack,Partials,Ctx);
_ ->
run(SectionIR,Acc,L,[{IR,Ctx}|Stack],Partials,Ctx)
run(Acc,SectionIR,L,[{IR,Ctx}|Stack],Partials,Ctx)
end;
F when erlang:is_function(F,2) ->
Acc0 = run(SectionIR,<<>>,[],Partials,Ctx),
Acc0 = run(<<>>,SectionIR,[],Partials,Ctx),
Acc1 = F(Acc0,Ctx),
run(IR,<<Acc/binary,Acc1/binary>>,Stack,Partials,Ctx);
run(<<Acc/binary,Acc1/binary>>,IR,Stack,Partials,Ctx);
_->
run(IR,Acc,Stack,Partials,Ctx)
run(Acc,IR,Stack,Partials,Ctx)
end.
run(_SectionIR,Acc,[],[{IR,OldCtx}|Stack],Partials,_Ctx)->
run(IR,Acc,Stack,Partials,OldCtx);
run(SectionIR,Acc,[H|T],Stack,Partials,Ctx)->
Acc0 = run(SectionIR,Acc,[],Partials,maps:merge(Ctx,H)),
run(SectionIR,Acc0,T,Stack,Partials,Ctx).
run(Acc,_SectionIR,[],[{IR,OldCtx}|Stack],Partials,_Ctx)->
run(Acc,IR,Stack,Partials,OldCtx);
run(Acc,SectionIR,[H|T],Stack,Partials,Ctx)->
Acc0 = run(Acc,SectionIR,[],Partials,maps:merge(Ctx,H)),
run(Acc0,SectionIR,T,Stack,Partials,Ctx).


0 comments on commit f00511e

Please sign in to comment.