% !Mode:: "TeX:DE:UTF-8:Main"
%chapter not correct!!
\documentclass[a4paper,oneside]{memoir}
\settocdepth{subsubsection}
\setsecnumdepth{subsubsection}

\usepackage[english]{babel}


\usepackage{tagpdf}

\tagpdfifpdftexT
 {
  %set language / can also be done with hyperref
  \pdfcatalog{/Lang (en-US)}
  \usepackage[T1]{fontenc}
 }

\tagpdfifluatexT
 {
  %set language / can also be done with hyperref
  \pdfextension catalog{/Lang (en-US)}
  \usepackage{fontspec}
  \usepackage{luacode}
 }


\tagpdfsetup{tabsorder=structure,
             activate-all,
             uncompress
             }

\usepackage{lipsum}%\textheight3cm

\usepackage{etoolbox}

\ifdef\cfttocbeforelisthook{
  \appto\cfttocbeforelisthook{\tagstructbegin{tag=TOC}}%\noindent TOC\par}
}{
  \csdef\cfttocbeforelisthook{\tagstructbegin{tag=TOC}}%\noindent TOC\par}
}
\ifdef\cfttocafterlisthook{
  \appto\cfttocafterlisthook{\tagstructend}%\noindent /TOC\par}
}{
  \csdef\cfttocafterlisthook{\tagstructend}%\noindent /TOC\par}
}

\def\M{{\fontsize{3pt}{3pt}\sffamily\selectfont NN}}

%what about unnumbered entries in the toc??????
\appto\cftchapterpresnum{%
     \tagstructbegin{tag=TOCI}% is there a better place? what about unnumbered entries?
     \tagstructbegin{tag=Lbl}%
     \tagmcbegin{tag=Lbl}%
}
\appto\cftchapteraftersnum{%
  \tagmcend
  \tagstructend}

\appto\cftsectionpresnum{%
    \tagstructbegin{tag=TOCI}% is there a better place? what about unnumbered entries?
    \tagstructbegin{tag=Lbl}%
    \tagmcbegin{tag=Lbl}%
}
\appto\cftsectionaftersnum{%
 \tagmcend
 \tagstructend}

\appto\cftsubsectionpresnum{%
    \tagstructbegin{tag=TOCI}% is there a better place? what about unnumbered entries?
    \tagstructbegin{tag=Lbl}%
    \tagmcbegin{tag=Lbl}%
 }
\appto\cftsubsectionaftersnum{%
 \tagmcend
 \tagstructend}
\appto\cftsubsubsectionpresnum{%
    \tagstructbegin{tag=TOCI}% is there a better place? what about unnumbered entries?
    \tagstructbegin{tag=Lbl}%
    \tagmcbegin{tag=Lbl}%
}
\appto\cftsubsubsectionaftersnum{%
 \tagmcend
 \tagstructend}

%begin of entry
\appto\cftchapteraftersnumb{%
     \tagstructbegin{tag=P}%
     \tagmcbegin{tag=P}}
\appto\cftsectionaftersnumb{%
     \tagstructbegin{tag=P}%
     \tagmcbegin{tag=P}}
\appto\cftsubsectionaftersnumb{%
     \tagstructbegin{tag=P}%
     \tagmcbegin{tag=P}}
\appto\cftsubsubsectionaftersnumb{%
     \tagstructbegin{tag=P}%
     \tagmcbegin{tag=P}}

%end of entry
\pretocmd\cftchapterfillnum{%
 \tagmcend
 \tagstructend
}{}{}
\pretocmd\cftsectionfillnum{
 \tagmcend
 \tagstructend}{}{}
\pretocmd\cftsubsectionfillnum{
 \tagmcend
 \tagstructend}{}{}
\pretocmd\cftsubsubsectionfillnum{%
 \tagmcend
 \tagstructend}{}{}


