Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make it release ready

  • Loading branch information...
commit 0b0329ca06bcd37626cfeb657362f24d35406740 1 parent 7dc2163
Devin Torres authored
View
9 LICENSE
@@ -0,0 +1,9 @@
+This is the MIT license.
+
+Copyright (c) 2007 Mochi Media, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 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 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.
View
69 README.md
@@ -1,4 +1,67 @@
-Walrus - An Erlang Mustache Compiler
-====================================
+Walrus - Mustache-like Templating
+=================================
-TODO: Write the compiler :]
+Walrus is 50% Mustache and 50% large flippered marine mammal.
+
+Lexing is done with Leex and parsing is done with Yecc, both
+of which are included in newer versions of Erlang.
+
+Most Mustache constructs work, such as variables, unescaped
+variables, blocks, and inverse blocks. However, The Walrus is
+an opinionated animal, and partials will never be supported.
+Also, functions passed into the context are simple evaluated
+and are not passed the raw template.
+
+There is no real "compilation" step, however you can skip
+lexing and parsing templates every time you want to render
+them by using `compile/1`, which returns a lambda you can
+then pass a context that renders the template.
+
+Examples
+--------
+
+### Simple `render`
+
+ 1> Tmpl = "Hello {{{name}}}.
+ 1>
+ 1> Drinks:
+ 1>
+ 1> {{#drinks}}
+ 1> - {{name}}, {{tastiness}}
+ 1> {{/drinks}}".
+ "Hello {{{name}}}.\n\nDrinks:\n\n{{#drinks}}\n - {{name}}, {{tastiness}}\n{{/drinks}}"
+ 2>
+ 2> Ctx = [{name, "Devin & Jane"},
+ 2> {drinks, [[{name, "Beer"},
+ 2> {tastiness, 5}],
+ 2> [{name, "Juice"},
+ 2> {tastiness, 8}]]}].
+ [{name,"Devin & Jane"},
+ {drinks,[[{name,"Beer"},{tastiness,5}],
+ [{name,"Juice"},{tastiness,8}]]}]
+ 3>
+ 3> walrus:render(Tmpl, Ctx).
+ <<"Hello Devin & Jane.\n\nDrinks:\n\n\n - Beer, 5\n\n - Juice, 8\n">>
+
+### "Compiled" `fun`
+
+ 1> Tmpl = walrus:compile("Hello {{{name}}}.\n\nDrinks:\n\n{{#drinks}}\n - {{name}}, {{tastiness}}\n{{/drinks}}").
+ #Fun<walrus.0.90102984>
+ 2> Ctx = [{name, "Devin & Jane"},
+ 2> {drinks, [[{name, "Beer"},
+ 2> {tastiness, 5}],
+ 2> [{name, "Juice"},
+ 2> {tastiness, 8}]]}].
+ [{name,"Devin & Jane"},
+ {drinks,[[{name,"Beer"},{tastiness,5}],
+ [{name,"Juice"},{tastiness,8}]]}]
+ 3>
+ 3> Tmpl(Ctx).
+ <<"Hello Devin & Jane.\n\nDrinks:\n\n\n - Beer, 5\n\n - Juice, 8\n">>
+
+License
+-------
+
+All code released into the public domain (see `UNLICENSE`)
+except for the file `mochinum.erl`, which has it's own
+license (see `LICENSE`).
View
24 UNLICENSE
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+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 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.
+
+For more information, please refer to <http://unlicense.org/>
View
BIN  rebar
Binary file not shown
View
354 src/mochinum.erl
@@ -0,0 +1,354 @@
+%% @copyright 2007 Mochi Media, Inc.
+%% @author Bob Ippolito <bob@mochimedia.com>
+
+%% @doc Useful numeric algorithms for floats that cover some deficiencies
+%% in the math module. More interesting is digits/1, which implements
+%% the algorithm from:
+%% http://www.cs.indiana.edu/~burger/fp/index.html
+%% See also "Printing Floating-Point Numbers Quickly and Accurately"
+%% in Proceedings of the SIGPLAN '96 Conference on Programming Language
+%% Design and Implementation.
+
+-module(mochinum).
+-author("Bob Ippolito <bob@mochimedia.com>").
+-export([digits/1, frexp/1, int_pow/2, int_ceil/1]).
+
+%% IEEE 754 Float exponent bias
+-define(FLOAT_BIAS, 1022).
+-define(MIN_EXP, -1074).
+-define(BIG_POW, 4503599627370496).
+
+%% External API
+
+%% @spec digits(number()) -> string()
+%% @doc Returns a string that accurately represents the given integer or float
+%% using a conservative amount of digits. Great for generating
+%% human-readable output, or compact ASCII serializations for floats.
+digits(N) when is_integer(N) ->
+ integer_to_list(N);
+digits(0.0) ->
+ "0.0";
+digits(Float) ->
+ {Frac1, Exp1} = frexp_int(Float),
+ [Place0 | Digits0] = digits1(Float, Exp1, Frac1),
+ {Place, Digits} = transform_digits(Place0, Digits0),
+ R = insert_decimal(Place, Digits),
+ case Float < 0 of
+ true ->
+ [$- | R];
+ _ ->
+ R
+ end.
+
+%% @spec frexp(F::float()) -> {Frac::float(), Exp::float()}
+%% @doc Return the fractional and exponent part of an IEEE 754 double,
+%% equivalent to the libc function of the same name.
+%% F = Frac * pow(2, Exp).
+frexp(F) ->
+ frexp1(unpack(F)).
+
+%% @spec int_pow(X::integer(), N::integer()) -> Y::integer()
+%% @doc Moderately efficient way to exponentiate integers.
+%% int_pow(10, 2) = 100.
+int_pow(_X, 0) ->
+ 1;
+int_pow(X, N) when N > 0 ->
+ int_pow(X, N, 1).
+
+%% @spec int_ceil(F::float()) -> integer()
+%% @doc Return the ceiling of F as an integer. The ceiling is defined as
+%% F when F == trunc(F);
+%% trunc(F) when F &lt; 0;
+%% trunc(F) + 1 when F &gt; 0.
+int_ceil(X) ->
+ T = trunc(X),
+ case (X - T) of
+ Pos when Pos > 0 -> T + 1;
+ _ -> T
+ end.
+
+
+%% Internal API
+
+int_pow(X, N, R) when N < 2 ->
+ R * X;
+int_pow(X, N, R) ->
+ int_pow(X * X, N bsr 1, case N band 1 of 1 -> R * X; 0 -> R end).
+
+insert_decimal(0, S) ->
+ "0." ++ S;
+insert_decimal(Place, S) when Place > 0 ->
+ L = length(S),
+ case Place - L of
+ 0 ->
+ S ++ ".0";
+ N when N < 0 ->
+ {S0, S1} = lists:split(L + N, S),
+ S0 ++ "." ++ S1;
+ N when N < 6 ->
+ %% More places than digits
+ S ++ lists:duplicate(N, $0) ++ ".0";
+ _ ->
+ insert_decimal_exp(Place, S)
+ end;
+insert_decimal(Place, S) when Place > -6 ->
+ "0." ++ lists:duplicate(abs(Place), $0) ++ S;
+insert_decimal(Place, S) ->
+ insert_decimal_exp(Place, S).
+
+insert_decimal_exp(Place, S) ->
+ [C | S0] = S,
+ S1 = case S0 of
+ [] ->
+ "0";
+ _ ->
+ S0
+ end,
+ Exp = case Place < 0 of
+ true ->
+ "e-";
+ false ->
+ "e+"
+ end,
+ [C] ++ "." ++ S1 ++ Exp ++ integer_to_list(abs(Place - 1)).
+
+
+digits1(Float, Exp, Frac) ->
+ Round = ((Frac band 1) =:= 0),
+ case Exp >= 0 of
+ true ->
+ BExp = 1 bsl Exp,
+ case (Frac =/= ?BIG_POW) of
+ true ->
+ scale((Frac * BExp * 2), 2, BExp, BExp,
+ Round, Round, Float);
+ false ->
+ scale((Frac * BExp * 4), 4, (BExp * 2), BExp,
+ Round, Round, Float)
+ end;
+ false ->
+ case (Exp =:= ?MIN_EXP) orelse (Frac =/= ?BIG_POW) of
+ true ->
+ scale((Frac * 2), 1 bsl (1 - Exp), 1, 1,
+ Round, Round, Float);
+ false ->
+ scale((Frac * 4), 1 bsl (2 - Exp), 2, 1,
+ Round, Round, Float)
+ end
+ end.
+
+scale(R, S, MPlus, MMinus, LowOk, HighOk, Float) ->
+ Est = int_ceil(math:log10(abs(Float)) - 1.0e-10),
+ %% Note that the scheme implementation uses a 326 element look-up table
+ %% for int_pow(10, N) where we do not.
+ case Est >= 0 of
+ true ->
+ fixup(R, S * int_pow(10, Est), MPlus, MMinus, Est,
+ LowOk, HighOk);
+ false ->
+ Scale = int_pow(10, -Est),
+ fixup(R * Scale, S, MPlus * Scale, MMinus * Scale, Est,
+ LowOk, HighOk)
+ end.
+
+fixup(R, S, MPlus, MMinus, K, LowOk, HighOk) ->
+ TooLow = case HighOk of
+ true ->
+ (R + MPlus) >= S;
+ false ->
+ (R + MPlus) > S
+ end,
+ case TooLow of
+ true ->
+ [(K + 1) | generate(R, S, MPlus, MMinus, LowOk, HighOk)];
+ false ->
+ [K | generate(R * 10, S, MPlus * 10, MMinus * 10, LowOk, HighOk)]
+ end.
+
+generate(R0, S, MPlus, MMinus, LowOk, HighOk) ->
+ D = R0 div S,
+ R = R0 rem S,
+ TC1 = case LowOk of
+ true ->
+ R =< MMinus;
+ false ->
+ R < MMinus
+ end,
+ TC2 = case HighOk of
+ true ->
+ (R + MPlus) >= S;
+ false ->
+ (R + MPlus) > S
+ end,
+ case TC1 of
+ false ->
+ case TC2 of
+ false ->
+ [D | generate(R * 10, S, MPlus * 10, MMinus * 10,
+ LowOk, HighOk)];
+ true ->
+ [D + 1]
+ end;
+ true ->
+ case TC2 of
+ false ->
+ [D];
+ true ->
+ case R * 2 < S of
+ true ->
+ [D];
+ false ->
+ [D + 1]
+ end
+ end
+ end.
+
+unpack(Float) ->
+ <<Sign:1, Exp:11, Frac:52>> = <<Float:64/float>>,
+ {Sign, Exp, Frac}.
+
+frexp1({_Sign, 0, 0}) ->
+ {0.0, 0};
+frexp1({Sign, 0, Frac}) ->
+ Exp = log2floor(Frac),
+ <<Frac1:64/float>> = <<Sign:1, ?FLOAT_BIAS:11, (Frac-1):52>>,
+ {Frac1, -(?FLOAT_BIAS) - 52 + Exp};
+frexp1({Sign, Exp, Frac}) ->
+ <<Frac1:64/float>> = <<Sign:1, ?FLOAT_BIAS:11, Frac:52>>,
+ {Frac1, Exp - ?FLOAT_BIAS}.
+
+log2floor(Int) ->
+ log2floor(Int, 0).
+
+log2floor(0, N) ->
+ N;
+log2floor(Int, N) ->
+ log2floor(Int bsr 1, 1 + N).
+
+
+transform_digits(Place, [0 | Rest]) ->
+ transform_digits(Place, Rest);
+transform_digits(Place, Digits) ->
+ {Place, [$0 + D || D <- Digits]}.
+
+
+frexp_int(F) ->
+ case unpack(F) of
+ {_Sign, 0, Frac} ->
+ {Frac, ?MIN_EXP};
+ {_Sign, Exp, Frac} ->
+ {Frac + (1 bsl 52), Exp - 53 - ?FLOAT_BIAS}
+ end.
+
+%%
+%% Tests
+%%
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+int_ceil_test() ->
+ ?assertEqual(1, int_ceil(0.0001)),
+ ?assertEqual(0, int_ceil(0.0)),
+ ?assertEqual(1, int_ceil(0.99)),
+ ?assertEqual(1, int_ceil(1.0)),
+ ?assertEqual(-1, int_ceil(-1.5)),
+ ?assertEqual(-2, int_ceil(-2.0)),
+ ok.
+
+int_pow_test() ->
+ ?assertEqual(1, int_pow(1, 1)),
+ ?assertEqual(1, int_pow(1, 0)),
+ ?assertEqual(1, int_pow(10, 0)),
+ ?assertEqual(10, int_pow(10, 1)),
+ ?assertEqual(100, int_pow(10, 2)),
+ ?assertEqual(1000, int_pow(10, 3)),
+ ok.
+
+digits_test() ->
+ ?assertEqual("0",
+ digits(0)),
+ ?assertEqual("0.0",
+ digits(0.0)),
+ ?assertEqual("1.0",
+ digits(1.0)),
+ ?assertEqual("-1.0",
+ digits(-1.0)),
+ ?assertEqual("0.1",
+ digits(0.1)),
+ ?assertEqual("0.01",
+ digits(0.01)),
+ ?assertEqual("0.001",
+ digits(0.001)),
+ ?assertEqual("1.0e+6",
+ digits(1000000.0)),
+ ?assertEqual("0.5",
+ digits(0.5)),
+ ?assertEqual("4503599627370496.0",
+ digits(4503599627370496.0)),
+ %% small denormalized number
+ %% 4.94065645841246544177e-324 =:= 5.0e-324
+ <<SmallDenorm/float>> = <<0,0,0,0,0,0,0,1>>,
+ ?assertEqual("5.0e-324",
+ digits(SmallDenorm)),
+ ?assertEqual(SmallDenorm,
+ list_to_float(digits(SmallDenorm))),
+ %% large denormalized number
+ %% 2.22507385850720088902e-308
+ <<BigDenorm/float>> = <<0,15,255,255,255,255,255,255>>,
+ ?assertEqual("2.225073858507201e-308",
+ digits(BigDenorm)),
+ ?assertEqual(BigDenorm,
+ list_to_float(digits(BigDenorm))),
+ %% small normalized number
+ %% 2.22507385850720138309e-308
+ <<SmallNorm/float>> = <<0,16,0,0,0,0,0,0>>,
+ ?assertEqual("2.2250738585072014e-308",
+ digits(SmallNorm)),
+ ?assertEqual(SmallNorm,
+ list_to_float(digits(SmallNorm))),
+ %% large normalized number
+ %% 1.79769313486231570815e+308
+ <<LargeNorm/float>> = <<127,239,255,255,255,255,255,255>>,
+ ?assertEqual("1.7976931348623157e+308",
+ digits(LargeNorm)),
+ ?assertEqual(LargeNorm,
+ list_to_float(digits(LargeNorm))),
+ %% issue #10 - mochinum:frexp(math:pow(2, -1074)).
+ ?assertEqual("5.0e-324",
+ digits(math:pow(2, -1074))),
+ ok.
+
+frexp_test() ->
+ %% zero
+ ?assertEqual({0.0, 0}, frexp(0.0)),
+ %% one
+ ?assertEqual({0.5, 1}, frexp(1.0)),
+ %% negative one
+ ?assertEqual({-0.5, 1}, frexp(-1.0)),
+ %% small denormalized number
+ %% 4.94065645841246544177e-324
+ <<SmallDenorm/float>> = <<0,0,0,0,0,0,0,1>>,
+ ?assertEqual({0.5, -1073}, frexp(SmallDenorm)),
+ %% large denormalized number
+ %% 2.22507385850720088902e-308
+ <<BigDenorm/float>> = <<0,15,255,255,255,255,255,255>>,
+ ?assertEqual(
+ {0.99999999999999978, -1022},
+ frexp(BigDenorm)),
+ %% small normalized number
+ %% 2.22507385850720138309e-308
+ <<SmallNorm/float>> = <<0,16,0,0,0,0,0,0>>,
+ ?assertEqual({0.5, -1021}, frexp(SmallNorm)),
+ %% large normalized number
+ %% 1.79769313486231570815e+308
+ <<LargeNorm/float>> = <<127,239,255,255,255,255,255,255>>,
+ ?assertEqual(
+ {0.99999999999999989, 1024},
+ frexp(LargeNorm)),
+ %% issue #10 - mochinum:frexp(math:pow(2, -1074)).
+ ?assertEqual(
+ {0.5, -1073},
+ frexp(math:pow(2, -1074))),
+ ok.
+
+-endif.
View
78 src/walrus.erl
@@ -1,9 +1,17 @@
-module(walrus).
+-author("Devin Torres <devin@devintorres.com>").
--export([render/2]).
+-export([compile/1, render/2]).
-define(is_falsy(V),
- (V =:= undefined orelse V =:= null orelse V =:= false orelse V =:= [])).
+ (V =:= false orelse V =:= [] orelse V =:= undefined orelse V =:= null)).
+
+compile(Template) ->
+ {ok, Tokens, _} = walrus_lexer:string(Template),
+ {ok, ParseTree} = walrus_parser:parse(Tokens),
+ fun (Context) ->
+ render(ParseTree, Context, [])
+ end.
render(Template, Context) ->
{ok, Tokens, _} = walrus_lexer:string(Template),
@@ -12,26 +20,68 @@ render(Template, Context) ->
render([{text, Text} | ParseTree], Context, Acc) ->
render(ParseTree, Context, [Text | Acc]);
-render([{var, Var} | ParseTree], Context, Acc) ->
- Val = proplists:get_value(Var, Context),
- render(ParseTree, Context, [Val | Acc]);
+render([{var, Key} | ParseTree], Context, Acc) ->
+ Value = get(Key, Context),
+ render(ParseTree, Context, [stringify(Value, Context, true) | Acc]);
+render([{var_unescaped, Key} | ParseTree], Context, Acc) ->
+ Value = get(Key, Context),
+ render(ParseTree, Context, [stringify(Value, Context, false) | Acc]);
render([{block, Key, SubParseTree} | ParseTree], Context, Acc) ->
- Val = proplists:get_value(Key, Context),
- case Val of
- V when ?is_falsy(V) ->
+ Value = get(Key, Context),
+ case Value of
+ Val when ?is_falsy(Val) ->
render(ParseTree, Context, Acc);
- Ctx ->
- Tmpl = render(SubParseTree, Ctx, []),
+ Val when is_list(Val) ->
+ Tmpl = [render(SubParseTree, Ctx, []) || Ctx <- Val],
+ render(ParseTree, Context, [Tmpl | Acc]);
+ _ ->
+ Tmpl = render(SubParseTree, Context, []),
render(ParseTree, Context, [Tmpl | Acc])
end;
render([{inverse, Key, SubParseTree} | ParseTree], Context, Acc) ->
- Val = proplists:get_value(Key, Context),
- case Val of
- V when ?is_falsy(V) ->
+ Value = get(Key, Context),
+ case Value of
+ Val when ?is_falsy(Val) ->
Tmpl = render(SubParseTree, Context, []),
render(ParseTree, Context, [Tmpl | Acc]);
_ ->
render(ParseTree, Context, Acc)
end;
render([], _Context, Acc) ->
- lists:flatten(lists:reverse(Acc)).
+ iolist_to_binary(lists:reverse(Acc)).
+
+get(Key, Context) ->
+ Value = proplists:get_value(Key, Context),
+ if
+ is_function(Value) -> Value(Context);
+ true -> Value
+ end.
+
+stringify(Value, _Context, _Escape) when is_integer(Value) ->
+ integer_to_list(Value);
+stringify(Value, _Context, _Escape) when is_float(Value) ->
+ mochinum:digits(Value);
+stringify(Value, Context, Escape) when is_function(Value) ->
+ stringify(Value(Context), Context, Escape);
+stringify(Value, _Context, true) when is_atom(Value) ->
+ escape(atom_to_list(Value));
+stringify(Value, _Context, false) when is_atom(Value) ->
+ atom_to_list(Value);
+stringify(Value, _Context, true) ->
+ escape(Value);
+stringify(Value, _Context, false) ->
+ Value.
+
+escape(Value) ->
+ escape(Value, []).
+
+escape([$< | Tail], Acc) ->
+ escape(Tail, [<<"&lt;">> | Acc]);
+escape([$> | Tail], Acc) ->
+ escape(Tail, [<<"&gt;">> | Acc]);
+escape([$& | Tail], Acc) ->
+ escape(Tail, [<<"&amp;">> | Acc]);
+escape([C | Tail], Acc) ->
+ escape(Tail, [C | Acc]);
+escape([], Acc) ->
+ lists:reverse(Acc).
View
7 src/walrus_lexer.xrl
@@ -11,11 +11,12 @@ Rules.
{{/ : {token,{'{{/',TokenLine}}.
{{\^ : {token,{'{{^',TokenLine}}.
{{{ : {token,{'{{{',TokenLine}}.
-\s*{K}\s*}} : {token,{key,TokenLine,?to_key(TokenChars,TokenLen)},"}}"}.
+\s*{K}\s*}} : {token,{key,TokenLine,key(TokenChars,TokenLen)},"}}"}.
}} : {token,{'}}',TokenLine}}.
}}} : {token,{'}}}',TokenLine}}.
Erlang code.
--define(to_key(TokenChars, TokenLen),
- (list_to_atom(string:strip(string:left(TokenChars, TokenLen-2))))).
+-compile({inline, key/2}).
+key(TokenChars, TokenLen) ->
+ list_to_atom(string:strip(string:left(TokenChars, TokenLen-2))).
View
34 src/walrus_parser.yrl
@@ -7,22 +7,40 @@ Rootsymbol template.
template -> token : ['$1'].
template -> token template : ['$1' | '$2'].
-token -> text : {text, ?value_of('$1')}.
+token -> text : {text, value_of('$1')}.
token -> var : '$1'.
token -> block : '$1'.
token -> inverse : '$1'.
-var -> '{{' key '}}' : {var, ?value_of('$2')}.
-var -> '{{{' key '}}}' : {var_unescaped, ?value_of('$2')}.
+var -> '{{' key '}}' : {var, value_of('$2')}.
+var -> '{{{' key '}}}' : {var_unescaped, value_of('$2')}.
block -> '{{#' key '}}' template '{{/' key '}}'
- : {block, ?keys_match(?value_of('$2'), ?value_of('$6')), '$4'}.
+ : {block, match_keys('$2', '$6'), '$4'}.
inverse -> '{{^' key '}}' template '{{/' key '}}'
- : {inverse, ?keys_match(?value_of('$2'), ?value_of('$6')), '$4'}.
+ : {inverse, match_keys('$2', '$6'), '$4'}.
Erlang code.
--define(value_of(Token), (element(3, Token))).
--define(keys_match(K1, K2),
- (if K1 =/= K2 -> throw("keys don't match"); true -> K1 end)).
+-compile({inline, line_of/1}).
+line_of(Token) ->
+ element(2, Token).
+
+-compile({inline, value_of/1}).
+value_of(Token) ->
+ element(3, Token).
+
+-compile({inline, match_keys/2}).
+match_keys(Tok1, Tok2) ->
+ K1 = value_of(Tok1),
+ K2 = value_of(Tok2),
+ if
+ K1 =/= K2 ->
+ L1 = integer_to_list(line_of(Tok1)),
+ L2 = integer_to_list(line_of(Tok2)),
+ throw("Missed an opening or closing of a section. "
+ ++ atom_to_list(K1) ++ " =/= " ++ atom_to_list(K2)
+ ++ " (lines " ++ L1 ++ ", " ++ L2 ++ ")");
+ true -> K1
+ end.
View
13 test/compile_test.erl
@@ -0,0 +1,13 @@
+-module(compile_test).
+
+-compile([export_all]).
+
+basic_test() ->
+ Template = walrus:compile("Hello {{{name}}}.\n\nDrinks:\n\n{{#drinks}}\n - {{name}}, {{tastiness}}\n{{/drinks}}"),
+ Context = [{name, "Devin & Jane"},
+ {drinks, [[{name, "Beer"},
+ {tastiness, 5}],
+ [{name, "Juice"},
+ {tastiness, 8}]]}],
+ Expected = <<"Hello Devin & Jane.\n\nDrinks:\n\n\n - Beer, 5\n\n - Juice, 8\n">>,
+ Expected = Template(Context).
View
2  test/lexer_test.erl
@@ -27,4 +27,4 @@ basic_test() ->
{key,3,over18},
{'}}',3},
{text,3,"?"}],3},
- Expected = walrus_lexer:string(Tmpl).
+ Expected = walrus_lexer:string(Tmpl).
View
13 test/render_test.erl
@@ -0,0 +1,13 @@
+-module(render_test).
+
+-compile([export_all]).
+
+basic_test() ->
+ Template = "Hello {{{name}}}.\n\nDrinks:\n\n{{#drinks}}\n - {{name}}, {{tastiness}}\n{{/drinks}}",
+ Context = [{name, "Devin & Jane"},
+ {drinks, [[{name, "Beer"},
+ {tastiness, 5}],
+ [{name, "Juice"},
+ {tastiness, 8}]]}],
+ Expected = <<"Hello Devin & Jane.\n\nDrinks:\n\n\n - Beer, 5\n\n - Juice, 8\n">>,
+ Expected = walrus:render(Template, Context).
Please sign in to comment.
Something went wrong with that request. Please try again.