%%
%% This is file `luamml.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% luamml.dtx  (with options: `package,luatex')
%% 
%% IMPORTANT NOTICE:
%% 
%% For the copyright see the source file.
%% 
%% Any modified versions of this file must be renamed
%% with new filenames distinct from luamml.sty.
%% 
%% For distribution of the original source see the terms
%% for copying and modification in the file luamml.dtx.
%% 
%% This generated file may be distributed as long as the
%% original source files, as listed above, are part of the
%% same distribution. (The sources need not necessarily be
%% in the same archive or directory.)
%% Copyright (C) 2020-2025 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.
\ProvidesExplPackage {luamml} {2025-03-06} {0.5.0}
  {Automatically generate presentational MathML from LuaTeX math expressions}
\int_new:N \l__luamml_flag_int
\int_new:N \l__luamml_pretty_int
\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
\int_new:N \tracingmathml

\int_set:Nn \l__luamml_pretty_int { 1 }
\lua_now:n { require'luamml-tex' }
\hook_new_with_args:nn { luamml / converted } { 1 }

\cs_new_protected:Npn \__luamml_output_hook:n {
  \hook_use:nnw { luamml / converted } { 1 }
}
\__luamml_register_output_hook:N \__luamml_output_hook:n

\cs_new_protected:Npn \luamml_process: {
  \tl_set:Nn \l__luamml_label_tl {}
  \int_set:Nn \l__luamml_flag_int { 3 }
}
\cs_set_eq:NN \luamml_flag_process: \luamml_process:
\cs_new:Npn \__luamml_maybe_structelem: {
  (
    8 * \int_mod:nn {
      \int_div_truncate:nn { \l__luamml_flag_int } {8}
    } {2}
  ) +
}
\cs_new:Npn \__luamml_style_to_num:N #1 {
  32 * #1
}
\cs_new_protected:Npn \luamml_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_save:nN #1#2 {
  \tl_set:Nn \l__luamml_label_tl {#1}
  \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 17 + \__luamml_style_to_num:N #2 }
}
\cs_new_protected:Npn \luamml_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_save:nNn #1#2 {
  \tl_set:Nn \l__luamml_label_tl {#1}
  \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 21 + \__luamml_style_to_num:N #2 }
  \tl_set:Nn \l__luamml_root_tl
}
\cs_set_eq:NN \luamml_flag_save:n \luamml_save:n
\cs_set_eq:NN \luamml_flag_save:nN \luamml_save:nN
\cs_set_eq:NN \luamml_flag_save:nn \luamml_save:nn
\cs_set_eq:NN \luamml_flag_save:nNn \luamml_save:nNn
\cs_new_protected:Npn \luamml_ignore: {
  \int_set:Nn \l__luamml_flag_int { 0 }
}
\cs_set_eq:NN \luamml_flag_ignore: \luamml_ignore:
\cs_new_protected:Npn \luamml_structelem: {
  \tl_set:Nn \l__luamml_label_tl {}
  \int_set:Nn \l__luamml_flag_int { 11 }
}
\cs_set_eq:NN \luamml_flag_structelem: \luamml_structelem:
\cs_new_protected:Npn \luamml_set_filename:n {
  \tl_set:Nn \l__luamml_filename_tl
}
\luamml_process:
\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}
}

\cs_new_eq:NN \luamml_pdf_write: \scan_stop:
\str_if_exist:cF { l__socket_tagsupport/math/luamml/save/nn_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/save/nn}{1}
   \AssignSocketPlug{tagsupport/math/luamml/save/nn}{noop}
   \NewSocket{tagsupport/math/luamml/save/nNn}{1}
   \AssignSocketPlug{tagsupport/math/luamml/save/nNn}{noop}
 }
\NewSocketPlug{tagsupport/math/luamml/save/nNn}{luamml}
 {
   \luamml_save:nNn #1
 }
\AssignSocketPlug{tagsupport/math/luamml/save/nNn}{luamml}
\NewSocketPlug{tagsupport/math/luamml/save/nn}{luamml}
 {
   \luamml_save:nn #1
 }
\AssignSocketPlug{tagsupport/math/luamml/save/nn}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/annotate/false_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/annotate/false}{2}
   \NewSocketPlug{tagsupport/math/luamml/annotate/false}{default}{#2}
   \AssignSocketPlug{tagsupport/math/luamml/annotate/false}{default}
 }
\NewSocketPlug{tagsupport/math/luamml/annotate/false}{luamml}
 {
   \luamml_annotate:en { core = false }
    {
      #2
    }
 }
