% \iffalse meta-comment
%
%% Copyright (C) 2020-2021 by Marcel Krueger
%%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c of this license or (at your option) any later
%% version. The latest version of this license is in:
%%
%% http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%
%<*batch>
%<*gobble>
\ifx\jobname\relax\let\documentclass\undefined\fi
\ifx\documentclass\undefined
\csname fi\endcsname
%</gobble>
\input docstrip.tex
\keepsilent
\generate{
  \file{luamml.sty}{\from{luamml.dtx}{package,luatex}}
  \file{luamml-pdf.sty}{\from{luamml.dtx}{package,pdftex}}
}
\endbatchfile
%</batch>
%<*gobble>
\fi
\expandafter\ifx\csname @currname\endcsname\empty
\csname fi\endcsname
%</gobble>
%<*driver>
\documentclass{l3doc}
\usepackage{luamml}
\usepackage{csquotes,luacolor}
\RecordChanges
\begin{document}
\tracingmathml2
\DocInput{luamml.dtx}
\PrintIndex
\PrintChanges
\end{document}
%</driver>
%<*gobble>
\fi
%</gobble>
% \fi
%
% \title{The \pkg{luamml} package}
%
% \author{Marcel Krüger}
%
% \maketitle
%
% \begin{documentation}
% \end{documentation}
%
% \begin{implementation}
% \section{Package Implementation}
% \subsection{Initialization}
% \iffalse
%<*package>
% \fi
%    \begin{macrocode}
%<@@=luamml>
%<*luatex>
\ProvidesExplPackage {luamml} {2021-04-23} {0.0.1-alpha}
  {Automatically generate presentational MathML from LuaTeX math expressions}
%</luatex>
%<*pdftex>
\ProvidesExplPackage {luamml-pdf} {2021-05-31} {0.0.1-alpha}
  {MathML generation for L̶u̶a̶pdfLaTeX}
%</pdftex>
%    \end{macrocode}
%
% \subsection{Initialization}
% These variable have to appear before the Lua module is loaded and will be used to
% communicate information to the callback.
%
% Here \cs{tracingmathml} does not use a expl3 name since it is not intended for
% programming use but only as a debugging helper for the user.
% The other variables are internal, but we provide public interfaces for setting
% them later.
%    \begin{macrocode}
\int_new:N \l__luamml_flag_int
%<luatex>\tl_new:N \l__luamml_filename_tl
\tl_new:N \l__luamml_root_tl
\tl_set:Nn \l__luamml_root_tl { mrow }
\tl_new:N \l__luamml_label_tl
%<pdftex>\int_new:N \g__luamml_formula_id_int
%<luatex>\int_new:N \tracingmathml
%    \end{macrocode}
%
% Now we can load the Lua module which defines the callback.
% Of course until pdf\TeX starts implementing \cs{directlua} this is only
% done in Lua\TeX.
%    \begin{macrocode}
%<luatex>\lua_now:n { require'luamml-tex' }
%    \end{macrocode}
%
% \subsection{Flags}
% The most important interface is for setting the flag which controls how the
% formulas should be converted.
%
% \begin{macro}{\luamml_flag_process:}
%   Consider the current formula to be a complete, free-standing mathematical
%   expression which should be converted to MathML. Additionally, the formula
%   is also saved in the \texttt{start\_math} node as with
%   \cs{luamml_flag_save:}.
%    \begin{macrocode}
\cs_new_protected:Npn \luamml_flag_process: {
  \tl_set:Nn \l__luamml_label_tl {}
  \int_set:Nn \l__luamml_flag_int { 3 }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\__luamml_maybe_structelem:}
% A internal helper which can be added to a tag to preserve the external state
% of the structelem flag.
%    \begin{macrocode}
\cs_new:Npn \__luamml_maybe_structelem: {
  (
    8 * \int_mod:nn {
      \int_div_truncate:nn { \l__luamml_flag_int } {8}
    } {2}
  ) +
}
% \end{macro}
%
% \begin{macro}{\luamml_flag_save:n,
%               \luamml_flag_save:nN,
%               \luamml_flag_save:nn,
%               \luamml_flag_save:nNn}
%   Convert the current formula but only save it's representation in the math
%   node without emitting it as a complete formula. This is useful when the
%   expression forms part of a bigger formula and will be intergrated into it's
%   MathML tables later by special code.
%   It optinally accepts three parameters: A label, one math style command
%   (\cs{displaystyle}, \cs{textstyle}, etc.) which is the implicit math style
%   (so the style which the surrounding code expects this style to have) and a
%   name for the root element (defaults to \texttt{mrow}).
%   If the root element name is \texttt{mrow}, it will get suppressed in some
%   cases.
%    \begin{macrocode}
\cs_new_protected:Npn \luamml_flag_save:n #1 {
  \tl_set:Nn \l__luamml_label_tl {#1}
  \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 1 }
}
\cs_new_protected:Npn \luamml_flag_save:nN #1#2 {
  \tl_set:Nn \l__luamml_label_tl {#1}
  \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 17 + 32 * #2 }
}
\cs_new_protected:Npn \luamml_flag_save:nn #1 {
  \tl_set:Nn \l__luamml_label_tl {#1}
  \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 5 }
  \tl_set:Nn \l__luamml_root_tl
}
\cs_new_protected:Npn \luamml_flag_save:nNn #1#2 {
  \tl_set:Nn \l__luamml_label_tl {#1}
  \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 21 + 32 * #2 }
  \tl_set:Nn \l__luamml_root_tl
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\luamml_flag_ignore:}
%   Completely ignore the math mode material.
%    \begin{macrocode}
\cs_new_protected:Npn \luamml_flag_ignore: {
  \int_set:Nn \l__luamml_flag_int { 0 }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\luamml_flag_structelem:}
