% verbatim-mo testphase Module
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Highly experimental module for tagging characters in verbatim
% LaTeX Project March 2025
% Licence https://www.latex-project.org/lppl.txt
\ProvidesFile{verbatim-af-latex-lab-testphase.ltx}
        [2025-03-31 v 0.1 Highly experimental module for tagging characters in verbatim]

\makeatletter
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=13 \catcode `\}=13
\catcode`\\=12
|gdef|@xxverbatim#1\end{verbatim}[#1|end[verbatim]]
|gdef|@sxverbatim#1\end{verbatim*}[#1|end[verbatim*]]
|endgroup
\def\@xverbatim{\activechars\everypar{\tagobeyedline}\@xxverbatim}
\makeatother

\ExplSyntaxOn
\cs_set:Npn\verbatim_tmp:nn#1#2{
  \group_begin:
  \pdfdict_put:nnn { l_pdffile/Filespec }{AFRelationship} { /Supplement }
  \pdfdict_put:nne { l_pdffile }{Subtype}
       { \pdf_name_from_unicode_e:n{application/mathml+xml} }
        \pdffile_embed_stream:nnn{
<math~xmlns="http://www.w3.org/1998/Math/MathML"><mo ~style="font-family:monospace">#2</mo></math>
}{#1.xml}{verb/#2}
  \group_end:
}
\cs_generate_variant:Nn \verbatim_tmp:nn {ne}
% delay to later to avoid to open the PDF to fast, which would no longer
% allow to change compression or the pdf version
\AddToHook{begindocument/before}
 {
  \verbatim_tmp:nn{openparen}{(}
  \verbatim_tmp:nn{closeparen}{)}
  \verbatim_tmp:ne{openbrace}{\c_left_brace_str}
  \verbatim_tmp:ne{closebrace}{\c_right_brace_str}
  \verbatim_tmp:nn{quote}{"}
  \verbatim_tmp:nn{semi}{;}
  \verbatim_tmp:nn{comma}{,}
  
  \group_begin:
  \pdfdict_put:nnn { l_pdffile/Filespec }{AFRelationship} { /Supplement }
  \pdfdict_put:nne { l_pdffile }{Subtype}
       { \pdf_name_from_unicode_e:n{application/mathml+xml} }
        \pdffile_embed_stream:nnn{
<math ~xmlns="http://www.w3.org/1998/Math/MathML"><mrow ~intent="_newline"></mrow></math>
}{newline.xml}{verb/newline}
  \group_end:
 }

\ExplSyntaxOff

\makeatletter
\def\defactive#1#2\relax{%
  \def#1{%
    \leavevmode\tagmcend\tagstructbegin{tag=Formula, AF={verb/\string#1}}\tagmcbegin{}%
    \detokenize{#1}%
    \tagmcend\tagstructend\tagmcbegin{}}%
}

\protected\gdef\tagobeyedline{%
    \tagmcend\tagstructbegin{tag=Formula, AF={verb/newline}}\tagmcbegin{}%
    \tagmcend\tagstructend\tagmcbegin{}}%

  
\makeatother

\begingroup
\catcode`\B=1
\catcode`\E=2
\catcode`\(\active
\catcode`\)\active
\catcode`\{\active
\catcode`\}\active
\catcode`\;\active
\catcode`\,\active
\catcode`\"\active

\gdef\activecharsB%
\catcode`\(\active
\catcode`\)\active
\catcode`\{\active
\catcode`\}\active
\catcode`\;\active
\catcode`\,\active
\catcode`\"\active
 \defactive(open paren\relax
 \defactive)close paren\relax
 \defactive{open brace\relax
 \defactive}close brace\relax
 \defactive;semicolon\relax
 \defactive,comma\relax
 \defactive"quote\relax
 E
\endgroup
