%%
%% This is file `latex-lab-testphase-sec.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latex-lab-sec.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright 2021-2023 LaTeX Project
%% 
%% This file was generated from file(s) of the  `LaTeX-lab Bundle'.
%% ------------------------------------------------------------------------------------
%% 
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008 or later.
%% 
%% This file may only be distributed together with a copy of the LaTeX
%% `LaTeX-lab Bundle'. You may however distribute the `LaTeX-lab Bundle'
%% without such generated files.
%% 
%% The newest sources can be found below
%% 
%%    https://github.com/latex3/latex2e/required/latex-lab
%% 
%% where one can also log issues in case there are any.
%% 
%% 
%% File: latex-lab-sec.dtx (C) Copyright 2022-2024 LaTeX Project
\def\ltlabsecdate{2024-10-04}
\def\ltlabsecversion{0.84f}
\ProvidesExplPackage {latex-lab-testphase-sec} {\ltlabsecdate} {\ltlabsecversion}
  {Code related to the tagging of sectioning commands}
\RequirePackage{latex-lab-kernel-changes}
\seq_new:N   \g__tag_sec_stack_seq
\seq_gpush:Nn\g__tag_sec_stack_seq {{Document}{-100}{2}}
\cs_new:Npn \__tag_get_data_current_Sect:
 {
   \exp_last_unbraced:Ne\use_iii:nnn{\seq_item:Nn\g__tag_sec_stack_seq{1}}
 }
