Browse files

add unescaped tag functionality

  • Loading branch information...
1 parent 9e57917 commit d89cbc1f96c9f660682b2dd3727697226580f0fc @mojombo committed Dec 23, 2009
Showing with 31 additions and 1 deletion.
  1. +12 −0 examples/unescaped.erl
  2. +1 −0 examples/unescaped.mustache
  3. +18 −1 mustache.erl
View
12 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, []).
View
1 examples/unescaped.mustache
@@ -0,0 +1 @@
+<h1>{{{title}}}</h1>
View
19 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,
@@ -94,6 +94,9 @@ tag_kind(T, {K0, 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) ->
"[]".
@@ -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]) ->

0 comments on commit d89cbc1

Please sign in to comment.