%   Like \cs{luamml_flag_process:}, but additionally add PDF structure
%   elements. This only works in Lua\TeX\ and requires that the \pkg{tagpdf} package
%   has been loaded \emph{before} \texttt{luamml}.
%    \begin{macrocode}
%<*luatex>
\cs_new_protected:Npn \luamml_flag_structelem: {
  \tl_set:Nn \l__luamml_label_tl {}
  \int_set:Nn \l__luamml_flag_int { 11 }
}
%</luatex>
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\luamml_set_filename:n}
%   Allows to set a filename to which the generated MathML gets written.
%   Previous content from the file will get overwritten. This includes results
%   written by a previous formula. Therefore this has to be called separately
%   for every formula or it must expand to different values to be useful.
%   The value is fully expanded when the file is written.
%   
%   Only complete formulas get written into files (so formulas where
%   \cs{luamml_flag_process:} or \cs{luamml_flag_structelem:} are in effect).
%
%   Only implemented in Lua\TeX, in pdf\TeX\ the arguments for \texttt{pdfmml}
%   determine the output location.
%    \begin{macrocode}
%<*luatex>
\cs_new_protected:Npn \luamml_set_filename:n {
  \tl_set:Nn \l__luamml_filename_tl
}
%</luatex>
%    \end{macrocode}
% \end{macro}
%
% By default, the flag is set to assume complete formulas.
%    \begin{macrocode}
\luamml_flag_process:
%    \end{macrocode}
%
% \subsection{Annotations}
% These are implemented very differently depending on the engine, but the interface
% should be the same.
% \subsubsection{Lua\TeX}
%    \begin{macrocode}
%<*luatex>
%    \end{macrocode}
% \begin{macro}{\luamml_annotate:nen, \luamml_annotate:en}
% A simple annotation scheme: The first argument is the number of top level
% noads to be annotated, the second parameter the annotation and the third
% parameter the actual list of math tokens. The first argument can be omitted to
% let Lua\TeX determine the number itself.
%
% Passing the first parameter explicitly is useful for any annotations which
% should be compatible with fututre pdf\TeX versions of this functionality.
%    \begin{macrocode}
\cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {
  \__luamml_annotate_begin:
    #3
    \__luamml_annotate_end:we \tex_numexpr:D #1 \scan_stop: {#2}
}