\AssignSocketPlug{tagsupport/math/luamml/annotate/false}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/array/finalize_plug_str }
  {
     \NewSocket{tagsupport/math/luamml/array/save}{0}
     \NewSocket{tagsupport/math/luamml/array/finalize}{0}
     \NewSocket{tagsupport/math/luamml/array/initcol}{0}
     \NewSocket{tagsupport/math/luamml/array/finalizecol}{1}
     \AssignSocketPlug{tagsupport/math/luamml/array/finalizecol}{noop}
  }
\AddToHook{package/array/after}{\lua_now:n { require'luamml-array' }}
\NewSocketPlug{tagsupport/math/luamml/array/save}{luamml}
 {
   \__luamml_array_save_array:
 }
\NewSocketPlug{tagsupport/math/luamml/array/finalize}{luamml}
 {
   \mode_if_math:T { \__luamml_array_finalize_array: }
 }
\NewSocketPlug{tagsupport/math/luamml/array/initcol}{luamml}
 {
   \__luamml_array_init_col:
 }
\NewSocketPlug{tagsupport/math/luamml/array/finalizecol}{luamml}
 {
   \__luamml_array_finalize_col:w #1~
 }
\AssignSocketPlug{tagsupport/math/luamml/array/save}{luamml}
\AssignSocketPlug{tagsupport/math/luamml/array/finalize}{luamml}
\AssignSocketPlug{tagsupport/math/luamml/array/initcol}{luamml}
\AssignSocketPlug{tagsupport/math/luamml/array/finalizecol}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/mtable/finalizecol_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/mtable/finalizecol}{1}
 }
\NewSocketPlug{tagsupport/math/luamml/mtable/finalizecol}{luamml}
 {
   \use:c{__luamml_amsmath_add_#1_to_row:}
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/finalizecol}{luamml}

\str_if_exist:cF { l__socket_tagsupport/math/luamml/mtable/innertable/save_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/mtable/innertable/save}{0}
   \NewSocket{tagsupport/math/luamml/mtable/smallmatrix/save}{0}
   \NewSocket{tagsupport/math/luamml/mtable/innertable/finalize}{0}
 }
\NewSocketPlug{tagsupport/math/luamml/mtable/innertable/save}{luamml}
 {
   \__luamml_amsmath_save_inner_table:n \@currenvir
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/innertable/save}{luamml}
\NewSocketPlug{tagsupport/math/luamml/mtable/smallmatrix/save}{luamml}
 {
   \__luamml_amsmath_save_smallmatrix:
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/smallmatrix/save}{luamml}
\NewSocketPlug{tagsupport/math/luamml/mtable/innertable/finalize}{luamml}
 {
   \__luamml_amsmath_finalize_inner_table:
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/innertable/finalize}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/mtable/finalize_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/mtable/finalize}{1}
   \AssignSocketPlug{tagsupport/math/luamml/mtable/finalize}{noop}
 }
\NewSocketPlug{tagsupport/math/luamml/mtable/finalize}{luamml}
 {
   \__luamml_amsmath_finalize_table:n {#1}
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/finalize}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/mtable/aligncol_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/mtable/aligncol}{1}
   \AssignSocketPlug{tagsupport/math/luamml/mtable/aligncol}{noop}
 }
\NewSocketPlug{tagsupport/math/luamml/mtable/aligncol}{luamml}
 {
   \__luamml_amsmath_set_row_columnalign:n {#1}
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/aligncol}{luamml}

\str_if_exist:cF { l__socket_tagsupport/math/luamml/mtable/tag/save_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/mtable/tag/save}{0}
   \NewSocket{tagsupport/math/luamml/mtable/tag/set}{0}
 }
\NewSocketPlug{tagsupport/math/luamml/mtable/tag/save}{luamml}
 {
   \__luamml_amsmath_save_tag:
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/tag/save}{luamml}
\NewSocketPlug{tagsupport/math/luamml/mtable/tag/set}{luamml}
 {
   \__luamml_amsmath_set_tag:
 }
\AssignSocketPlug{tagsupport/math/luamml/mtable/tag/set}{luamml}

\str_if_exist:cF { l__socket_tagsupport/math/display/tag/begin_plug_str }
 {
   \NewSocket{tagsupport/math/display/tag/begin}{0}
   \NewSocket{tagsupport/math/display/tag/end}{0}
 }
