Skip to content

Commit

Permalink
add unescaped tag functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Dec 23, 2009
1 parent 9e57917 commit d89cbc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions examples/unescaped.erl
@@ -0,0 +1,12 @@
-module(unescaped).
-compile(export_all).

title() ->
"Bear > Shark".

%%---------------------------------------------------------------------------

start() ->
code:add_patha(".."),
Output = mustache:render(unescaped, "unescaped.mustache"),
io:format(Output, []).
1 change: 1 addition & 0 deletions examples/unescaped.mustache
@@ -0,0 +1 @@
<h1>{{{title}}}</h1>
19 changes: 18 additions & 1 deletion mustache.erl
@@ -1,6 +1,6 @@
-module(mustache). %% v0.1.0beta
-author("Tom Preston-Werner").
-export([compile/2, render/2, render/3, get/2, get/3, start/1]).
-export([compile/2, render/2, render/3, get/2, get/3, escape/1, start/1]).

-record(mstate, {mod = undefined,
section_re = undefined,
Expand Down Expand Up @@ -93,6 +93,9 @@ tag_kind(T, {K0, K1}) ->
string:substr(T, K0 + 1, K1).

compile_tag(none, Content, State) ->
Mod = State#mstate.mod,
"mustache:escape(mustache:get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ "))";
compile_tag("{", Content, State) ->
Mod = State#mstate.mod,
"mustache:get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ ")";
compile_tag("!", _Content, _State) ->
Expand Down Expand Up @@ -135,6 +138,20 @@ to_s(Val) when is_atom(Val) ->
to_s(Val) ->
Val.

escape(HTML) ->
escape(HTML, []).

escape([], Acc) ->
lists:reverse(Acc);
escape(["<" | Rest], Acc) ->
escape(Rest, lists:reverse("&lt;", Acc));
escape([">" | Rest], Acc) ->
escape(Rest, lists:reverse("&gt;", Acc));
escape(["&" | Rest], Acc) ->
escape(Rest, lists:reverse("&amp;", Acc));
escape([X | Rest], Acc) ->
escape(Rest, [X | Acc]).

%%---------------------------------------------------------------------------

start([T]) ->
Expand Down

0 comments on commit d89cbc1

Please sign in to comment.