\bool_new:N     \l__tag_sec_Sect_bool
\bool_set_true:N\l__tag_sec_Sect_bool
\cs_new_protected:Npn\__tag_sec_begin:nn #1 #2 %#1 level #2 keyval
  {
    \tag_struct_begin:n
      {
         tag= {\int_compare:nNnTF {#1}={-1}{Part}{Sect}}
        ,#2
      }
    \seq_gpush:Ne \g__tag_sec_stack_seq
      {{\g__tag_struct_tag_tl}{\int_eval:n{#1}}{\g__tag_struct_stack_current_tl}}
  }
\msg_new:nnn { tag } {wrong-sect-nesting}
  {
    The~structure~#1~can~not~be~closed.\\
    It~is~not~equal~to~the~current~structure~#2~on~the~main~stack
  }

\cs_new_protected:Npn\__tag_sec_end:n #1 % #1 level
  {
    \seq_get:NN \g__tag_sec_stack_seq \l__tag_tmpa_tl
    \int_compare:nNnT {#1}<{\exp_last_unbraced:NV\use_ii:nnn\l__tag_tmpa_tl+1}
      {
        \seq_get:NN\g__tag_struct_tag_stack_seq \l__tag_tmpb_tl
        \exp_args:Nee
          \tl_if_eq:nnTF
            {\exp_last_unbraced:NV\use_i:nnn\l__tag_tmpa_tl}
            {\exp_last_unbraced:NV\use_i:nn\l__tag_tmpb_tl}
            {
              \seq_gpop:NN \g__tag_sec_stack_seq \l__tag_tmpa_tl
              \tag_struct_end:
              \__tag_sec_end:n {#1}
            }
            {
              \msg_warning:nnee {tag}{wrong-sect-nesting}
               { \exp_last_unbraced:NV\use_i:nnn \l__tag_tmpa_tl }
               { \exp_last_unbraced:NV\use_i:nn \l__tag_tmpb_tl }
            }
      }
  }

\cs_new_protected:Npn \__tag_tool_para_split:
  {
    \tag_mc_end:
    \tag_struct_end:
    \tag_struct_begin:n{tag=\l__tag_para_tag_default_tl}
    \tag_mc_begin:n{}
    \__tag_setup_restore_para_default:
  }

\cs_new_protected:Npn \__tag_setup_restore_para_default:
  {
    \tl_set:Nn \l__tag_para_main_tag_tl {text-unit}
    \tl_set_eq:NN\l__tag_para_tag_tl\l__tag_para_tag_default_tl
  }
\cs_new_protected:Npn \__tag_sec_end_display:
  {
    \tag_struct_end: %P = Hn
    \__tag_setup_restore_para_default:
  }
\hook_gput_code:nnn{tagpdf/finish/before}{tagpdf/sec}{\__tag_sec_end:n{-10}}
\hook_gset_rule:nnnn {tagpdf/finish/before}{tagpdf/sec}{before}{tagpdf}
\AddToHook{cmd/frontmatter/before}{\__tag_sec_end:n{-10}}
\AddToHook{cmd/mainmatter/before} {\__tag_sec_end:n{-10}}
\AddToHook{cmd/backmatter/before} {\__tag_sec_end:n{-10}}
\AddToHook{cmd/appendix/before}   {\__tag_sec_end:n{-10}}
\cs_if_free:NT \tag_tool:n
 {
   \cs_new_protected:Npn \tag_tool:n #1
    {
      \tag_if_active:T { \keys_set:nn {tag / tool}{#1} }
    }
   \cs_set_eq:NN\tagtool\tag_tool:n
 }
\keys_define:nn { tag / tool}
  {
    ,sec-start-part .code:n =
      {
        \bool_if:NT\l__tag_sec_Sect_bool
          {
            \__tag_sec_end:n   {-1}
            \__tag_sec_begin:nn{-1}{tag=Part}
          }
         \tag_struct_begin:n{tag=part,title=#1}
         \tl_set:Nn\l__tag_para_main_tag_tl {NonStruct}
         \tl_set:Nn\l__tag_para_tag_tl {Span}
      }
    ,sec-stop-part .code:n = {\__tag_sec_end_display:}
    ,sec-start-chapter .code:n =
     {
       \bool_if:NT\l__tag_sec_Sect_bool
         {
           \__tag_sec_end:n   {0}
           \__tag_sec_begin:nn{0}{tag=Sect}
         }
        \tag_struct_begin:n{tag=chapter,title=#1}
        \tl_set:Nn\l__tag_para_main_tag_tl {NonStruct}
        \tl_set:Nn\l__tag_para_tag_tl {Span}
     }
    ,sec-stop-chapter .meta:n = { sec-stop-part}
    ,sec-start .code:n = % #1 is a name like "section"
      {
        \bool_if:NT\l__tag_sec_Sect_bool
          {
            \__tag_sec_end:n    {\cs_if_exist_use:c{toclevel@#1}+0}
            \__tag_sec_begin:nn {\cs_if_exist_use:c{toclevel@#1}+0}{tag=Sect}
          }
        \tl_set:Nn\l__tag_para_tag_tl{#1}
      }
    ,sec-start .value_required:n = true
    ,sec-split-para .code:n = {\__tag_tool_para_split:}
    ,restore-para .code:n = {\__tag_setup_restore_para_default:}
    ,sec-stop .code:n =
      {
        \par\__tag_sec_end:n   {\cs_if_exist_use:c{toclevel@#1}+0}
      }
    ,sec-stop .value_required:n = true
    ,sec-add-grouping .bool_set:N = \l__tag_sec_Sect_bool
  }
\AddToHook{class/after}
 {
  \@ifundefined{chapter}
    {
     \@ifundefined{part}{}
      {
        \RenewDocumentCommand\part{ s O{#3} m }
         {
           \if@noskipsec \leavevmode \fi
           \par
           \addvspace{4ex}%
           \@afterindentfalse
        % tagging start commands
          \tag_tool:n {sec-start-part=#2}
        % end tagging start commands
           \bool_lazy_any:nT
            {
              { #1 }
              {
                \int_compare_p:nNn {\c@secnumdepth}<{-1}
              }
            }
            {
              \MakeLinkTarget[part]{}
            }
          \IfBooleanTF
            {#1}
            { \@spart {#3} }
            { \@part [{#2}]{#3} }
         \tag_tool:n {sec-stop-part}
         }
       }
    }
    {
     \RenewDocumentCommand\chapter{ s O{#3} m }
      {
        \if@openright\cleardoublepage\else\clearpage\fi
        \thispagestyle{plain}%
        \global\@topnum\z@
        \@afterindentfalse
        \tag_tool:n { sec-start-chapter= #2 }
        \bool_lazy_any:nT
          {
            { #1 }
            {
              \int_compare_p:nNn {\c@secnumdepth}<{0}
            }
            {
              %in book target also needed in frontmatter
              \bool_lazy_and_p:nn
                { \cs_if_exist_p:c { @mainmattertrue } }
                { ! \legacy_if_p:n { @mainmatter } }
            }
          }
          {
            \MakeLinkTarget[chapter]{}
          }
        \IfBooleanTF
          {#1}
          { \@schapter {#3} }
          { \@chapter [{#2}]{#3} }
        \tag_tool:n {sec-stop-chapter}
      }
     \RenewDocumentCommand\part{ s O{#3} m }
      {
        \if@openright
          \cleardoublepage
        \else
          \clearpage
        \fi
        \thispagestyle{plain}%
        \if@twocolumn
          \onecolumn
          \@tempswatrue
        \else
          \@tempswafalse
        \fi
        \null\vfil
       \tag_tool:n {sec-start-part=#2}
        \bool_lazy_any:nT
          {
            { #1 }
            {
              \int_compare_p:nNn {\c@secnumdepth}<{-1}
            }
            {
              %in book target also needed in frontmatter
              \bool_lazy_and_p:nn
                { \cs_if_exist_p:c { @mainmattertrue } }
                { ! \legacy_if_p:n { @mainmatter } }
            }
          }
          {
            \MakeLinkTarget[part]{}
          }
        \IfBooleanTF
          {#1}
          { \@spart {#3} }
          { \@part [{#2}]{#3} }
        \tag_tool:n{sec-stop-part}
      }
    }
 }
\def\hyper@nopatch@sectioning{}
\cs_new_protected:Npn \@hyp@section@target@nnn #1 #2 #3 %#1 optarg #2 name/counter, #3 indent
  {
    \makebox[0pt][l]
     {
       \skip_set:Nn \@tempskipa {#3}
       \dim_compare:nNnF {\@tempskipa}<{0pt}{\kern-\@tempskipa}
       \MakeLinkTarget#1{#2}
     }
  }
\def\@startsection#1#2#3#4#5#6{%
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \tag_tool:n { sec-start=#1}%new
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}
\cs_new_protected:Npn \@kernel@tag@hangfrom #1
  {
     \tagstructbegin{tag=\l__tag_para_tag_tl}
     \cs_if_exist_use:N \__tag_gincr_para_begin_int:
     \tagstructbegin{tag=Lbl}
     \setbox\@tempboxa
       \hbox
         {
           \bool_lazy_and:nnT
            {\tag_if_active_p:}
            {\g__tag_mode_lua_bool}
            {\tagmcbegin{tag=Lbl}}
           {#1}
         }
   \tag_suspend:n{hangfrom}
   \hangindent \wd\@tempboxa\noindent
   \tag_resume:n{hangfrom}
   \tagmcbegin{}\box\@tempboxa\tagmcend\tagstructend\tagmcbegin{}}
\cs_new_protected:Npn \@kernel@tag@svsec
  {
    \tag_mc_end_push:
    \tag_struct_begin:n{tag=Lbl}
    \tag_mc_begin:n{}
    \@svsec
    \tag_mc_end:
    \tag_struct_end:
    \tag_mc_begin_pop:n{}
  }
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \def\@svsec{\@hyp@section@target@nnn{[section]}{}{#3}}
  \else
    \LinkTargetOff
    \refstepcounter{#1}%
    \LinkTargetOn
    \protected@edef\@svsec{\@hyp@section@target@nnn{}{#1}{#3}\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
    \tagtool{para-flattened=true} % or \bool_set_true\l_@@_para_flattened_bool
      #6{%
         \ifnum #2>\c@secnumdepth
          \@hangfrom {\hskip #3\relax\@svsec}%
         \else
          \@kernel@tag@hangfrom{\hskip #3\relax\@svsec}%
         \fi
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \ifnum #2>\c@secnumdepth
       \@svsec
      \else
       \@kernel@tag@svsec
      \fi  #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}
\def\@ssect#1#2#3#4#5{%
  \@tempskipa #3\relax
  \ifdim \@tempskipa>\z@
    \begingroup
    \tagtool{para-flattened=true}
      #4{%
        \@hangfrom{\hskip #1\relax\@hyp@section@target@nnn{[section]}{}{#1}}%
          \interlinepenalty \@M #5\@@par}%
    \endgroup
  \else
    \def\@svsechd{#4{\hskip #1\relax\@hyp@section@target@nnn{[section]}{}{#3}\relax #5}}%
  \fi
  \@xsect{#3}}
\def\@xsect#1{%
  \@tempskipa #1\relax
  \ifdim \@tempskipa>\z@
    \par \nobreak
    \vskip \@tempskipa
    \tag_tool:n {restore-para}
    \@afterheading
  \else
    \@nobreakfalse
    \global\@noskipsectrue
    \everypar{%
      \if@noskipsec
        \global\@noskipsecfalse
       {\setbox\z@\lastbox}%
        \clubpenalty\@M
        \begingroup \@svsechd \endgroup
        \unskip
        \tag_tool:n {sec-split-para}
        \@tempskipa #1\relax
        \hskip -\@tempskipa
      \else
        \clubpenalty \@clubpenalty
        \everypar{}%
      \fi}%
  \fi
  \ignorespaces}

\endinput
%%
%% End of file `latex-lab-testphase-sec.sty'.
