% The LaTeX generator wraps all custom spans in \DUrole{class}{contents}.  That
% command then checks for another command called \DUroleclass.

% Most of our CSS class names have dashes, so we need ‘\csname … \endcsname’

% <magic>
% \def\newcssclass#1#2{\expandafter\def\csname DUrole#1\endcsname ##1{#2}}
% </magic>

\RequirePackage{adjustbox}
\RequirePackage{xcolor}
\RequirePackage{amsmath}

\definecolor{nbordercolor}{HTML}{AAAAAA}
\definecolor{nbgcolor}{HTML}{EAEAEA}
\definecolor{nholecolor}{HTML}{4E9A06}

\newlength{\nscriptsize}
\setlength{\nscriptsize}{0.8em}

\newlength{\nboxsep}
\setlength{\nboxsep}{2pt}

\newcommand*{\scriptsmallsquarebox}[1]{%
  % Force width
  \makebox[\nscriptsize]{%
    % Force height and center vertically
    \raisebox{\dimexpr .5\nscriptsize - .5\height \relax}[\nscriptsize][0pt]{%
      % Cancel depth
      \raisebox{\depth}{#1}}}}
\newcommand*{\nscriptdecoratedbox}[2][]{\adjustbox{cfbox=nbordercolor 0.5pt 0pt,bgcolor=nbgcolor}{#2}}
\newcommand*{\nscriptbox}[1]{\nscriptdecoratedbox{\scriptsmallsquarebox{\textbf{#1}}}}
\newcommand*{\nscript}[2]{\text{\hspace{-.5\nscriptsize}\raisebox{-#1\nscriptsize}{\nscriptbox{\small#2}}}}
\newcommand*{\nsup}[1]{^{\nscript{0.15}{#1}}}
\newcommand*{\nsub}[1]{_{\nscript{0.35}{#1}}}
\newcommand*{\nnotation}[1]{#1}
\newcommand*{\nbox}[1]{\adjustbox{cfbox=nbordercolor 0.5pt \nboxsep,bgcolor=nbgcolor}{#1}}
\newcommand*{\nrepeat}[1]{\text{\nbox{#1\hspace{.5\nscriptsize}}}}
\newcommand*{\nwrapper}[1]{\ensuremath{\displaystyle#1}} % https://tex.stackexchange.com/questions/310877/
\newcommand*{\nhole}[1]{\textit{\color{nholecolor}#1}}

% <magic>
% Make it easier to define new commands matching CSS classes
\newcommand{\newcssclass}[2]{%
  \expandafter\def\csname DUrole#1\endcsname##1{#2}
}
% </magic>

% https://tex.stackexchange.com/questions/490262/
\def\naltsep{}
\newsavebox{\nsavedalt}
\newlength{\naltvruleht}
\newlength{\naltvruledp}
\def\naltvrule{\smash{\vrule height\naltvruleht depth\naltvruledp}}
\newcommand{\nalternative}[2]{%
  % First measure the contents of the box without the bar
  \bgroup%
  \def\naltsep{}%
  \savebox{\nsavedalt}{#1}%
  \setlength{\naltvruleht}{\ht\nsavedalt}%
  \setlength{\naltvruledp}{\dp\nsavedalt}%
  \addtolength{\naltvruleht}{#2}%
  \addtolength{\naltvruledp}{#2}%
  % Then redraw it with the bar
  \def\naltsep{\naltvrule}%
  #1\egroup}

\newcssclass{notation-sup}{\nsup{#1}}
\newcssclass{notation-sub}{\nsub{#1}}
\newcssclass{notation}{\nnotation{\textbf{#1}}}
\newcssclass{repeat}{\nrepeat{#1}}
\newcssclass{repeat-wrapper}{\nwrapper{#1}}
\newcssclass{repeat-wrapper-with-sub}{\nwrapper{#1}}
\newcssclass{hole}{\nhole{#1}}
\newcssclass{alternative}{\nalternative{\nbox{#1}}{0pt}}
\newcssclass{alternative-block}{#1}
\newcssclass{repeated-alternative}{\nalternative{#1}{\nboxsep}}
\newcssclass{alternative-separator}{\quad\naltsep{}\quad}
\newcssclass{prodn-table}{%
  \begin{savenotes}
  \sphinxattablestart
  \begin{tabulary}{\linewidth}[t]{lLLL}
  #1
  \end{tabulary}
  \par
  \sphinxattableend
  \end{savenotes}}
% latex puts targets 1 line below where they should be; prodn-target corrects for this
\newcssclass{prodn-target}{\raisebox{\dimexpr \nscriptsize \relax}{#1}}
\newcssclass{prodn-cell-nonterminal}{#1 &}
\newcssclass{prodn-cell-op}{#1 &}
\newcssclass{prodn-cell-production}{#1 &}
\newcssclass{prodn-cell-tag}{#1\\}
