%\RequirePackage[enable-debug]{expl3}[2018/06/14]
\ProvidesExplPackage {tagpdfdocu-patches} {2021-07-03} {0.91}
 {patches/commands for the tagpdf documentation}
\RequirePackage{etoolbox}
%%%%
% header and footer (now needed as attributes are global again)
%%%%
%% patching \@oddfoot/\@oddhead is nonsense, that is redefined all the time.
%% Using the scrlayer interfaces would probably work
%% but we will need a core latex solution anyway
%% so let's test it here and patch \@outputpage
%% we also disable paratagging, not for the tagging but to avoid that the small
%% numbers gives overfull vbox messages ...
\patchcmd\@outputpage
 {\hb@xt@\textwidth{\@thehead}}
 {
  \hb@xt@\textwidth
   {
    \bool_set_false:N \l__tag_para_bool
    \tag_mc_artifact_group_begin:n{pagination}
    \@thehead
    \tag_mc_artifact_group_end:
   }
 }
 {}{\fail}
\patchcmd\@outputpage
 {\hb@xt@\textwidth{\@thefoot}}
 {
  \hb@xt@\textwidth
   {
    \bool_set_false:N \l__tag_para_bool
    \tag_mc_artifact_group_begin:n{pagination}
    \@thefoot
    \tag_mc_artifact_group_end:
   }
 }
 {}{\fail}

%%%%%
% tableofcontents
% no internal patches, but KOMA-only solution
% TODO it must be checked how best practice is now for the table of contents.
%%%%%


