<commit_before>\section{Erlang JavaScript Compiler}

\subsection{Compilation and Macros}
Erlang JavaScript Parse Transform has two modes driven
by {\bf \-jsmacro} and {\bf \-js} Erlang module attributes.
First mode is for precompiling Erlang module functions
to JavaScript strings, second is for export Erlang function
to separate JavaScript file ready to run within Browser or node.js.

\paragraph{}
Sample of using {\bf \-jsmacro} and {\bf \-js}:

\vspace{1\baselineskip}
\begin{lstlisting}
    -module(sample).
    -compile({parse_transform, shen}).
    -jsmacro([tabshow/0,doc_ready/1,event/3]).
    -js(doc_ready/1).
\end{lstlisting}

\subsection{Erlang Macro Functions}
Macro functions are useful when N2O is used as server-side framework.
The function rewrites during Erlang compilation to JavaScript format
strings ready for embedding. Here is exmaple from N2O pages:

\begin{lstlisting}
tabshow() ->
    X = jq("a[data-toggle=tab]"),
    X:on("show", fun(E) -> T = jq(E:at("target")), tabshow(T:attr("href")) end).

doc_ready(E) ->
    D = jq(document),
    D:ready(fun() -> T = jq("a[href=\"#" ++ E ++ "\"]"), T:tab("show") end).

event(A,B,C) ->
    ws:send('Bert':encodebuf(
        [{source,'Bert':binary(A)}, {x,C},
         {pickle,'Bert':binary(B)}, {linked,C}])).

main() ->
    Script1 = tabshow(),
    Script2 = event(1, 2, 3),
    Script3 = doc_ready(wf:js_list("tab")),
    io:format("tabshow/0:~n~s~nevent/3:~n~s~ndoc_ready/1:~n~s~n",
        [Script1,Script2,Script3]).
\end{lstlisting}
\vspace{1\baselineskip}

\newpage
Perform compilation and run tests:

\vspace{1\baselineskip}
\begin{lstlisting}
        $ erlc sample.erl
        $ erl
        > sample:main().
\end{lstlisting}
\vspace{1\baselineskip}

You will get following output:

\vspace{1\baselineskip}
\begin{lstlisting}
        tabshow/0:
            var x = $('a[data-toggle=tab]');
            x.on('show',function(e) {
                var t = $(e['target']);
                return tabshow(t.attr('href'));
            });

        event/3:
            ws.send(Bert.encodebuf({source:Bert.binary(1),
                                    x:3,
                                    pickle:Bert.binary(2),
                                    linked:3}));

        doc_ready/1:
        var d = $(document);
        d.ready(function() {
            var t = $('a[href="#' + 'tab' + '"]');
            return t.tab('show');
        });
\end{lstlisting}
\vspace{1\baselineskip}

\subsection{JavaScript File Compilation}
Export Erlang function to JavaScript filw with \begin{verbatim}-js([sample/0,fun_args/2])\end{verbatim}.
You could include function both for {\bf macro} ans {\bf js} definitions.
<commit_msg>macro samples prettifying<commit_after>\section{Erlang JavaScript Compiler}

\subsection{Compilation and Macros}
Erlang JavaScript Parse Transform has two modes driven
by {\bf \-jsmacro} and {\bf \-js} Erlang module attributes.
First mode is for precompiling Erlang module functions
to JavaScript strings, second is for export Erlang function
to separate JavaScript file ready to run within Browser or node.js.

\paragraph{}
Sample of using {\bf \-jsmacro} and {\bf \-js}:

\vspace{1\baselineskip}
\begin{lstlisting}
    -module(sample).
    -compile({parse_transform, shen}).
    -jsmacro([tabshow/0,doc_ready/1,event/3]).
    -js(doc_ready/1).
\end{lstlisting}

\subsection{Erlang Macro Functions}
Macro functions are useful when N2O is used as server-side framework.
The function rewrites during Erlang compilation to JavaScript format
strings ready for embedding. Here is exmaple from N2O pages:

\begin{lstlisting}
    tabshow() ->
        X = jq("a[data-toggle=tab]"),
        X:on("show", 
            fun(E) -> T = jq(E:at("target")), tabshow(T:attr("href")) end).

    doc_ready(E) ->
        D = jq(document),
        D:ready(fun() -> T = jq("a[href=\"#" ++ E ++ "\"]"), T:tab("show") end).

    event(A,B,C) ->
        ws:send('Bert':encodebuf(
            [{source,'Bert':binary(A)}, {x,C},
             {pickle,'Bert':binary(B)}, {linked,C}])).

    main() ->
        Script1 = tabshow(),
        Script2 = event(1, 2, 3),
        Script3 = doc_ready(wf:js_list("tab")),
        io:format("tabshow/0:~n~s~nevent/3:~n~s~ndoc_ready/1:~n~s~n",
            [Script1,Script2,Script3]).
\end{lstlisting}
\vspace{1\baselineskip}

\newpage
Perform compilation and run tests:

\vspace{1\baselineskip}
\begin{lstlisting}
        $ erlc sample.erl
        $ erl
        > sample:main().
\end{lstlisting}
\vspace{1\baselineskip}

You will get following output:

\vspace{1\baselineskip}
\begin{lstlisting}
        tabshow/0:
            var x = $('a[data-toggle=tab]');
            x.on('show',function(e) {
                var t = $(e['target']);
                return tabshow(t.attr('href'));
            });

        event/3:
            ws.send(Bert.encodebuf({source:Bert.binary(1),
                                    x:3,
                                    pickle:Bert.binary(2),
                                    linked:3}));

        doc_ready/1:
        var d = $(document);
        d.ready(function() {
            var t = $('a[href="#' + 'tab' + '"]');
            return t.tab('show');
        });
\end{lstlisting}
\vspace{1\baselineskip}

You see, no source-map needed.

\subsection{JavaScript File Compilation}
Export Erlang function to JavaScript file with {\bf -js([sample/0,fun\_{args}/2])}.
You could include function both for {\bf macro} ans {\bf js} definitions.

{'fraction_non_alphanumeric': 0.15869247626004382, 'fraction_numerical': 0.012052593133674214, 'mean_word_length': 2.619960343688037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7098699', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1807, 'n_words': 430}