Browse files

Added {% raw %}..{% endraw %} support. With thanks to Watt Poosanguan…

…sit.
  • Loading branch information...
1 parent d25b8fb commit 264b9dd492246702c42d01453b3536f113d00d96 @mworrell mworrell committed Jul 18, 2011
Showing with 77 additions and 0 deletions.
  1. +1 −0 CONTRIBUTORS
  2. +76 −0 src/erlydtl/erlydtl_scanner.erl
View
1 CONTRIBUTORS
@@ -28,3 +28,4 @@ Mikael Magnusson <mikma264@gmail.com>
Richard Fergie <richard.fergie@gmail.com>
Steve Strong <srstrong@gmail.com>
Tim Benniks <tim@timbenniks.nl>
+Watt Poosanguansit <wpoosanguansit@yahoo.com>
View
76 src/erlydtl/erlydtl_scanner.erl
@@ -108,6 +108,41 @@ scan([], _Scanned, _, {in_trans, _}) ->
scan([], _Scanned, _, _) ->
{error, "Reached end of file inside a code block."};
+%%we just capture the {% raw %} {% endraw %} tags and pass on string between the tags as is
+scan("{%" ++ T, Scanned, {Row, Column}, {in_raw, "{% endraw %}"}) ->
+ case find_endraw(T, "%}", Row, Column+2) of
+ {ok, T1, Row1, Column1} ->
+ scan(T1, Scanned, {Row1, Column1}, in_text);
+ notfound ->
+ Scanned1 = append_text_char(Scanned, {Row, Column}, ${),
+ Scanned2 = append_text_char(Scanned1, {Row, Column}, $%),
+ scan(T, Scanned2, {Row, Column + 2}, {in_raw, "{% endraw %}"})
+ end;
+scan("<!--{%" ++ T, Scanned, {Row, Column}, {in_raw, "<!--{% endraw %}-->"}) ->
+ case find_endraw(T, "%}-->", Row, Column+2) of
+ {ok, T1, Row1, Column1} ->
+ scan(T1, Scanned, {Row1, Column1}, in_text);
+ notfound ->
+ Scanned1 = append_text_char(Scanned, {Row, Column}, $<),
+ Scanned2 = append_text_char(Scanned1, {Row, Column}, $!),
+ Scanned3 = append_text_char(Scanned2, {Row, Column}, $-),
+ Scanned4 = append_text_char(Scanned3, {Row, Column}, $-),
+ Scanned5 = append_text_char(Scanned4, {Row, Column}, ${),
+ Scanned6 = append_text_char(Scanned5, {Row, Column}, $%),
+ scan(T, Scanned6, {Row, Column + 6}, {in_raw, "<!--{% endraw %}-->"})
+ end;
+
+scan("\r\n" ++ T, Scanned, {Row, Column}, {in_raw, Closer}) ->
+ Scanned1 = append_text_char(Scanned, {Row, Column}, $\r),
+ Scanned2 = append_text_char(Scanned1, {Row, Column}, $\n),
+ scan(T, Scanned2, {Row+1, 1}, {in_raw, Closer});
+
+scan("\n" ++ T, Scanned, {Row, Column}, {in_raw, Closer}) ->
+ scan(T, append_text_char(Scanned, {Row, Column}, $\n), {Row+1, 1}, {in_raw, Closer});
+
+scan([H | T], Scanned, {Row, Column}, {in_raw, Closer}) ->
+ scan(T, append_text_char(Scanned, {Row, Column}, H), {Row, Column + 1}, {in_raw, Closer});
+
scan("<!--{{" ++ T, Scanned, {Row, Column}, in_text) ->
scan(T, [{open_var, {Row, Column}, "<!--{{"} | Scanned], {Row, Column + length("<!--{{")}, {in_code, "}}-->"});
@@ -158,9 +193,17 @@ scan("{%" ++ T, Scanned, {Row, Column}, in_text) ->
scan([H | T], Scanned, {Row, Column}, {in_trans, Closer}) ->
scan(T, append_char(Scanned, H), {Row, Column + 1}, {in_trans, Closer});
+scan("\r\n" ++ T, Scanned, {Row, Column}, in_text) ->
+ Scanned1 = append_text_char(Scanned, {Row, Column}, $\r),
+ Scanned2 = append_text_char(Scanned1, {Row, Column}, $\n),
+ scan(T, Scanned2, {Row + 1, 1}, in_text);
+
scan("\n" ++ T, Scanned, {Row, Column}, in_text) ->
scan(T, append_text_char(Scanned, {Row, Column}, $\n), {Row + 1, 1}, in_text);
+scan("\r\n" ++ T, Scanned, {Row, _Column}, {in_code, Closer}) ->
+ scan(T, Scanned, {Row + 1, 1}, {in_code, Closer});
+
scan("\n" ++ T, Scanned, {Row, _Column}, {in_code, Closer}) ->
scan(T, Scanned, {Row + 1, 1}, {in_code, Closer});
@@ -212,10 +255,16 @@ scan([H | T], Scanned, {Row, Column}, {in_single_quote, Closer}) ->
scan(T, append_char(Scanned, H), {Row, Column + 1}, {in_single_quote, Closer});
% Closing code blocks
+scan("%}-->" ++ T, [{identifier,_,"war"},{open_tag,_,"%{--!<"}|Scanned], {Row, Column}, {_, "%}-->"}) ->
+ scan(T, Scanned, {Row, Column + 2}, {in_raw, "<!--{% endraw %}-->"});
+
scan("%}-->" ++ T, Scanned, {Row, Column}, {_, "%}-->"}) ->
scan(T, [{close_tag, {Row, Column}, lists:reverse("%}-->")} | Scanned],
{Row, Column + 2}, in_text);
+scan("%}" ++ T, [{identifier,_,"war"},{open_tag,_,"%{"}|Scanned], {Row, Column}, {_, "%}"}) ->
+ scan(T, Scanned, {Row, Column + 2}, {in_raw, "{% endraw %}"});
+
scan("%}" ++ T, Scanned, {Row, Column}, {_, "%}"}) ->
scan(T, [{close_tag, {Row, Column}, lists:reverse("%}")} | Scanned],
{Row, Column + 2}, in_text);
@@ -377,3 +426,30 @@ char_type(Char) ->
_ ->
undefined
end.
+
+
+% Find the 'endraw %}' tag
+find_endraw([C|Rest], Closer, Row, Column) when C == 9; C == 32 ->
+ find_endraw(Rest, Closer, Row, Column+1);
+find_endraw("\r\n" ++ Rest, Closer, Row, _Column) ->
+ find_endraw(Rest, Closer, Row+1, 1);
+find_endraw("\n" ++ Rest, Closer, Row, _Column) ->
+ find_endraw(Rest, Closer, Row+1, 1);
+find_endraw("endraw" ++ Rest, Closer, Row, Column) ->
+ find_endraw_close(Rest, Closer, Row, Column+6);
+find_endraw(_Rest, _Closer, _Row, _Column) ->
+ notfound.
+
+find_endraw_close([C|Rest], Closer, Row, Column) when C == 9; C == 32 ->
+ find_endraw_close(Rest, Closer, Row, Column+1);
+find_endraw_close("\r\n" ++ Rest, Closer, Row, _Column) ->
+ find_endraw_close(Rest, Closer, Row+1, 1);
+find_endraw_close("\n" ++ Rest, Closer, Row, _Column) ->
+ find_endraw_close(Rest, Closer, Row+1, 1);
+find_endraw_close("%}-->" ++ Rest, "%}-->", Row, Column) ->
+ {ok, Rest, Row, Column+5};
+find_endraw_close("%}" ++ Rest, "%}", Row, Column) ->
+ {ok, Rest, Row, Column+2};
+find_endraw_close(_T, _Closer, _Row, _Colum) ->
+ notfound.
+

0 comments on commit 264b9dd

Please sign in to comment.