% \iffalse meta-comment
%
%% File: latex-lab-toc-hyperref-changes.dtx (C) Copyright 2022-2023 LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
%
% The development version of the bundle can be found below
%
%    https://github.com/latex3/latex2e/required/latex-lab
%
% for those people who are interested or want to report an issue.
%
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-toc-hyperref-changes.dtx}
\end{document}
%</driver>
%
% \fi
% \title{The \textsf{latex-lab-toc-hyperref-changes} package\\
% Changes and additions to hyperref related to toc entries}
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
% \date{Version 0.83 2023-06-07}
%
% \maketitle
%
% \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
% \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
% \newcommand{\docclass}{document class \marginpar{\raggedright document class
% customizations}}
%
% \providecommand\hook[1]{\texttt{#1}}
%
% \begin{abstract}
% \end{abstract}
%
% \section{Introduction}
%
% The followings contains various change needed for hyperref for the new toc tagging
% code. They should be only loaded if hyperref is used!
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \section{Changes for hyperref}
% \subsection{\cs{contentsline},\cs{addcontentsline}}
% The standard \cs{contentsline} and \cs{addcontentsline}
% should also be used with hyperref (together
% with more changes below) so we have to disable the redefinition in hyperref (this 
% requires hyperref version 7.00v!
%    \begin{macrocode}
\def\hyper@nopatch@toc{}
\AddToHook{package/hyperref/after}
 {
   \@ifpackagelater{hyperref}{2023-02-07}{}
   {\PackageWarning{latex-lab-testphase-toc-tagging}{hyperref too old}{}}
 }  
%    \end{macrocode}
%
% \subsection{Add links to toc entries}
%    \begin{macrocode}
\ExplSyntaxOn
\AddToHook{package/hyperref/after}
 {
   \AddToHookWithArguments{contentsline/text/before}[hyp]
     {
       \tl_if_blank:nF {#2}%text
        {
          \tl_if_blank:nF {#4}%destination
            {
              \int_case:nnF {\Hy@linktoc}
                {
                  {0}{} %none
                  {1}{\hyper@linkstart{link}{#4}} %section
                  {2}{} %page
                }
                {\hyper@linkstart{link}{#4}} %all
            }
          }
      }
    \AddToHookWithArguments{contentsline/text/after}[hyp]
      {
        \tl_if_blank:nF {#2}
         {
           \tl_if_blank:nF {#4}
             {
               \int_case:nnF {\Hy@linktoc}
                 {
                   {0}{} %none
                   {1}{\hyper@linkend} %section
                   {2}{} %page
                 }
                 {\hyper@linkend} %all
             } % none
           }
       }

    \AddToHookWithArguments{contentsline/page/before}[hyp]
      {
        \tl_if_blank:nF {#3}
         {
           \tl_if_blank:nF {#4} %
             {
               \int_case:nnF {\Hy@linktoc}
                 {
                   {0}{} %none
                   {1}{} %section
                   {2}{\hyper@linkstart{link}{#4}} %page
                 }
                 {\hyper@linkstart{link}{#4}} %all
             } % none
           }
       }
   \AddToHookWithArguments{contentsline/page/after}[hyp]
      {
        \tl_if_blank:nF {#3}
         {
           \tl_if_blank:nF {#4}
             {
               \int_case:nnF {\Hy@linktoc}
                 {
                   {0}{} %none
                   {1}{} %section
                   {2}{\hyper@linkend} %page
                 }
                 {\hyper@linkend} %all
             } % none
           }
       }
    \AddToHookWithArguments{cmd/addcontentsline/before}[hyp]
     {%
       \Hy@addcontentsline@addbookmark
         {#1}
         {#2}
         {#3}%
     }   
   }    
\ExplSyntaxOff
%    \end{macrocode}
% \subsection{Add bookmark code to \cs{addcontentsline}}
%
%    \begin{macrocode}
\newcommand\Hy@addcontentsline@addbookmark[3]%#1 toc type, #2 level, #3 content
  {%
   \ifx\@currentHref\@empty
    \Hy@Warning{%
      No destination for bookmark of \string\addcontentsline,%
      \MessageBreak destination is added%
     }%
    \phantomsection
  \fi
  \begingroup
    \expandafter\ifx\csname toclevel@#2\endcsname\relax
      \begingroup
        \def\Hy@tempa{#1}%
        \ifx\Hy@tempa\Hy@bookmarkstype
          \Hy@WarningNoLine{%
            bookmark level for unknown #2 defaults to 0%
          }%
        \else
          \Hy@Info{bookmark level for unknown #2 defaults to 0}%
        \fi
      \endgroup
      \expandafter\gdef\csname toclevel@#2\endcsname{0}%
    \fi
    \edef\Hy@toclevel{\csname toclevel@#2\endcsname}%
    \Hy@writebookmark{\csname the#2\endcsname}%
      {#3}%
      {\@currentHref}%
      {\Hy@toclevel}%
      {#1}%
    \ifHy@verbose
      \begingroup
        \def\Hy@tempa{#3}%
        \@onelevel@sanitize\Hy@tempa
        \let\temp@online\on@line
        \let\on@line\@empty
        \Hy@Info{%
          bookmark\temp@online:\MessageBreak
          thecounter {\csname the#2\endcsname}\MessageBreak
          text {\Hy@tempa}\MessageBreak
          reference {\@currentHref}\MessageBreak
          toclevel {\Hy@toclevel}\MessageBreak
          type {#1}%
        }%
      \endgroup
    \fi
   \endgroup
  }
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
