% \iffalse meta-comment
%
%% File: latex-lab-minipage.dtx (C) Copyright 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-minipage.dtx}
\end{document}
%</driver>
%
% \fi
%

\def\ltlistsversion{v0.81}
\def\ltlistsdate{2023/05/10}

%
% \title{The \textsf{latex-lab-minipage} package\\
% Changes related to the tagging of minipage and parbox}
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
% \date{\ltlistsversion\ \ltlistsdate}
%
% \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}
% 
%  How to tag minipages and parboxes is still under discussion. 
%  But as they start and contain paragraphs it is clear that 
%  hooks are needed to interrupt and restart the tagging. 
%  
%  The tagging added is currently quite simple minded. See the todo-testfiles and the
%  code comments for some open points. 
%  
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \section{Implementation}
%    \begin{macrocode}
\ProvidesExplPackage {latex-lab-testphase-minipage} {2023-05-10} {0.81}
  {Code related to the tagging of minipages and parboxes}
%    \end{macrocode}
%  

% \subsection{Provide the kernel hooks}
% [kernel?]
% We need to check/set the status before the
% initial \cs{leavevmode} to decide how to handle the structure. 
% Perhaps the same command will work for both?
%    \begin{macrocode}
\def\@kernel@init@minipage{}
\def\@kernel@init@parbox{}
%    \end{macrocode}
% We need a few hooks.
%    \begin{macrocode}
\def\@kernel@before@minipage{}
\def\@kernel@after@minipage{}
\def\@kernel@before@parbox{}
\def\@kernel@after@parbox{}
%    \end{macrocode}
%
% \subsection{Patch minipage}
% [kernel?]
% The begin hook is inserted in \cs{@iiiminipage}
%    \begin{macrocode}
\def\@iiiminipage#1#2[#3]#4{%
%    \end{macrocode}
% Before switching to hmode we insert a hook. It is currently unused
% but could for example check if we are in vmode and change the following
% paragraph tag. 
%    \begin{macrocode}
  \@kernel@init@minipage
  \leavevmode 
  \@pboxswfalse
  \setlength\@tempdima{#4}%
  \def\@mpargs{{#1}{#2}[#3]{#4}}%
%    \end{macrocode}
% insert the hook
%    \begin{macrocode}
  \@kernel@before@minipage% new
  \setbox\@tempboxa\vbox\bgroup
    \color@begingroup
      \hsize\@tempdima
      \textwidth\hsize \columnwidth\hsize
      \@parboxrestore
      \def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@
      \let\@footnotetext\@mpfootnotetext
      \let\@listdepth\@mplistdepth \@mplistdepth\z@
      \@minipagerestore
      \@setminipage}
%    \end{macrocode}
%  And a hook at the end
%    \begin{macrocode}
\def\endminipage{%
    \par
    \unskip
    \ifvoid\@mpfootins\else
      \vskip\skip\@mpfootins
      \normalcolor
      \footnoterule
      \unvbox\@mpfootins
    \fi
    \@minipagefalse   %% added 24 May 89
  \color@endgroup
  \egroup
%    \end{macrocode}
% as the parbox command is called, we must prevent that it adds its additional
% tagging commands:
%    \begin{macrocode}
  \tag_stop:n{minipage}
  \expandafter\@iiiparbox\@mpargs{\unvbox\@tempboxa}%
  \tag_start:n{minipage}
  \@kernel@after@minipage%new end ...
  }
%    \end{macrocode}
%
% And similar for \cs{parbox}
%    \begin{macrocode}
\long\def\@iiiparbox#1#2[#3]#4#5{%
  \@kernel@init@parbox
  \leavevmode
  \@pboxswfalse
  \setlength\@tempdima{#4}%
  \@kernel@before@parbox% new
  \@begin@tempboxa\vbox{\hsize\@tempdima\@parboxrestore#5\@@par}%
    \ifx\relax#2\else
      \setlength\@tempdimb{#2}%
      \edef\@parboxto{to\the\@tempdimb}%
    \fi
    \if#1b\vbox
    \else\if #1t\vtop
    \else\ifmmode\vcenter
    \else\@pboxswtrue $\vcenter
    \fi\fi\fi
    \@parboxto{\let\hss\vss\let\unhbox\unvbox
       \csname bm@#3\endcsname}%
    \if@pboxsw \m@th$\fi
  \@end@tempboxa%
  \@kernel@after@parbox%new end
  }
%    \end{macrocode}
%
% \subsection{Tagging commands}
% 
%    \begin{macrocode}
\tl_new:N  \l__ltboxes_tag_tl
\tl_set:Nn \l__ltboxes_tag_tl {Div}
\cs_set_protected:Npn\@kernel@before@minipage
  {
%    \end{macrocode}
% As there was a \cs{leavevmode} before that is a P/text open that we have to close.
%    \begin{macrocode}
    \tag_mc_end_push:\tag_struct_end:
%    \end{macrocode}
% TODO: the P can be further up, we need a proper method to test and handle this. 
%    \begin{macrocode}   
    \tag_struct_begin:n{tag=\l__ltboxes_tag_tl}}
\cs_set_protected:Npn\@kernel@after@minipage
  {\tag_struct_end:\tag_struct_begin:n{tag=\l__tag_para_tag_tl}\tag_mc_begin_pop:n{}}
\cs_set_protected:Npn\@kernel@before@parbox
  {\tag_mc_end_push:\tag_struct_end:\tag_struct_begin:n{tag=\l__ltboxes_tag_tl}}
\cs_set_protected:Npn\@kernel@after@parbox
  {\tag_struct_end:\tag_struct_begin:n{tag=\l__tag_para_tag_tl}\tag_mc_begin_pop:n{}}
%    \end{macrocode}
%    \begin{macrocode}
%</package>  
%    \end{macrocode}

%    \begin{macrocode}
%<*latex-lab>
\ProvidesFile{minipage-latex-lab-testphase.ltx}
        [2022-10-26 v0.8 code related to the tagging of sectioning commands]
\RequirePackage{latex-lab-testphase-minipage}
%</latex-lab>
%    \end{macrocode}
