%\RequirePackage[enable-debug]{expl3}[2018/06/14]
\ProvidesExplPackage {tagpdfdocu-patches} {2023-03-07} {0.98e}
 {patches/commands for the tagpdf documentation}
\RequirePackage{etoolbox,xpatch}

%Logos

\ExplSyntaxOn
\DeclareRobustCommand\TeX{
  \leavevmode
  \tag_mc_end_push:
   \tag_struct_begin:n{tag=Span,alt=TeX,actualtext=TeX}
    \tagmcbegin{}
     T\kern-.1667em\lower.5ex\hbox{E}\kern-.05emX\@ %changed from -.125em
    \tagmcend
   \tag_struct_end:
  \tag_mc_begin_pop:n{}}
\DeclareRobustCommand{\LaTeX}{
        \leavevmode
        \tag_mc_end_push:
        \tag_struct_begin:n{tag=Span,alt=LaTeX,actualtext=LaTeX}
        \tagmcbegin{}
        L\kern-.25em % %changed from -0.36em
        \sbox\z@ T%
         \vbox to\ht\z@{\hbox{\check@mathfonts
                              \fontsize\sf@size\z@
                              \math@fontsfalse\selectfont
                              A}%
                        \vss}%
        \kern-.1em % %changed from 0.15em
        T\kern-.1667em\lower.5ex\hbox{E}\kern-.05emX\@ %changed from -0.125
        \tagmcend
        \tag_struct_end:
        \tag_mc_begin_pop:n{}    
  }
% textbf

\AddToHook{cmd/textbf/before}
  {\leavevmode\tag_mc_end_push:\tag_struct_begin:n{tag=Strong}\tagmcbegin{}}

\AddToHook{cmd/textbf/after}
  {\tag_mc_end:\tag_struct_end:\tag_mc_begin_pop:n{}}

% emph
\AddToHook{cmd/emph/before}
  {\leavevmode\tag_mc_end_push:\tag_struct_begin:n{tag=Em}\tagmcbegin{}}

\AddToHook{cmd/emph/after}
  {\tag_mc_end:\tag_struct_end:\tag_mc_begin_pop:n{}}
  
% minisec, correct tagging is still unclear.  
\newcommand\minisec[1]{%
    \if@noskipsec \leavevmode \fi
    \par
    \@afterindentfalse
    \if@nobreak
      \everypar{}%
    \else
      \addpenalty\@secpenalty\addvspace{1.5ex}%
    \fi
  {\tagpdfsetup{paratag=H10}\parindent \z@    
   \setlength{\parfillskip}{\z@ plus 1fil}%
    \raggedright\normalfont\bfseries\nobreak
    \nobreak\interlinepenalty \@M #1\par\nobreak%
  }\nobreak
  \@afterheading
}                    

% listings. Unclear how to make it work for the original lstlisting, so we make a 
% copy taglstlisting for now
\DeclareInstance{blockenv}{lstlisting}{display}
{
  env-name       = lstlisting,
  tag-name       = verbatim,
  tag-class      = ,
  tagging-recipe = standard,
  inner-level-counter  = ,
  level-increase = false,
  setup-code     = ,
  block-instance = displayblock ,
  inner-instance = ,
  final-code     =  \tl_set:Nn \l__tag_para_main_tag_tl {codeline}\tagtool{paratag=Code},
}

\lstnewenvironment{taglstlisting}[2][]{%
     \UseInstance{blockenv}{lstlisting} {}
     \lst@TestEOLChar{#2}%
     \lstset{#1}%
     \csname\@lst @SetFirstNumber\endcsname%
   }{%
     \@nobreakfalse
     \csname\@lst @SaveFirstNumber\endcsname%
     \endblockenv
   }
      
      % ======== 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:
 }

\ExplSyntaxOff
% ======== 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.
% we force also a \par and add a div structure, to avoid clashes with the block
% tagging code. This needs revisting!

\AddToHook{env/tcolorbox/before}{\par\tagstructbegin{tag=Div}}
\AddToHook{env/tcolorbox/begin}{\tagpdfparaOff \tcbset{before upper=\tagpdfparaOn}}
\AddToHook{env/tcolorbox/after}{\par\tagstructend}

% this is not good and only temporary, but it is still unclear how to handle internal lists.
\ExplSyntaxOn
\AddToHook{env/docCommand/before}{\par\tagtool{para=false}\tagstructbegin{tag=Code}\tagmcbegin{}\tag_stop:}
\AddToHook{env/docCommand/after}{\par\tag_start:\tagmcend\tagstructend\tagtool{para=true}}
\ExplSyntaxOff

%\AddToHook{env/docCommand/begin}{\tagpdfparaOff \tcbset{before upper=\tagpdfparaOn}\tagtool{paratag=Code}\tagtool{para-flattened=true}}

% ======= footnote ========
% done in testphase code

% ======= 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{}}
\newbibmacro*{finentry}{\finentry\tagmcend\tagstructend}

\def\blx@endbibliography{%
  \csuse{blx@endenv@\blx@theenv}%
  \blx@noitem
  \blx@locallabelwidth@finish
  \endgroup
  \blx@bibnote\blx@thepostnote
%  \endgroup
   \expandafter\endgroup\if@endpe\@doendpe\fi
}

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

\AddToHook{package/hyperref/after}
 {%
  \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]}
  }

\endinput