\clist_map_inline:nn
  {
    align,
    align*,
    alignat,
    alignat*,
    xalignat,
    xalignat*,
    %xxalignat,
    flalign,
    flalign*,
    gather,
    gather*,
    %multline, % NO
    %multline*, % NO
    %equation, % NO
    %equation*, % NO
    %split, % NO
  }
  {\tl_const:cn { c__luamml_label_#1_tl}{}}
\NewSocketPlug{tagsupport/math/display/tag/begin}{luamml}
 {
   \tag_mc_end:
   \bool_lazy_and:nnTF
    { \tl_if_exist_p:c { c__luamml_label_ \@currenvir _tl } }
    { \int_if_odd_p:n { \int_div_truncate:nn { \l__luamml_flag_int } { 8 } } }
    {
      %\typeout{Stash~and~move~\@currenvir\c_space_tl Lbl}
      \tag_struct_begin:n {tag=Lbl,stash}
      \directlua{table.insert(ltx.__tag.struct.luamml.labels,\tag_get:n{struct_num})}
    }
    {
      \tag_struct_begin:n {tag=Lbl}
    }
   \tag_mc_begin:n {}
 }
\AssignSocketPlug{tagsupport/math/display/tag/begin}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/hbox_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/hbox}{2}
   \NewSocketPlug{tagsupport/math/luamml/hbox}{default}{#2}
   \AssignSocketPlug{tagsupport/math/luamml/hbox}{default}
 }
\NewSocketPlug{tagsupport/math/luamml/hbox}{luamml}
 {
   \bool_lazy_and:nnTF
    { \mode_if_math_p: }
    { \int_if_odd_p:n { \int_div_truncate:nn { \l__luamml_flag_int } { 8 } } }
    {
      \tag_struct_begin:n
       {
         tag=mtext,
         stash,
       }
      \tag_mc_begin:n {}
      \luamml_annotate:en
       {
         nucleus = true,
         structnum=\tag_get:n{struct_num}
       }
       { #2 }
      \tag_mc_end:
      \tag_struct_end:
    }
    { #2 }
  }
\AssignSocketPlug{tagsupport/math/luamml/hbox}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/artifact_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/artifact}{0}
 }
\NewSocketPlug{tagsupport/math/luamml/artifact}{luamml}
 {
   \int_if_odd:nT { \int_div_truncate:nn { \l__luamml_flag_int } { 8 } }
    {
      \tag_mc_begin:n{artifact}
    }
 }
\AssignSocketPlug{tagsupport/math/luamml/artifact}{luamml}
\str_if_exist:cF { l__socket_tagsupport/math/luamml/finph@nt_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/finph@nt}{2}
   \NewSocketPlug{tagsupport/math/luamml/finph@nt}{default}{#2}
   \AssignSocketPlug{tagsupport/math/luamml/finph@nt}{default}
 }
\NewSocketPlug{tagsupport/math/luamml/finph@nt}{luamml}
 {
   \luamml_annotate:nen {1}
    {
     nucleus = true,
     core =
       {
        [0] = 'mpadded',
        \ifh@\else
         width = 0,
        \fi
        \ifv@\else
         height = 0, depth = 0,
        \fi
        consume_label'mathphant',
      }
    }
    { #2 }
 }
\AssignSocketPlug{tagsupport/math/luamml/finph@nt}{luamml}

\str_if_exist:cF { l__socket_tagsupport/math/luamml/finsm@sh_plug_str }
 {
   \NewSocket{tagsupport/math/luamml/finsm@sh}{2}
   \NewSocketPlug{tagsupport/math/luamml/finsm@sh}{default}{#2}
   \AssignSocketPlug{tagsupport/math/luamml/finsm@sh}{default}
 }
\NewSocketPlug{tagsupport/math/luamml/finsm@sh}{luamml}
 {
   \luamml_annotate:nen {2}
    {
     nucleus = true,
     core =
       consume_label('mathsmash',
           function(padded)
             padded.height, padded.depth = 0, 0~
           end),
    }
    { #2 }
 }
\AssignSocketPlug{tagsupport/math/luamml/finsm@sh}{luamml}
\cs_new_protected:Npn \__luamml_patch_package:nn #1 #2 {
  \@ifpackageloaded {#1} {#2} {
    \hook_gput_code:nnn {package/#1/after} {luamml} {#2}
  }
}
\cs_new_protected:Npn \__luamml_patch_package:n #1 {
  \__luamml_patch_package:nn {#1} {
    \RequirePackage { luamml-patches-#1 }
  }
}
\RequirePackage { luamml-patches-kernel }
\__luamml_patch_package:n {amstext}
\__luamml_patch_package:n {amsmath}
\__luamml_patch_package:n {mathtools}

\endinput
%%
%% End of file `luamml.sty'.
