\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage
  {markdownthemewitiko_expltools_explcheck_warnings-and-errors}%
  {2025-06-19}%
  {0.0.6}%
  {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
\bool_new:N
  \l__expltools_explcheck_work_in_progress_bool
\prop_new:N
  \g__expltools_explcheck_labels_prop
\seq_new:N
  \g__expltools_explcheck_labels_seq
\RequirePackage
  { xcolor }
\cs_new_protected: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
      }
    \bool_if:NTF
      \l__expltools_explcheck_work_in_progress_bool
      {
        \tl_set:NV
          \l_tmpa_tl
          \l__expltools_explcheck_current_label_tl
        \tl_put_right:Nn
          \l_tmpa_tl
          {
            @
            \textcolor
              { gray }
          }
        \tl_put_right:Nx
          \l_tmpa_tl
          {
              { \l__expltools_explcheck_current_label_tl }
          }
        \exp_args:NV
          \index
          \l_tmpa_tl
      }
      {
        \exp_args:NV
          \index
          \l__expltools_explcheck_current_label_tl
      }
    \seq_gput_right:NV
      \g__expltools_explcheck_labels_seq
      \l__expltools_explcheck_current_label_tl
  }
\color_set:nnn
  { __expltools_explcheck_gray }
  { gray }
  { 0.5 }
\cs_new_protected:Nn
  \__expltools_explcheck_display_heading:Nn
  {
    \bool_if:NTF
      \l__expltools_explcheck_work_in_progress_bool
      {
        #1
          {
            \textcolor
              { gray }
              { #2 }
           }
        \markdownSetup
          {
            renderers = {
              headerAttributeContextEnd += {
                \color_select:n
                  { __expltools_explcheck_gray }
                \usemintedstyle { bw }
              },
            }
          }
      }
      {
        #1
          { #2 }
      }
    \__expltools_explcheck_increment_counters:
  }
\cs_generate_variant:Nn
  \__expltools_explcheck_display_heading:Nn
  { NV }
\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:NVNTF
              \g__expltools_explcheck_labels_prop
              \l_tmpb_tl
              \l_tmpb_tl
              {
                \tl_put_right:Nx
                  \l_tmpa_tl
                  { { \l_tmpb_tl } }
              }
              {
                \msg_error:nnV
                  { expltools / explcheck }
                  { unknown-referenced-label }
                  \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:
        \bool_set_true:N
          \l__expltools_explcheck_work_in_progress_bool
        \markdownSetup
          {
            renderers = {
              attributeIdentifier = {
                \tl_set:Nn
                  \l__expltools_explcheck_current_identifier_tl
                  { ##1 }
              },
              attributeKeyValue = {
                \str_if_eq:nnT
                  { ##1 }
                  { label }
                  {
                    \bool_set_false:N
                      \l__expltools_explcheck_work_in_progress_bool
                    \tl_set:Nx
                      \l_tmpa_tl
                      { \str_uppercase:n { ##2 } }
                    \str_if_eq:VVF
                      \l_tmpa_tl
                      \l__expltools_explcheck_current_label_tl
                      {
                        \tl_set:Nx
                          \l_tmpb_tl
                          { \tl_tail:V \l_tmpa_tl }
                        % If the provided label is out-of-order, check whether
                        % it had already been used.
                        \seq_if_in:NVTF
                          \g__expltools_explcheck_labels_seq
                          \l_tmpb_tl
                          {
                            % If it had already been used, raise an error.
                            \msg_error:nnVV
                              { expltools / explcheck }
                              { unexpected-label }
                              \l_tmpa_tl
                              \l__expltools_explcheck_current_label_tl
                          }
                          {
                            % Otherwise, skip any intermediate labels and use it.
                            \int_gset:Nn
                              \g__expltools_explcheck_subsection_int
                              { \tl_tail:V \l_tmpb_tl }
                            \tl_set:NV
                              \l__expltools_explcheck_current_label_tl
                              \l_tmpa_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 = {
                \str_if_eq:nnTF
                  { ##1 }
                  { work-in-progress }
                  {
                    \bool_set_true:N
                      \l__expltools_explcheck_work_in_progress_bool
                  }
                  {
                    \tl_set:Nx
                      \l__expltools_explcheck_current_label_tl
                      {
                        \str_uppercase:n { ##1 }
                        \thesection
                        \g__expltools_explcheck_subsection_tl
                      }
                    \markdownSetup
                      {
                        renderers = {
                          headingTwo = {
                            \tl_set:Nn
                              \l_tmpa_tl
                              { ####1~[ }
                            \tl_put_right:NV
                              \l_tmpa_tl
                              \l__expltools_explcheck_current_label_tl
                            \tl_put_right:Nn
                              \l_tmpa_tl
                              { ] }
                            \__expltools_explcheck_display_heading:NV
                              \subsection
                              \l_tmpa_tl
                          },
                          headingThree = {
                            \tl_set:Nn
                              \l_tmpa_tl
                              { ####1~[ }
                            \tl_put_right:NV
                              \l_tmpa_tl
                              \l__expltools_explcheck_current_label_tl
                            \tl_put_right:Nn
                              \l_tmpa_tl
                              { ] }
                            \__expltools_explcheck_display_heading:NV
                              \subsubsection
                              \l_tmpa_tl
                          },
                        },
                      }
                  }
              }
            },
          }
      },
      headerAttributeContextEnd = {
        \group_end:
      },
      sectionBegin = {
        \group_begin:
        \color_group_begin:
      },
      sectionEnd = {
        \color_group_end:
        \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 }
\msg_new:nnn
  { expltools / explcheck }
  { unknown-referenced-label }
  { Referenced~section~label~"#1"~does~not~exist~or~is~a~forward-reference }
