\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage
  {markdownthemewitiko_expltools_explcheck_warnings-and-errors}%
  {2025-02-14}%
  {0.0.4}%
  {Snippets for typesetting the documentation of the warnings and errors for %
   the expl3 analysis tool}
\int_new:N
  \g__expltools_explcheck_subsection_int
\tl_new:N
  \g__expltools_explcheck_subsection_tl
\tl_new:N
  \l__expltools_explcheck_current_label_tl
\tl_new:N
  \l__expltools_explcheck_current_identifier_tl
\prop_new:N
  \g__expltools_explcheck_labels_prop
\cs_new:Nn
  \__expltools_explcheck_increment_counters:
  {
    \int_gincr:N
      \g__expltools_explcheck_subsection_int
    \int_compare:nTF
      { \g__expltools_explcheck_subsection_int < 10 }
      {
        \tl_gset:Nn
          \g__expltools_explcheck_subsection_tl
          { 0 }
        \tl_gput_right:NV
          \g__expltools_explcheck_subsection_tl
          \g__expltools_explcheck_subsection_int
      }
      {
        \tl_gset:NV
          \g__expltools_explcheck_subsection_tl
          \g__expltools_explcheck_subsection_int
      }
    \tl_if_empty:NF
      \l__expltools_explcheck_current_identifier_tl
      {
        \prop_gput:NVV
          \g__expltools_explcheck_labels_prop
          \l__expltools_explcheck_current_identifier_tl
          \l__expltools_explcheck_current_label_tl
        \exp_args:NV
          \label
          \l__expltools_explcheck_current_identifier_tl
      }
    \exp_args:NV
      \index
      \l__expltools_explcheck_current_label_tl
  }
\markdownSetupSnippet
  { body }
  {
    citations,
    content_blocks,
    definition_lists,
    fenced_code,
    notes,
    header_attributes,
    html,
    raw_attribute,
    relative_references,
    renderers = {
      underscore = {
        \discretionary
          { _ \textnormal { - } }
          { }
          { _ }
      },
      link = {
        \tl_set:Nn
          \l_tmpa_tl
          { #3 }
        \tl_set:Nx
          \l_tmpb_tl
          {
            \tl_range:Nnn
              \l_tmpa_tl
              { 1 }
              { 1 }
          }
        \str_if_eq:NNT
          \l_tmpb_tl
          \c_hash_str
          {
            \tl_set:Nx
              \l_tmpb_tl
              {
                \tl_range:Nnn
                  \l_tmpa_tl
                  { 2 }
                  { -1 }
              }
            \tl_set:Nn
              \l_tmpa_tl
              { \hyperref [ }
            \tl_put_right:NV
              \l_tmpa_tl
              \l_tmpb_tl
            \tl_put_right:Nn
              \l_tmpa_tl
              { ] }
            \prop_get:NVN
              \g__expltools_explcheck_labels_prop
              \l_tmpb_tl
              \l_tmpb_tl
            \tl_put_right:Nx
              \l_tmpa_tl
              { { \l_tmpb_tl } }
            \tl_use:N
              \l_tmpa_tl
          }
      },
      headingOne = {
        \section { #1 }
        \int_gset:Nn
          \g__expltools_explcheck_subsection_int
          { 0 }
        \tl_gset:Nn
          \g__expltools_explcheck_subsection_tl
          { 00 }
      },
      headerAttributeContextBegin = {
        \group_begin:
        \markdownSetup
          {
            renderers = {
              attributeIdentifier = {
                \tl_set:Nn
                  \l__expltools_explcheck_current_identifier_tl
                  { ##1 }
              },
              attributeKeyValue = {
                \str_if_eq:nnT
                  { ##1 }
                  { label }
                  {
                    \tl_set:Nx
                      \l_tmpa_tl
                      { \str_uppercase:n { ##2 } }
                    \str_if_eq:VVF
                      \l_tmpa_tl
                      \l__expltools_explcheck_current_label_tl
                      {
                        \msg_error:nnVV
                          { expltools / explcheck }
                          { unexpected-label }
                          \l_tmpa_tl
                          \l__expltools_explcheck_current_label_tl
                      }
                  }
                \str_if_eq:nnT
                  { ##1 }
                  { removed }
                  {
                    % Remove the rest of this section from the output.
                    \markdownSetup
                      {
                        renderers = {
                          heading(Two|Tree) = {
                            \__expltools_explcheck_increment_counters:
                          },
                          headerAttributeContextEnd += {
                            \cs_set:Npn
                              \next
                              ########1 \markdownRendererSectionEnd
                              {
                                \markdownRendererSectionEnd
                              }
                            \next
                          },
                        },
                      }
                  }
              },
              attributeClassName = {
                \tl_set:Nx
                  \l__expltools_explcheck_current_label_tl
                  {
                    \str_uppercase:n { ##1 }
                    \thesection
                    \g__expltools_explcheck_subsection_tl
                  }
                \markdownSetup
                  {
                    renderers = {
                      headingTwo = {
                        \subsection
                          {
                            ####1~[ \l__expltools_explcheck_current_label_tl ]
                          }
                        \__expltools_explcheck_increment_counters:
                      },
                      headingThree = {
                        \subsubsection
                          {
                            ####1~[ \l__expltools_explcheck_current_label_tl ]
                          }
                        \__expltools_explcheck_increment_counters:
                      },
                    },
                  }
              }
            },
          }
      },
      headerAttributeContextEnd = {
        \group_end:
      },
      contentBlock = {
        \str_case:nnF
          { #1 }
          {
            { tex }
              {
                \markdownRendererInputFencedCode
                  { #3 }
                  { tex }
                  { tex }
              }
          }
          {
            \markdownInput
              { #3 }
          }
      },
    },
  }
\msg_new:nnn
  { expltools / explcheck }
  { unexpected-label }
  { Expected~section~label~"#1",~but~"#2"~was~generated }