\cs_new_protected:Npn \luamml_annotate:en #1#2 {
  \__luamml_annotate_begin:
    #2
  \__luamml_annotate_end:e {#1}
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</luatex>
%    \end{macrocode}

% \subsubsection{pdf\TeX}
%    \begin{macrocode}
%<*pdftex>
%    \end{macrocode}
% \begin{macro}{\__luamml_pdf_showlists:}
% Here and in many other locations the \pdfTeX{} implementation is based on \cs{showlists},
% so we define a internal wrapper which sets all relevant parameters.
%    \begin{macrocode}
\cs_if_exist:NTF \showstream {
  \iow_new:N \l__luamml_pdf_stream
  \iow_open:Nn \l__luamml_pdf_stream { \jobname .tml }
  \cs_new_protected:Npn \__luamml_pdf_showlists: {
    \group_begin:
      \int_set:Nn \tex_showboxdepth:D { \c_max_int }
      \int_set:Nn \tex_showboxbreadth:D { \c_max_int }
      \showstream = \l__luamml_pdf_stream
      \tex_showlists:D
    \group_end:
  }
} {
  \cs_set_eq:NN \l__luamml_pdf_stream \c_log_iow
  \cs_set_eq:NN \__luamml_pdf_set_showstream: \scan_stop:
  \cs_new_protected:Npn \__luamml_pdf_showlists: {
    \group_begin:
      \int_set:Nn \l_tmpa_int { \tex_interactionmode:D }
      \int_set:Nn \tex_interactionmode:D { 0 }
      \int_set:Nn \tex_showboxdepth:D { \c_max_int }
      \int_set:Nn \tex_showboxbreadth:D { \c_max_int }
      \tex_showlists:D
      \int_set:Nn \tex_interactionmode:D { \l_tmpa_int }
    \group_end:
  }
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\luamml_annotate:nen, \luamml_annotate:en}
% Now we can define the annotation commands for pdf\TeX.
%    \begin{macrocode}
\cs_generate_variant:Nn \tl_to_str:n { e }
\int_new:N \g__luamml_annotation_id_int
\cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {
  \int_gincr:N \g__luamml_annotation_id_int
  \iow_shipout_x:Nx \l__luamml_pdf_stream {
    \tl_to_str:e {
      LUAMML_MARK:
      \int_use:N \g__luamml_annotation_id_int
      :
      count = \int_eval:n {#1},
      #2
    }
    \exp_not:N \iow_newline:
    LUAMML_MARK_END
  }
  #3
}
\cs_new_protected:Npn \luamml_annotate:en #1#2 {
  \int_gincr:N \g__luamml_annotation_id_int
  \iow_shipout_x:Nx \l__luamml_pdf_stream {
    \tl_to_str:e {
      LUAMML_MARK:
      \int_use:N \g__luamml_annotation_id_int
      :
      count = data.count[\int_use:N \g__luamml_annotation_id_int],
      #1
    }
    \exp_not:N \iow_newline:
    LUAMML_MARK_END
  }
  \iow_now:Nx \l__luamml_pdf_stream {
    LUAMML_COUNT:
    \int_use:N \g__luamml_annotation_id_int
  }
  \__luamml_pdf_showlists:
  #2
  \iow_now:Nx \l__luamml_pdf_stream {
    LUAMML_COUNT_END
  }
  \__luamml_pdf_showlists:
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</pdftex>
%    \end{macrocode}
%
% \subsection{Trigger for specific formula}
% This only applies for pdf\TeX\ since in Lua\TeX\ everything is controlled by the callback.
%
%    \begin{macrocode}
%<*pdftex>
%    \end{macrocode}
%
% \begin{macro}{\luamml_pdf_write:}
% We could accept parameters for the flag and tag here, but for compatibility
% with Lua\TeX they are passed in macros instead.
%    \begin{macrocode}
\cs_new_protected:Npn \luamml_pdf_write: {
  \int_gincr:N \g__luamml_formula_id_int
  \iow_now:Nx \l__luamml_pdf_stream {
    LUAMML_FORMULA_BEGIN:
    \int_use:N \g__luamml_formula_id_int
    :
    \int_use:N \l__luamml_flag_int
    :
    \l__luamml_root_tl
    :
    \l__luamml_label_tl
  }
  \__luamml_pdf_showlists:
  \iow_now:Nx \l__luamml_pdf_stream {
    LUAMML_FORMULA_END
  }
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</pdftex>
%    \end{macrocode}
%
% \subsection{Further helpers}
%
% \begin{macro}{\RegisterFamilyMapping}
% The Lua version of this is defined in the Lua module.
%    \begin{macrocode}
%<*pdftex>
\NewDocumentCommand \RegisterFamilyMapping {m m} {
  \iow_now:Nx \l__luamml_pdf_stream {
    LUAMML_INSTRUCTION:REGISTER_MAPPING: \int_use:N #1 : #2
  }
}
%</pdftex>
%    \end{macrocode}
%
%
% \subsection{Patching}
% For some packages, we ship with patches to make them more compatible and to
% demonstrate how other code can be patched to work with \texttt{luamml}.
% 
% These are either loaded directly if the packages are loaded or delayed using
% \LaTeX's hook system otherwise.
% \begin{macro}{\__luamml_patch_package:nn, \__luamml_patch_package:n}
% For this, we use two helpers: First a wrapper which runs arbitrary code either
% now (if the package is already loaded) or as soon as the package loads, second
% an application of the first one to load packages following \texttt{luamml}'s
% naming scheme for these patch packages.
%    \begin{macrocode}
\cs_new_protected:Npn \__luamml_patch_package:nn #1 #2 {
  \@ifpackageloaded {#1} {#2} {
    \hook_gput_code:nnn {package/after/#1} {luamml} {#2}
  }
}
\cs_new_protected:Npn \__luamml_patch_package:n #1 {
  \__luamml_patch_package:nn {#1} {
    \RequirePackage { luamml-patches-#1 }
  }
}
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
%<*luatex>
\RequirePackage { luamml-patches-kernel }
\__luamml_patch_package:n {amsmath}
\__luamml_patch_package:n {array}
%</luatex>
%    \end{macrocode}

% \iffalse
%</package>
% \fi
% \end{implementation}
% \Finale
