%%
%% This is file `document-structure.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% document-structure.dtx  (with options: `package')
%% 
\ProvidesExplPackage{document-structure}{2022/09/14}{3.2.0}{Modular Document Structure}
\RequirePackage{l3keys2e}

\keys_define:nn{ document-structure }{
  class       .str_set_x:N  = \c_document_structure_class_str,
  topsect     .str_set_x:N  = \c_document_structure_topsect_str,
  unknown     .code:n       = {
    \PassOptionsToClass{\CurrentOption}{stex}
    \PassOptionsToClass{\CurrentOption}{tikzinput}
  }
}
\ProcessKeysOptions{ document-structure }
\str_if_empty:NT \c_document_structure_class_str {
  \str_set:Nn \c_document_structure_class_str {article}
}
\str_if_empty:NT \c_document_structure_topsect_str {
  \str_set:Nn \c_document_structure_topsect_str {section}
}
\RequirePackage{xspace}
\RequirePackage{comment}
\RequirePackage{stex}
\AddToHook{begindocument}{
\ltx@ifpackageloaded{babel}{
    \clist_set:Nx \l_tmpa_clist {\bbl@loaded}
    \exp_args:NNx \clist_if_in:NnT \l_tmpa_clist {\detokenize{ngerman}}{
      \makeatletter\input{document-structure-ngerman.ldf}\makeatother
    }
  }{}
}
\int_new:N \l_document_structure_section_level_int
\str_case:VnF \c_document_structure_topsect_str {
  {part}{
    \int_set:Nn \l_document_structure_section_level_int {0}
  }
  {chapter}{
    \int_set:Nn \l_document_structure_section_level_int {1}
  }
}{
  \str_case:VnF \c_document_structure_class_str {
    {book}{
      \int_set:Nn \l_document_structure_section_level_int {0}
    }
    {report}{
      \int_set:Nn \l_document_structure_section_level_int {0}
    }
  }{
    \int_set:Nn \l_document_structure_section_level_int {2}
  }
}
\def\current@section@level{document}%
\newcommand\currentsectionlevel{\lowercase\expandafter{\current@section@level}\xspace}%
\newcommand\Currentsectionlevel{\expandafter\MakeUppercase\current@section@level\xspace}%
\cs_new_protected:Npn \skipfragment {
  \ifcase\l_document_structure_section_level_int
  \or\stepcounter{part}
  \or\stepcounter{chapter}
  \or\stepcounter{section}
  \or\stepcounter{subsection}
  \or\stepcounter{subsubsection}
  \or\stepcounter{paragraph}
  \or\stepcounter{subparagraph}
  \fi
}
\newcommand\at@begin@blindsfragment[1]{}
\newenvironment{blindfragment}
{
  \int_incr:N\l_document_structure_section_level_int
  \at@begin@blindsfragment\l_document_structure_section_level_int
}{}
\newcommand\sfragment@nonum[2]{
  \ifx\hyper@anchor\@undefined\else\phantomsection\fi
  \addcontentsline{toc}{#1}{#2}\@nameuse{#1}*{#2}
}
\newcommand\sfragment@num[2]{
  \tl_if_empty:NTF \l__document_structure_sfragment_short_tl {
    \@nameuse{#1}{#2}
  }{
    \cs_if_exist:NTF\rdfmeta@sectioning{
      \@nameuse{rdfmeta@#1@old}[\l__document_structure_sfragment_short_tl]{#2}
    }{
      \@nameuse{#1}[\l__document_structure_sfragment_short_tl]{#2}
    }
  }
}
\keys_define:nn { document-structure / sfragment }{
  id            .str_set_x:N = \l__document_structure_sfragment_id_str,
  date          .str_set_x:N = \l__document_structure_sfragment_date_str,
  creators      .clist_set:N = \l__document_structure_sfragment_creators_clist,
  contributors  .clist_set:N = \l__document_structure_sfragment_contributors_clist,
  srccite       .tl_set:N    = \l__document_structure_sfragment_srccite_tl,
  type          .tl_set:N    = \l__document_structure_sfragment_type_tl,
  short         .tl_set:N    = \l__document_structure_sfragment_short_tl,
  intro         .tl_set:N    = \l__document_structure_sfragment_intro_tl,
  imports       .tl_set:N    = \l__document_structure_sfragment_imports_tl,
  loadmodules   .bool_set:N  = \l__document_structure_sfragment_loadmodules_bool
}
\cs_new_protected:Nn \__document_structure_sfragment_args:n {
  \str_clear:N \l__document_structure_sfragment_id_str
  \str_clear:N \l__document_structure_sfragment_date_str
  \clist_clear:N \l__document_structure_sfragment_creators_clist
  \clist_clear:N \l__document_structure_sfragment_contributors_clist
  \tl_clear:N \l__document_structure_sfragment_srccite_tl
  \tl_clear:N \l__document_structure_sfragment_type_tl
  \tl_clear:N \l__document_structure_sfragment_short_tl
  \tl_clear:N \l__document_structure_sfragment_imports_tl
  \tl_clear:N \l__document_structure_sfragment_intro_tl
  \bool_set_false:N \l__document_structure_sfragment_loadmodules_bool
  \keys_set:nn { document-structure / sfragment } { #1 }
}
\newif\if@mainmatter\@mainmattertrue
\newcommand\at@begin@sfragment[3][]{}
\keys_define:nn { document-structure / sectioning }{
  name    .str_set_x:N  = \l__document_structure_sect_name_str   ,
  ref     .str_set_x:N  = \l__document_structure_sect_ref_str    ,
  clear   .bool_set:N   = \l__document_structure_sect_clear_bool ,
  clear   .default:n    = {true}                ,
  num     .bool_set:N   = \l__document_structure_sect_num_bool   ,
  num     .default:n    = {true}
}
\cs_new_protected:Nn \__document_structure_sect_args:n {
  \str_clear:N \l__document_structure_sect_name_str
  \str_clear:N \l__document_structure_sect_ref_str
  \bool_set_false:N \l__document_structure_sect_clear_bool
  \bool_set_false:N \l__document_structure_sect_num_bool
  \keys_set:nn { document-structure / sectioning } { #1 }
}
\newcommand\omdoc@sectioning[3][]{
  \__document_structure_sect_args:n {#1 }
  \let\omdoc@sect@name\l__document_structure_sect_name_str
  \bool_if:NT \l__document_structure_sect_clear_bool { \cleardoublepage }
  \if@mainmatter% numbering not overridden by frontmatter, etc.
    \bool_if:NTF \l__document_structure_sect_num_bool {
      \sfragment@num{#2}{#3}
    }{
      \sfragment@nonum{#2}{#3}
    }
    \def\current@section@level{\omdoc@sect@name}
  \else
    \sfragment@nonum{#2}{#3}
  \fi
}% if@mainmatter
\newcommand\sfragment@redefine@addtocontents[1]{%
}% hypreref.sty loaded?
\newenvironment{sfragment}[2][]% keys, title
{
  \__document_structure_sfragment_args:n { #1 }%\sref@target%
  \stex_csl_to_imports:No \usemodule \l__document_structure_sfragment_imports_tl

  \bool_if:NT \l__document_structure_sfragment_loadmodules_bool {
    \sfragment@redefine@addtocontents{
      %\@ifundefined{module@id}\used@modules%
      %{\@ifundefined{module@\module@id @path}{\used@modules}\module@id}
    }
  }

  \stex_document_title:n { #2 }

  \int_incr:N\l_document_structure_section_level_int
  \ifcase\l_document_structure_section_level_int
    \or\omdoc@sectioning[name=\omdoc@part@kw,clear,num]{part}{#2}
    \or\omdoc@sectioning[name=\omdoc@chapter@kw,clear,num]{chapter}{#2}
    \or\omdoc@sectioning[name=\omdoc@section@kw,num]{section}{#2}
    \or\omdoc@sectioning[name=\omdoc@subsection@kw,num]{subsection}{#2}
    \or\omdoc@sectioning[name=\omdoc@subsubsection@kw,num]{subsubsection}{#2}
    \or\omdoc@sectioning[name=\omdoc@paragraph@kw,ref=this \omdoc@paragraph@kw]{paragraph}{#2}
    \or\omdoc@sectioning[name=\omdoc@subparagraph@kw,ref=this \omdoc@subparagraph@kw]{paragraph}{#2}
  \fi
  \at@begin@sfragment[#1]\l_document_structure_section_level_int{#2}
  \str_if_empty:NF \l__document_structure_sfragment_id_str {
    \stex_ref_new_doc_target:n\l__document_structure_sfragment_id_str
  }
}% for customization
{}
\newcommand\omdoc@part@kw{Part}
\newcommand\omdoc@chapter@kw{Chapter}
\newcommand\omdoc@section@kw{Section}
\newcommand\omdoc@subsection@kw{Subsection}
\newcommand\omdoc@subsubsection@kw{Subsubsection}
\newcommand\omdoc@paragraph@kw{paragraph}
\newcommand\omdoc@subparagraph@kw{subparagraph}
\providecommand\printindex{\IfFileExists{\jobname.ind}{\input{\jobname.ind}}{}}
\cs_if_exist:NTF\frontmatter{
  \let\__document_structure_orig_frontmatter\frontmatter
  \let\frontmatter\relax
}{
  \tl_set:Nn\__document_structure_orig_frontmatter{
    \clearpage
    \@mainmatterfalse
    \pagenumbering{roman}
  }
}
\cs_if_exist:NTF\backmatter{
  \let\__document_structure_orig_backmatter\backmatter
  \let\backmatter\relax
}{
  \tl_set:Nn\__document_structure_orig_backmatter{
    \clearpage
    \@mainmatterfalse
    \pagenumbering{roman}
  }
}
\newenvironment{frontmatter}{
  \__document_structure_orig_frontmatter
}{
  \cs_if_exist:NTF\mainmatter{
    \mainmatter
  }{
    \clearpage
    \@mainmattertrue
    \pagenumbering{arabic}
  }
}
\newenvironment{backmatter}{
  \__document_structure_orig_backmatter
}{
  \cs_if_exist:NTF\mainmatter{
    \mainmatter
  }{
    \clearpage
    \@mainmattertrue
    \pagenumbering{arabic}
  }
}
\@mainmattertrue\pagenumbering{arabic}
\def \c__document_structure_document_str{document}
\newcommand\afterprematurestop{}
\def\prematurestop@endsfragment{
  \unless\ifx\@currenvir\c__document_structure_document_str
    \expandafter\expandafter\expandafter\end\expandafter\expandafter\expandafter{\expandafter\@currenvir\expandafter}
    \expandafter\prematurestop@endsfragment
  \fi
}
\providecommand\prematurestop{
  \message{Stopping~sTeX~processing~prematurely}
  \prematurestop@endsfragment
  \afterprematurestop
  \end{document}
}
\RequirePackage{etoolbox}
\newcommand\setSGvar[1]{\@namedef{sTeX@Gvar@#1}}
\newrobustcmd\useSGvar[1]{%
  \@ifundefined{sTeX@Gvar@#1}
  {\PackageError{document-structure}
    {The sTeX Global variable #1 is undefined}
    {set it with \protect\setSGvar}}
\@nameuse{sTeX@Gvar@#1}}
\newrobustcmd\ifSGvar[3]{\def\@test{#2}%
  \@ifundefined{sTeX@Gvar@#1}
  {\PackageError{document-structure}
    {The sTeX Global variable #1 is undefined}
    {set it with \protect\setSGvar}}
  {\expandafter\ifx\csname sTeX@Gvar@#1\endcsname\@test #3\fi}}
\endinput
%%
%% End of file `document-structure.sty'.
