nitro_conv.erl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(B) when is_binary(B) -> html_encode(binary_to_list(B));
  48. html_encode([$\n|T]) -> "<br>" ++ html_encode(T);
  49. html_encode([H|T]) ->
  50. case H of
  51. $< -> "&lt;" ++ html_encode(T);
  52. $> -> "&gt;" ++ html_encode(T);
  53. $" -> "&quot;" ++ html_encode(T);
  54. $' -> "&#39;" ++ html_encode(T);
  55. $& -> "&amp;" ++ html_encode(T);
  56. BigNum when is_integer(BigNum) andalso BigNum > 255 ->
  57. %% Any integers above 255 are converted to their HTML encode equivilant,
  58. %% Example: 7534 gets turned into &#7534;
  59. [$&,$# | nitro:to_list(BigNum)] ++ ";" ++ html_encode(T);
  60. Tup when is_tuple(Tup) ->
  61. throw({html_encode,encountered_tuple,Tup});
  62. _ -> [H|html_encode(T)]
  63. end.
  64. html_encode_whites([]) -> [];
  65. html_encode_whites([H|T]) ->
  66. case H of
  67. $\s -> "&nbsp;" ++ html_encode_whites(T);
  68. $\t -> "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" ++ html_encode_whites(T);
  69. $< -> "&lt;" ++ html_encode_whites(T);
  70. $> -> "&gt;" ++ html_encode_whites(T);
  71. $" -> "&quot;" ++ html_encode_whites(T);
  72. $' -> "&#39;" ++ html_encode_whites(T);
  73. $& -> "&amp;" ++ html_encode_whites(T);
  74. $\n -> "<br>" ++ html_encode_whites(T);
  75. _ -> [H|html_encode_whites(T)]
  76. end.
  77. %% URL encode/decode
  78. url_encode(S) -> quote_plus(S).
  79. url_decode(S) -> unquote(S).
  80. -define(PERCENT, 37). % $\%
  81. -define(FULLSTOP, 46). % $\.
  82. -define(IS_HEX(C), ((C >= $0 andalso C =< $9) orelse
  83. (C >= $a andalso C =< $f) orelse
  84. (C >= $A andalso C =< $F))).
  85. -define(QS_SAFE(C), ((C >= $a andalso C =< $z) orelse
  86. (C >= $A andalso C =< $Z) orelse
  87. (C >= $0 andalso C =< $9) orelse
  88. (C =:= ?FULLSTOP orelse C =:= $- orelse C =:= $~ orelse
  89. C =:= $_))).
  90. quote_plus(Atom) when is_atom(Atom) -> quote_plus(atom_to_list(Atom));
  91. quote_plus(Int) when is_integer(Int) -> quote_plus(integer_to_list(Int));
  92. quote_plus(Bin) when is_binary(Bin) -> quote_plus(binary_to_list(Bin));
  93. quote_plus(String) -> quote_plus(String, []).
  94. quote_plus([], Acc) -> lists:reverse(Acc);
  95. quote_plus([C | Rest], Acc) when ?QS_SAFE(C) -> quote_plus(Rest, [C | Acc]);
  96. quote_plus([$\s | Rest], Acc) -> quote_plus(Rest, [$+ | Acc]);
  97. quote_plus([C | Rest], Acc) -> <<Hi:4, Lo:4>> = <<C>>, quote_plus(Rest, [digit(Lo), digit(Hi), ?PERCENT | Acc]).
  98. unquote(Binary) when is_binary(Binary) -> unquote(binary_to_list(Binary));
  99. unquote(String) -> qs_revdecode(lists:reverse(String)).
  100. unhexdigit(C) when C >= $0, C =< $9 -> C - $0;
  101. unhexdigit(C) when C >= $a, C =< $f -> C - $a + 10;
  102. unhexdigit(C) when C >= $A, C =< $F -> C - $A + 10.
  103. qs_revdecode(S) -> qs_revdecode(S, []).
  104. qs_revdecode([], Acc) -> Acc;
  105. qs_revdecode([$+ | Rest], Acc) -> qs_revdecode(Rest, [$\s | Acc]);
  106. 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]);
  107. qs_revdecode([C | Rest], Acc) -> qs_revdecode(Rest, [C | Acc]).
  108. %% JavaScript encode/decode
  109. js_escape(undefined) -> [];
  110. js_escape(Value) when is_list(Value) -> binary_to_list(js_escape(iolist_to_binary(Value)));
  111. js_escape(Value) -> js_escape(Value, <<>>).
  112. js_escape(<<"\\", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\\\">>);
  113. js_escape(<<"\r", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\r">>);
  114. js_escape(<<"\n", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\n">>);
  115. js_escape(<<"\"", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "\\\"">>);
  116. js_escape(<<"'",Rest/binary>>,Acc) -> js_escape(Rest, <<Acc/binary, "\\'">>);
  117. js_escape(<<"`",Rest/binary>>,Acc) -> js_escape(Rest, <<Acc/binary, "\\`">>);
  118. js_escape(<<"<script", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "<scr\" + \"ipt">>);
  119. js_escape(<<"script>", Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, "scr\" + \"ipt>">>);
  120. js_escape(<<C, Rest/binary>>, Acc) -> js_escape(Rest, <<Acc/binary, C>>);
  121. js_escape(<<>>, Acc) -> Acc.
  122. % JOIN
  123. join([],_) -> [];
  124. join([Item],_Delim) -> [Item];
  125. join([Item|Items],Delim) -> [Item,Delim | join(Items,Delim)].
  126. % Fast HEX
  127. digit(0) -> $0;
  128. digit(1) -> $1;
  129. digit(2) -> $2;
  130. digit(3) -> $3;
  131. digit(4) -> $4;
  132. digit(5) -> $5;
  133. digit(6) -> $6;
  134. digit(7) -> $7;
  135. digit(8) -> $8;
  136. digit(9) -> $9;
  137. digit(10) -> $a;
  138. digit(11) -> $b;
  139. digit(12) -> $c;
  140. digit(13) -> $d;
  141. digit(14) -> $e;
  142. digit(15) -> $f.
  143. hex(Bin) -> << << (digit(A1)),(digit(A2)) >> || <<A1:4,A2:4>> <= Bin >>.
  144. unhex(Hex) -> << << (erlang:list_to_integer([H1,H2], 16)) >> || <<H1,H2>> <= Hex >>.
  145. f(S) -> f(S, []).
  146. f(S, Args) -> lists:flatten(io_lib:format(S, Args)).
  147. replace([], _, _) -> [];
  148. replace(String, S1, S2) when is_list(String), is_list(S1), is_list(S2) ->
  149. Length = length(S1),
  150. case string:substr(String, 1, Length) of
  151. S1 -> S2 ++ replace(string:substr(String, Length + 1), S1, S2);
  152. _ -> [hd(String)|replace(tl(String), S1, S2)]
  153. end.
  154. coalesce({_,Y}) -> Y;
  155. coalesce(false) -> undefined;
  156. coalesce([]) -> undefined;
  157. coalesce([H]) -> H;
  158. coalesce([undefined|T]) -> coalesce(T);
  159. coalesce([[]|T]) -> coalesce(T);
  160. coalesce([H|_]) -> H.
  161. indexof(Key, Fields) -> indexof(Key, Fields, 2).
  162. indexof(_Key, [], _N) -> undefined;
  163. indexof(Key, [Key|_T], N) -> N;
  164. indexof(Key, [_|T], N) -> indexof(Key, T, N + 1).
  165. config(App, Key, Default) -> application:get_env(App,Key,Default).
  166. os_env(Key) -> os_env(Key, "").
  167. os_env(Key, Default) -> case os:getenv(Key) of false -> Default; V -> V end.