% !Mode:: "TeX:DE:UTF-8:Main"

% This example shows how one can patch lists command
% it is based on enumitem

% Pay attention to casing. Li/LI, Lbody/Lbody are different.

% It doesn't test nesting!

% We are using \DocumentMetadata
% This requires a new pdfmanagement and/or latex-dev! 
% Older systems should use \DeclareDocumentMetadata
\IfFormatAtLeastTF{2022-06-01}{}
  {\RequirePackage{pdfmanagement-testphase}}
\DocumentMetadata{pdfversion=1.7,uncompress,testphase=tagpdf}


\documentclass{scrbook}
\usepackage[english]{babel}

\usepackage{iftex}
\ifluatex
  \usepackage{fontspec}
  \usepackage{luacode}
\else
  \usepackage[T1]{fontenc}
\fi



\tagpdfsetup
  {
    tabsorder=structure,
  }

\usepackage{lipsum}%\textheight3cm

\usepackage{enumitem,xpatch}

% At first we add to the begin/end hooks the structure commands.
% To avoid problems with nested paragraphs we force a new paragraph for now. 
% Real code should avoid that!
\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

\makeatletter
\ExplSyntaxOn
%We detect is this is the first or a follow up item where we have to end a structure first:  
\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}

\makeatother



\begin{document}
%I don't want to handle pagestyles, so set everything to empty:
\pagestyle{empty}\renewcommand\chapterpagestyle{empty}



\begin{itemize}
\item Ducks say quack
\item Marmots say something Parmotlike
\end{itemize}

\begin{enumerate}
\item The highest pass is the Col de l'Iseran.
\item The second highest pass is the Passo dello Stelvio.
\end{enumerate}

\begin{description}
\item[ducks] yellow
\item[marmots] brown
\end{description}


\end{document} 