Browse Source

new nitro

Namdak Tonpa 9 years ago
parent
commit
afee035c2a
5 changed files with 23 additions and 205 deletions
  1. 2 2
      README.md
  2. 2 2
      src/actions/action_wire.erl
  3. 1 1
      src/nitro.app.src
  4. 18 14
      src/nitro.erl
  5. 0 186
      src/nitro_mochinum.erl

+ 2 - 2
README.md

@@ -9,8 +9,8 @@ Features
 * Native Erlang way for HTML Templating
 * Tag Inheritance
 * Polymorphic Tuples
-* Compact size (3000 LOC)
-* As fast as DTL
+* Compact size (2500 LOC)
+* As fast as DTL and EEX
 
 Credits
 -------

+ 2 - 2
src/actions/action_wire.erl

@@ -7,5 +7,5 @@ render_action(#wire{actions=Actions}) -> nitro:render(Actions);
 render_action(S) when is_list(S) -> S;
 render_action(_) -> [].
 
-wire(Actions) -> Actions = case get(actions) of undefined -> []; E -> E end,
-                 put(actions,Actions++[#wire{actions=Actions}]).
+wire(A) -> Actions = case get(actions) of undefined -> []; E -> E end,
+           put(actions,Actions++[#wire{actions=A}]).

+ 1 - 1
src/nitro.app.src

@@ -1,6 +1,6 @@
 {application, nitro, [
     {description,  "NITRO HTML5 DSL"},
-    {vsn,          "0.8"},
+    {vsn,          "0.9"},
     {applications, [kernel, stdlib]},
     {modules, []},
     {registered,   []},

+ 18 - 14
src/nitro.erl

@@ -30,28 +30,29 @@ js_escape(<<"script>", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "scr
 js_escape(<<C, Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, C>>);
 js_escape(<<>>, Acc) -> Acc.
 
-to_binary(A) when is_atom(A) -> atom_to_binary(A,latin1);
-to_binary(B) when is_binary(B) -> B;
-to_binary(I) when is_integer(I) -> to_binary(integer_to_list(I));
-to_binary(F) when is_float(F) -> to_binary(nitro_mochinum:digits(F));
-to_binary(L) when is_list(L) ->  iolist_to_binary(L). % unicode:characters_to_binary(L).
-
 -define(IS_STRING(Term), (is_list(Term) andalso Term /= [] andalso is_integer(hd(Term)))).
 
 to_list(L) when ?IS_STRING(L) -> L;
 to_list(L) when is_list(L) -> SubLists = [inner_to_list(X) || X <- L], lists:flatten(SubLists);
 to_list(A) -> inner_to_list(A).
-
 inner_to_list(A) when is_atom(A) -> atom_to_list(A);
 inner_to_list(B) when is_binary(B) -> binary_to_list(B);
 inner_to_list(I) when is_integer(I) -> integer_to_list(I);
 inner_to_list(L) when is_tuple(L) -> lists:flatten(io_lib:format("~p", [L]));
 inner_to_list(L) when is_list(L) -> L;
-inner_to_list(F) when is_float(F) ->
-    case F == round(F) of
-        true -> inner_to_list(round(F));
-        false -> nitro_mochinum:digits(F) end.
+inner_to_list(F) when is_float(F) -> float_to_list(F,[{decimals,9},compact]).
 
+to_atom(A) when is_atom(A) -> A;
+to_atom(B) when is_binary(B) -> to_atom(binary_to_list(B));
+to_atom(I) when is_integer(I) -> to_atom(integer_to_list(I));
+to_atom(F) when is_float(F) -> to_atom(float_to_list(F,[{decimals,9},compact]));
+to_atom(L) when is_list(L) -> list_to_atom(binary_to_list(list_to_binary(L))).
+
+to_binary(A) when is_atom(A) -> atom_to_binary(A,latin1);
+to_binary(B) when is_binary(B) -> B;
+to_binary(I) when is_integer(I) -> to_binary(integer_to_list(I));
+to_binary(F) when is_float(F) -> float_to_binary(F,[{decimals,9},compact]);
+to_binary(L) when is_list(L) ->  iolist_to_binary(L).
 
 -ifndef(PICKLER).
 -define(PICKLER, (application:get_env(n2o,pickler,nitro_pickle))).
@@ -69,9 +70,9 @@ temp_id() -> {_, _, C} = now(), "auto" ++ integer_to_list(C).
 html_encode(L,Fun) when is_function(Fun) -> Fun(L);
 html_encode(L,EncType) when is_atom(L) -> html_encode(nitro:to_list(L),EncType);
 html_encode(L,EncType) when is_integer(L) -> html_encode(integer_to_list(L),EncType);
-html_encode(L,EncType) when is_float(L) -> html_encode(nitro_mochinum:digits(L),EncType);
-html_encode(L, false) -> L; %wf:to_list(lists:flatten([L]));
-html_encode(L, true) -> L; %html_encode(wf:to_list(lists:flatten([L])));
+html_encode(L,EncType) when is_float(L) -> html_encode(float_to_list(L,[{decimals,9},compact]),EncType);
+html_encode(L, false) -> L;
+html_encode(L, true) -> L;
 html_encode(L, whites) -> html_encode_whites(nitro:to_list(lists:flatten([L]))).
 html_encode(<<>>) -> [];
 html_encode([]) -> [];
@@ -104,3 +105,6 @@ html_encode_whites([H|T]) ->
 		$\n -> "<br>" ++ html_encode_whites(T);
 		_ -> [H|html_encode_whites(T)]
 	end.
+
+script() -> get(script).
+script(Script) -> put(script,Script).

+ 0 - 186
src/nitro_mochinum.erl

@@ -1,186 +0,0 @@
--module(nitro_mochinum).
--author('Bob Ippolito').
--compile(export_all).
-
-%% IEEE 754 Float exponent bias
--define(FLOAT_BIAS, 1022).
--define(MIN_EXP, -1074).
--define(BIG_POW, 4503599627370496).
-
-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.
-
-frexp(F) -> frexp1(unpack(F)).
-
-int_pow(_X, 0) -> 1;
-int_pow(X, N) when N > 0 -> int_pow(X, N, 1).
-int_ceil(X) -> T = trunc(X), case (X - T) of Pos when Pos > 0 -> T + 1; _ -> T end.
-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.
-
-