%D \module
%D   [       file=math-inc,
%D        version=2018.06.23,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=XML inclusion,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Math Macros / XML inclusion}

\registerctxluafile{math-inc}{autosuffix}

%D I had some doubt about including this in \CONTEXT\ but it might serve some users
%D anyway. It's always the question to what extent one can be really roundtrip. I
%D might improve it when I need it.

% Talking about creating from a source ... June Lee's transcription of
% Close to You by Jacob Collier is an amazing example:
%
% https://www.youtube.com/watch?v=hdBVN-HMuqI

\unprotect

\definesymbol[mmlattachment][{\infofont\darkred  mml}]
\definesymbol[mmlcomment]   [{\infofont\darkblue mml}]

\permanent\protected\def\lxml_add_mml_blob#1#2%
  {\relax
   \clf_xmlformulatobuffer\the\c_strc_formulas_n{temp-xml-export}%
   #2%
     [\c!symbol=#1,%
      \c!space=\v!yes,%
      \c!buffer=temp-xml-export,%
      \c!name={formula-\the\c_strc_formulas_n.xml}]%
   \relax}

\permanent\protected\def\xmlattachmml
  {\iftrialtypesetting\orelse\ifexporting\iflocation
     \dostarttagged\t!ignore\empty
       \lxml_add_mml_blob{mmlattachment}\attachment
     \dostoptagged
   \fi\fi}

\permanent\protected\def\xmlcommentmml
  {\iftrialtypesetting\orelse\ifexporting\iflocation
     \dostarttagged\t!ignore\empty
       \lxml_add_mml_blob{mmlcomment}\comment
     \dostoptagged
   \fi\fi}

%D This kind of feature creep is not yet configurable, nor documented.

\permanent\protected\def\xmladdmmlsource
  {\iftrialtypesetting\orelse\ifexporting\iflocation
     \dostarttagged\t!ignore\empty
     \inleftmargin{%
       \lxml_add_mml_blob{mmlattachment}\attachment
       \quad
       \lxml_add_mml_blob{mmlcomment}\comment
     }%
     \dostoptagged
   \fi\fi}

\protect \endinput

