nitro_conv.erl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. -module(nitro_conv).
  2. -description('N2O Formatter: JSON, BERT').
  3. -author('Maxim Sokhatsky').
  4. -compile(export_all).
  5. -include_lib("nitro/include/nitro.hrl").
  6. % WF to_atom to_list to_binary
  7. -define(IS_STRING(Term), (is_list(Term) andalso Term /= [] andalso is_integer(hd(Term)))).
  8. -ifndef(N2O_JSON).
  9. -define(N2O_JSON, (application:get_env(neo,json,jsone))).
  10. -endif.
  11. to_list(L) when ?IS_STRING(L) -> L;
  12. to_list(L) when is_list(L) -> SubLists = [inner_to_list(X) || X <- L], lists:flatten(SubLists);
  13. to_list(A) -> inner_to_list(A).
  14. inner_to_list(A) when is_atom(A) -> atom_to_list(A);
  15. inner_to_list(B) when is_binary(B) -> binary_to_list(B);
  16. inner_to_list(I) when is_integer(I) -> integer_to_list(I);
  17. inner_to_list(L) when is_tuple(L) -> lists:flatten(io_lib:format("~p", [L]));
  18. inner_to_list(L) when is_list(L) -> L;
  19. inner_to_list(F) when is_float(F) -> float_to_list(F,[{decimals,9},compact]).
  20. to_atom(A) when is_atom(A) -> A;
  21. to_atom(B) when is_binary(B) -> to_atom(binary_to_list(B));
  22. to_atom(I) when is_integer(I) -> to_atom(integer_to_list(I));
  23. to_atom(F) when is_float(F) -> to_atom(float_to_list(F,[{decimals,9},compact]));
  24. to_atom(L) when is_list(L) -> list_to_atom(binary_to_list(list_to_binary(L))).
  25. to_binary(A) when is_atom(A) -> atom_to_binary(A,latin1);
  26. to_binary(B) when is_binary(B) -> B;
  27. to_binary(T) when is_tuple(T) -> term_to_binary(T);
  28. to_binary(I) when is_integer(I) -> to_binary(integer_to_list(I));
  29. to_binary(F) when is_float(F) -> float_to_binary(F,[{decimals,9},compact]);
  30. to_binary(L) when is_list(L) -> iolist_to_binary(L).
  31. to_integer(A) when is_atom(A) -> to_integer(atom_to_list(A));
  32. to_integer(B) when is_binary(B) -> to_integer(binary_to_list(B));
  33. to_integer(I) when is_integer(I) -> I;
  34. to_integer([]) -> 0;
  35. to_integer(L) when is_list(L) -> list_to_integer(L);
  36. to_integer(F) when is_float(F) -> round(F).
  37. % HTML encode/decode
  38. html_encode(L,Fun) when is_function(Fun) -> Fun(L);
  39. html_encode(L,EncType) when is_atom(L) -> html_encode(nitro:to_list(L),EncType);
  40. html_encode(L,EncType) when is_integer(L) -> html_encode(integer_to_list(L),EncType);
  41. html_encode(L,EncType) when is_float(L) -> html_encode(float_to_list(L,[{decimals,9},compact]),EncType);
  42. html_encode(L, false) -> L;
  43. html_encode(L, true) -> L;
  44. html_encode(L, whites) -> html_encode_whites(nitro:to_list(lists:flatten([L]))).
  45. html_encode(<<>>) -> [];
  46. html_encode([]) -> [];
  47. html_encode([$\n|T]) -> "<br>" ++ html_encode(T);
  48. html_encode([H|T]) ->
  49. case H of
  50. $< -> "&lt;" ++ html_encode(T);
  51. $> -> "&gt;" ++ html_encode(T);
  52. $" -> "&quot;" ++ html_encode(T);
  53. $' -> "&#39;" ++ html_encode(T);
  54. $& -> "&amp;" ++ html_encode(T);
  55. BigNum when is_integer(BigNum) andalso BigNum > 255 ->
  56. %% Any integers above 255 are converted to their HTML encode equivilant,
  57. %% Example: 7534 gets turned into &#7534;
  58. [$&,$# | nitro:to_list(BigNum)] ++ ";" ++ html_encode(T);
  59. Tup when is_tuple(Tup) ->
  60. throw({html_encode,encountered_tuple,Tup});
  61. _ -> [H|html_encode(T)]
  62. end.
  63. html_encode_whites([]) -> [];
  64. html_encode_whites([H|T]) ->
  65. case H of
  66. $\s -> "&nbsp;" ++ html_encode_whites(T);
  67. $\t -> "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" ++ html_encode_whites(T);
  68. $< -> "&lt;" ++ html_encode_whites(T);
  69. $> -> "&gt;" ++ html_encode_whites(T);
  70. $" -> "&quot;" ++ html_encode_whites(T);
  71. $' -> "&#39;" ++ html_encode_whites(T);
  72. $& -> "&amp;" ++ html_encode_whites(T);
  73. $\n -> "<br>" ++ html_encode_whites(T);
  74. _ -> [H|html_encode_whites(T)]
  75. end.
  76. %% URL encode/decode
  77. url_encode(S) -> quote_plus(S).
  78. url_decode(S) -> unquote(S).
  79. -define(PERCENT, 37). % $\%
  80. -define(FULLSTOP, 46). % $\.
  81. -define(IS_HEX(C), ((C >= $0 andalso C =< $9) orelse
  82. (C >= $a andalso C =< $f) orelse
  83. (C >= $A andalso C =< $F))).
  84. -define(QS_SAFE(C), ((C >= $a andalso C =< $z) orelse
  85. (C >= $A andalso C =< $Z) orelse
  86. (C >= $0 andalso C =< $9) orelse
  87. (C =:= ?FULLSTOP orelse C =:= $- orelse C =:= $~ orelse
  88. C =:= $_))).
  89. quote_plus(Atom) when is_atom(Atom) -> quote_plus(atom_to_list(Atom));
  90. quote_plus(Int) when is_integer(Int) -> quote_plus(integer_to_list(Int));
  91. quote_plus(Bin) when is_binary(Bin) -> quote_plus(binary_to_list(Bin));
  92. quote_plus(String) -> quote_plus(String, []).
  93. quote_plus([], Acc) -> lists:reverse(Acc);
  94. quote_plus([C | Rest], Acc) when ?QS_SAFE(C) -> quote_plus(Rest, [C | Acc]);
  95. quote_plus([$\s | Rest], Acc) -> quote_plus(Rest, [$+ | Acc]);
  96. quote_plus([C | Rest], Acc) -> <<Hi:4, Lo:4>> = <<C>>, quote_plus(Rest, [digit(Lo), digit(Hi), ?PERCENT | Acc]).
  97. unquote(Binary) when is_binary(Binary) -> unquote(binary_to_list(Binary));
  98. unquote(String) -> qs_revdecode(lists:reverse(String)).
  99. unhexdigit(C) when C >= $0, C =< $9 -> C - $0;
  100. unhexdigit(C) when C >= $a, C =< $f -> C - $a + 10;
  101. unhexdigit(C) when C >= $A, C =< $F -> C - $A + 10.
  102. qs_revdecode(S) -> qs_revdecode(S, []).
  103. qs_revdecode([], Acc) -> Acc;
  104. qs_revdecode([$+ | Rest], Acc) -> qs_revdecode(Rest, [$\s | Acc]);
  105. qs_revdecode([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) -> qs_revdecode(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]);
  106. qs_revdecode([C | Rest], Acc) -> qs_revdecode(Rest, [C | Acc]).
  107. %% JavaScript encode/decode
  108. js_escape(undefined) -> [];
  109. js_escape(Value) when is_list(Value) -> binary_to_list(js_escape(iolist_to_binary(Value)));
  110. js_escape(Value) -> js_escape(Value, <<>>).
  111. js_escape(<<"\\", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\\\">>);
  112. js_escape(<<"\r", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\r">>);
  113. js_escape(<<"\n", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\n">>);
  114. js_escape(<<"\"", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\\"">>);
  115. js_escape(<<"'",Rest/binary>>,Acc) -> js_escape(Rest, <<Acc/binary, "\\'">>);
  116. js_escape(<<"<script", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "<scr\" + \"ipt">>);
  117. js_escape(<<"script>", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "scr\" + \"ipt>">>);
  118. js_escape(<<C, Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, C>>);
  119. js_escape(<<>>, Acc) -> Acc.
  120. % JOIN
  121. join([],_) -> [];
  122. join([Item],_Delim) -> [Item];
  123. join([Item|Items],Delim) -> [Item,Delim | join(Items,Delim)].
  124. % Fast HEX
  125. digit(0) -> $0;
  126. digit(1) -> $1;
  127. digit(2) -> $2;
  128. digit(3) -> $3;
  129. digit(4) -> $4;
  130. digit(5) -> $5;
  131. digit(6) -> $6;
  132. digit(7) -> $7;
  133. digit(8) -> $8;
  134. digit(9) -> $9;
  135. digit(10) -> $a;
  136. digit(11) -> $b;
  137. digit(12) -> $c;
  138. digit(13) -> $d;
  139. digit(14) -> $e;
  140. digit(15) -> $f.
  141. hex(Bin) -> << << (digit(A1)),(digit(A2)) >> || <<A1:4,A2:4>> <= Bin >>.
  142. unhex(Hex) -> << << (erlang:list_to_integer([H1,H2], 16)) >> || <<H1,H2>> <= Hex >>.
  143. f(S) -> f(S, []).
  144. f(S, Args) -> lists:flatten(io_lib:format(S, Args)).
  145. replace([], _, _) -> [];
  146. replace(String, S1, S2) when is_list(String), is_list(S1), is_list(S2) ->
  147. Length = length(S1),
  148. case string:substr(String, 1, Length) of
  149. S1 -> S2 ++ replace(string:substr(String, Length + 1), S1, S2);
  150. _ -> [hd(String)|replace(tl(String), S1, S2)]
  151. end.
  152. coalesce({_,Y}) -> Y;
  153. coalesce(false) -> undefined;
  154. coalesce([]) -> undefined;
  155. coalesce([H]) -> H;
  156. coalesce([undefined|T]) -> coalesce(T);
  157. coalesce([[]|T]) -> coalesce(T);
  158. coalesce([H|_]) -> H.
  159. indexof(Key, Fields) -> indexof(Key, Fields, 2).
  160. indexof(_Key, [], _N) -> undefined;
  161. indexof(Key, [Key|_T], N) -> N;
  162. indexof(Key, [_|T], N) -> indexof(Key, T, N + 1).
  163. config(App, Key, Default) -> application:get_env(App,Key,Default).
  164. os_env(Key) -> os_env(Key, "").
  165. os_env(Key, Default) -> case os:getenv(Key) of false -> Default; V -> V end.
  166. stack(Error, Reason) ->
  167. Stacktrace = [case A of
  168. { Module,Function,Arity,Location} ->
  169. { Module,Function,Arity,proplists:get_value(line, Location) };
  170. Else -> Else end
  171. || A <- erlang:get_stacktrace()],
  172. [Error, Reason, Stacktrace].
  173. error_page(Class,Error) ->
  174. io_lib:format("ERROR: ~w:~w~n~n",[Class,Error]) ++
  175. "STACK: " ++
  176. [ wf_render:render([io_lib:format("\t~w:~w/~w:~w",
  177. [ Module,Function,Arity,proplists:get_value(line, Location) ]),"\n"])
  178. || { Module,Function,Arity,Location} <- erlang:get_stacktrace() ].