\AfterTOCHead{\tag_struct_begin:n{tag=TOC}}
\AfterStartingTOC{\tag_struct_end:} %end TOC
%Marking the toc entries
%around the whole entry so only structure:
\newcommand\tagscrtocentry[1]{\tag_struct_begin:n{tag=TOCI}#1\tag_struct_end:}

%leaf so structure and mc:
\newcommand\tagscrtocpagenumber[1]
 {
   \tag_struct_begin:n{tag=Reference}
   \tag_mc_begin:n{tag=Reference}
   #1
   \tag_mc_end:
 \tag_struct_end:
 }


\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{section}
\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{subsection}
\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{subsubsection}
\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{paragraph}


% \IfStr{#3} doesnt like a \label ...
\renewcommand{\addtocentrydefault}[3]{%
% \Ifstr{#3}{}{}
%   {%
   \Ifstr{#2}{}
    {%
     \addcontentsline{toc}{#1}
      {%
       \protect\nonumberline
       \tagstructbegin{tag=P}%
       \tagmcbegin{tag=P}%
        #3%
       \tagmcend
       \tagstructend
      }%
    }%
    {%
    \addcontentsline{toc}{#1}{%
     \tagstructbegin{tag=Lbl}%
     \tagmcbegin{tag=Lbl}%
     \protect\numberline{#2}%
     \tagmcend\tagstructend
     \tagstructbegin{tag=P}%
     \tagmcbegin{tag=P}%
      #3%
     \tagmcend
     \tagstructend
     }%
    }%
  %}
}%

% the dots must be marked too

\renewcommand*{\TOCLineLeaderFill}[1][.]{
  \leaders\hbox{$\m@th
    \mkern \@dotsep mu\hbox{\tag_mc_begin:n{artifact}#1\tag_mc_end:}\mkern \@dotsep
    mu$}\hfill
}

%%%%%%%%%
% Sectioning commands
% no internal patches, but KOMA-only solution
%%%%%%%%

\AddtoDoHook{heading/begingroup}{\tagpdfparaOff\use_none:n}

\prop_new:N    \g_tag_section_level_prop
\prop_gput:Nnn \g_tag_section_level_prop {section}{H1}
\prop_gput:Nnn \g_tag_section_level_prop {subsection}{H2}
\prop_gput:Nnn \g_tag_section_level_prop {subsubsection}{H3}
\prop_gput:Nnn \g_tag_section_level_prop {paragraph}{H4}

\renewcommand{\sectionlinesformat}[4]
 {
  \@hangfrom
   {\hskip #2
    \tagstructbegin{tag=\prop_item:Nn\g_tag_section_level_prop{#1}}
    %\tl_if_empty:nF{#3}
    \tl_if_in:nnF{\@empty}{#3}
    {
     \tag_mc_begin:n    {tag=\prop_item:Nn\g_tag_section_level_prop{#1}}
     #3
     \tag_mc_end:
    }
   }
   {\tag_mc_begin:n    {tag=\prop_item:Nn\g_tag_section_level_prop{#1}}
    #4
    \tag_mc_end:\tagstructend}%
 }

% minisec is simply P,  so we let paratagging handle this.
% But we add a strong structure
% TODO: Should changed to H6 as it is no longer required to have H6 only after H5
% but need to check how to disable paratagging ...
\cs_new:Nn \__tag_docu_tag_minisec:n
{
  \tag_struct_begin:n {tag=Strong}
  \tag_mc_begin:n{tag=Strong}
  #1
  \tag_mc_end:
  \tag_struct_end:
}
%
\addtokomafont{minisec}{\__tag_docu_tag_minisec:n}


%%%%
%% Lists
%% patches enumitem internals!
%%%%
\AddToHook{env/itemize/begin}{\par\tagstructbegin{tag=L}}%\par is needed to close the paragraph before.
\AddToHook{env/itemize/end}{\par\tagstructend\tagstructend\tagstructend}%LBody,LI,L
\AddToHook{env/enumerate/begin}{\par\tagstructbegin{tag=L}}
\AddToHook{env/enumerate/end}{\par\tagstructend\tagstructend\tagstructend}%LBody,LI,L
\AddToHook{env/description/begin}{\par\tagstructbegin{tag=L}}
\AddToHook{env/description/end}{\par\tagstructend\tagstructend\tagstructend}%LBody,LI,L

\newcommand\tag@enit@format@preset[1]{%
 \str_if_eq:eeTF { \prop_item:cn { g__tag_struct_\g__tag_struct_stack_current_tl _prop }{S} }{/L}
  {
   %\typeout{BEGIN~OF~LIST}
  }
  {%\typeout{NEXT ITEM}
   \tagstructend\tagstructend} % for the LBody/LI
  \tagstructbegin{tag=LI}
  \tagstructbegin{tag=Lbl}
  \tagmcbegin{tag=Lbl}
    #1
  \tagmcend
  \tagstructend
  \tagstructbegin{tag=LBody}
  }

\ExplSyntaxOff
\xpatchcmd\enit@preset{\@firstofone}{\tag@enit@format@preset}{}{\fail}
%close mc from paratagging and reopen ...
\xpatchcmd\@item{\box\@labels}{\tagmcend \box\@labels \tagmcbegin{tag=P}}{}{\fail}
\ExplSyntaxOn

% keys that overwrite \enit@format must be patched, eg like the follow
% the font key should be patched in a similar way
\enitkv@key{}{format}{% new enumitem don't use first argument
  \def\enit@format{%
   \tagstructbegin{tag=LI}%
   \tagstructbegin{tag=Lbl}%
   \tagmcbegin{tag=Lbl}%
   #1\tagmcend}}


% listings + verbatim
% the paragraph code could create span around the lines ...
% it is quite unclear if Code is a sensible structure as it no longer exists in pdf 2.0

\AddToHook{env/lstlisting/begin}{\tagpdfparaOff\tagstructbegin{tag=Code}\tagmcbegin{tag=Code}}
\AddToHook{env/lstlisting/end}{\tagmcend\tagstructend}

\AddToHook{env/verbatim/begin}{\tagpdfparaOff\tagstructbegin{tag=Code}\tagmcbegin{tag=Code}}
\AddToHook{env/verbatim/end}{\tagmcend\tagstructend}


% ======== marginnote ==========
% TODO marginnote has a bug (a \par is missing) so it messes up tagging.
% but currently unneeded as we marked them up as artifacts anyway as they don't contain
% meaningful contents

\NewDocumentCommand\sidenote{m}
 {
   \tag_mc_artifact_group_begin:n{notype}\tagpdfparaOff\marginnote{#1}\tag_mc_artifact_group_end:
 }

% ======== tikzpicture ==========
% TODO this needs some investigation: it messes up the stack if one add paraOff

\AddToHook{env/tikzpicture/begin}{}

%======== tcolorbox ========
% We switch of paratagging at the begin and reenable it locally in before upper.
% the before upper setting is dangerous as it can be overwritten by
% users. So a more stable hook is needed.
%\AddToHook{env/tcolorbox/begin}{\tagpdfparaOff}
%\AddToHook{env/docCommand/begin}{\tagpdfparaOff}
%\tcbset{before~upper=\tagpdfparaOn}
\AddToHook{env/tcolorbox/begin}{\tagpdfparaOff \tcbset{before~upper=\tagpdfparaOn}}
\AddToHook{env/docCommand/begin}{\tagpdfparaOff \tcbset{before~upper=\tagpdfparaOn}}

 %locally for now


% ======= footnote ========
% TODO

% ======= bibliography ========
% biblatex. Creates some empty mc-chunks.
% no internal patches, but redefining begentry/finentry is not safe.
% better hook is needed.

\newbibmacro*{begentry}{\tagstructbegin{tag=BibEntry}\tagmcbegin{tag=BibEntry}}
\newbibmacro*{finentry}{\finentry\tagmcend\tagstructend}

% ====== hyperref ========
% this should probably go into tagpdf, but it is related to
% problem of pdf strings and context ....

\@ifpackageloaded{hyperref}{%
\pdfstringdefDisableCommands{
 \let\tagstructbegin\@gobble
 \let\tagmcbegin\@gobble
 \let\tagmcend\relax
 \let\tagstructend\relax
}}{}


%====== pictures ========
% PAC3 complained that the BBox is missing, so we are cheating for now
% and add a fix size.
\tagpdfsetup
  {
     newattribute =
        {bbox}{/O /Layout /BBox [0~0~100~100]}
  }

%%%%%
%======== Role maps =========
%%%%%
\tagpdfsetup{ add-new-tag=Title/H1,add-new-tag=Strong/Span }



\endinput

% ==========================
% now unneeded commands

% we need a command which simply starts a paragraph \TagP^,
% a "continuation" variant which closes the previous paragraph (\TagP),
% and a final end.

\NewDocumentCommand\TagP{t{^}}
 {
  \tag_mc_if_in:T { \tag_mc_end: }
  \IfBooleanF{#1}
  {
    \tag_struct_end:
  }
  \tag_struct_begin:n{tag=P}
  \tag_mc_begin:n{tag=P}
 }
% close a mc and one structure

\NewDocumentCommand\TagPend{}
 {
  \tag_mc_if_in:T { \tag_mc_end: }
  \tag_struct_end:
 }


 \ExplSyntaxOn
\newcommand\showcurrentstruct{\prop_show:c { g__tag_struct_\g__tag_struct_stack_current_tl _prop }}


\ExplSyntaxOff
