% -*- coding: utf-8 -*-
% SPDX-FileCopyrightText: © 2026 Franz Glasner
% SPDX-License-Identifier: BSD-3-Clause OR MIT
%
% Some font hacks for my Sphinx PDF documents.
%
% Options:
% - stdtitle=<bool>: Use the Sphinx standard title when `true' or an
%                    augmented title (build date, VCS revision id).
%                    Default: false.
%
% Commands:
% - \vcsrevision#1
% - \builddate#1
% - \SuppressSectionNumbering
% - \SuppressSubsectionNumbering
% - \SuppressSubsubsectionNumbering
% - \EndSuppressNumbering
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{my-doc-style}[%
  2026/06/06 rv:ddd6d2bb4fdb (Franz Glasner) Some style customizations (title et al.)]
\RequirePackage{xkeyval}
\RequirePackage{hologo}

% A key compatible with options processing but bool (because DeclareBoolOptionX)
% If given than the Sphinx standard titlepage is used without changes.
\define@boolkey{my-doc-style.sty}{stdtitle}[true]{}

\ExecuteOptionsX{stdtitle=false}
\ProcessOptionsX\relax

%
% For the title page: "Last updated on ..."
%
\DeclareRobustCommand*\vcsrevision[1]{\gdef\mds@vcs@revision{#1}}
\DeclareRobustCommand*\builddate[1]{\gdef\mds@builddate{#1}}
\vcsrevision{}
\builddate{}

% \textsf consumes an argument!
\newcommand{\mds@titlefont}{\textsf}
\newcommand{\mds@titlefontsize}{\small}
%
% If the option "stdtitle" was not enabled augment the titlepage with
% the documentation build date and the VCS revision id.
%
\csname ifKV@my-doc-style.sty@stdtitle\endcsname
  \relax
\else
  % \sphinxmaketitle calls the macro \@thanks: (ab)use this
  \gdef\@thanks{%
    \ifx\mds@builddate\@empty\relax%
      \ifx\mds@vcs@revision\@empty\relax%
      \else%
        \hfill\mds@titlefontsize\mds@titlefont{(rev\ \mds@vcs@revision )}%
      \fi%
    \else%
      \hfill\mds@titlefontsize\mds@titlefont{Last updated on \mds@builddate%
      \ifx\mds@vcs@revision\@empty\relax\else\enspace(rev\ \mds@vcs@revision )\fi}
    \fi%
  }
\fi

%
% Allow to suppress section numbering for some parts of the document
% (e.g. in changelog entries).
%
% Sphinx employs the "titlesec" package to style its chapter and section
% headings and uses its \titleformat command with a 0.5em separation
% (see \titleformat calls in sphinxlatexstyleheadings.sty).
% So make \the<section> output nothing (e.g. the section number)
% but just a compensation for this whitespace.
%
% Each suppress command suppresses its level and all levels below
% (compare with the \titleformat calls in sphinxlatexstyleheadings.sty).
%
% \EndSuppressNumbering ends exactly one preceding
% \SuppressSubsectionNumbering or \SuppressSubsubsectionNumbering.
%

%
% A counter to store the current (Sphinx-set) tocdepth.
% Needed because counters are global in LaTeX.
%
\newcounter{@mds@stored@tocdepth}

\def\SuppressSectionNumbering{%
  \begingroup
  \setcounter{@mds@stored@tocdepth}{\value{tocdepth}}
  \addtocontents{toc}{\setcounter{tocdepth}{0}}% No \section in ToC
  \renewcommand\thesection{\hspace{-0.5em}}
  \renewcommand\thesubsection{\hspace{-0.5em}}
  \renewcommand\thesubsubsection{\hspace{-0.5em}}
  \renewcommand\theparagraph{\hspace{-0.5em}}
  \renewcommand\thesubparagraph{\hspace{-0.5em}}
}

\def\SuppressSubsectionNumbering{%
  \begingroup
  \setcounter{@mds@stored@tocdepth}{\value{tocdepth}}
  \addtocontents{toc}{\setcounter{tocdepth}{1}}% Allow \section in ToC
  \renewcommand\thesubsection{\hspace{-0.5em}}
  \renewcommand\thesubsubsection{\hspace{-0.5em}}
  \renewcommand\theparagraph{\hspace{-0.5em}}
  \renewcommand\thesubparagraph{\hspace{-0.5em}}
}
\def\SuppressSubsubsectionNumbering{%
  \begingroup
  \setcounter{@mds@stored@tocdepth}{\value{tocdepth}}
  \addtocontents{toc}{\setcounter{tocdepth}{2}}% Allow \subsection in ToC
  \renewcommand\thesubsubsection{\hspace{-0.5em}}
  \renewcommand\theparagraph{\hspace{-0.5em}}
  \renewcommand\thesubparagraph{\hspace{-0.5em}}
}

\def\EndSuppressNumbering{%
  % Reset the tocdepth to the previous value
  \addtocontents{toc}{\setcounter{tocdepth}{\the@mds@stored@tocdepth}}% Default
  \endgroup
}

%
% Use the installed sans font as document body font and let all headings
% use the main/serif font.
%
\newcommand{\SwitchFamilyDefaultToSans}{%
  \renewcommand*{\familydefault}{\sfdefault}
  \@ifpackageloaded{fncychap}{%
    \ChNameVar{\Large\rm}
    \ChTitleVar{\large\rm}
  }{}
  \renewcommand{\mds@titlefont}{\textrm}
  \renewcommand{\mds@titlefontsize}{\footnotesize}
  \renewcommand{\DUroletextother}{\textrm}
}

%
% For SmallCaps support and/or custom reST roles:
% see also: https://docutils.sourceforge.io/docs/user/latex.html
%

%
% SmallCaps role support (:smallcaps:).
% Here without argument declaration because \textsc
% expects one and will consume it instead.
%
\newcommand{\DUrolesmallcaps}{\textsc}

% Testing sans serif fonts with ":textsf:" role
% Here without argument declaration because \textsf
% expects one and will consume it instead.
\newcommand{\DUroletextsf}{\textsf}

% Testing serif fonts with ":textrm:" role
% Here without argument declaration because \textrm
% expects one and will consume it instead.
\newcommand{\DUroletextrm}{\textrm}

% Testing serif fonts with ":textother:" role
% Here without argument declaration because \textrm or \textsf
% expect one and will consume it instead.
\newcommand{\DUroletextother}{\textsf}


% TeX logos using hologo.
% The role argument will consumed by the \hologo command.
\newcommand*{\DUrolehologo}{\hologo}

\endinput