\makeatletter
\patchcmd\cftchapterformatpnum{\cftchapterpagefont #1}{%
  %\llap{\M pnum}%
  \tagstructbegin{tag=Reference}%
   \tagmcbegin{tag=Reference}
  \cftchapterpagefont #1%
  %\rlap{\M pnum}%
  \tagmcend
  \tagstructend
  \tagstructend %for /TOCI. is there a better place?
}{}{}
\patchcmd\cftsectionformatpnum{\@nameuse{cftsectionpagefont}#1}{%
  %\llap{\M pnum}%
  \tagstructbegin{tag=Reference}%
   \tagmcbegin{tag=Reference}
   \@nameuse{cftsectionpagefont}#1%
  %\rlap{\M pnum}%
  \tagmcend
 \tagstructend
 \tagstructend %for /TOCI. is there a better place?
}{}{\typeout{patch failed}}
\patchcmd\cftsubsectionformatpnum{\@nameuse{cftsubsectionpagefont}#1}{%
  %\llap{\M}%
  \tagstructbegin{tag=Reference}%
   \tagmcbegin{tag=Reference}
   \@nameuse{cftsubsectionpagefont}#1%
  %\rlap{\M}%
   \tagmcend
  \tagstructend
  \tagstructend %for /TOCI. is there a better place?
}{}{\typeout{patch failed}}

\patchcmd\cftsubsubsectionformatpnum{\@nameuse{cftsubsubsectionpagefont}#1}{%
  %\llap{\M}%
  \tagstructbegin{tag=Reference}%
   \tagmcbegin{tag=Reference}
   \@nameuse{cftsubsubsectionpagefont}#1%
  %\rlap{\M}%
   \tagmcend
  \tagstructend
  \tagstructend %for /TOCI. is there a better place?
}{}{\typeout{patch failed}}


% section is typeset using \M@sect
\ExplSyntaxOn
\prop_new:N   \g_tag_section_level_prop
\prop_put:Nnn \g_tag_section_level_prop {chapter}{H1}
\prop_put:Nnn \g_tag_section_level_prop {section}{H2}
\prop_put:Nnn \g_tag_section_level_prop {Starredsection}{H2}
\prop_put:Nnn \g_tag_section_level_prop {subsection}{H3}
\prop_put:Nnn \g_tag_section_level_prop {subsubsection}{H4}
\prop_put:Nnn \g_tag_section_level_prop {paragraph}{H5}
\ExplSyntaxOff
\patchcmd\M@sect{#9\@@par}{%
 %this place could be used for something between number and text
  #9%
     \tagmcend
     \tagstructend
  \@@par%
}{}{}

% section* use \@ssect, but memoir redefines it a little before
% calling the kernel version

\ExplSyntaxOn
\patchcmd\@ssect{\@mem@old@ssect{#1}{#2}{#3}{#4}{#5}}{%
  \@mem@old@ssect{#1}{#2}{#3}{#4}{%
  \tagstructbegin{tag=H2}% how to get level???
  \tagmcbegin    {tag=H2}% how to get level???
   #5
  \tagmcend
  \tagstructend}%
}{}{}

% section numbers are typeset using \@seccntformat


\pretocmd\@seccntformat{%
 \tagstructbegin{tag=\prop_item:Nn\g_tag_section_level_prop{#1}}%
 \tagmcbegin    {tag=\prop_item:Nn\g_tag_section_level_prop{#1}}
}{}{}
\ExplSyntaxOff
% could be used to insert something after the number ...
%\patchcmd\@seccntformat{\endcsname}{%
%  \endcsname\rlap{\M}%
%}{}{}

\renewcommand{\cftdot}{\tagmcbegin{artifact}.\tagmcend}

\makeatother
 % I don't want to handle header/footer now
\pagestyle{empty}
\makeevenfoot{plain}{}{}{}
\makeoddfoot{plain}{}{}{}


%% missing yet: chapter titles
\begin{document}

\tagstructbegin{tag=Document}

\tableofcontents*

\chapter{Test chapter}

\section{Test section}

 \tagstructbegin{tag=P}
   \tagmcbegin{tag=P}
test
   \tagmcend
 \tagstructend


\subsection{Test subsection}

 \tagstructbegin{tag=P}
   \tagmcbegin{tag=P}
test
   \tagmcend
 \tagstructend

\subsubsection{Test subsubsection}

 \tagstructbegin{tag=P}
   \tagmcbegin{tag=P}
test
   \tagmcend
 \tagstructend

\section*{Starred section}

\tagstructend
\end{document}


