Skip to content

Commit

Permalink
LaTeX: refactor how display environments use sphinxpackageboxes
Browse files Browse the repository at this point in the history
This is to share code as much as possible and facilitate future
extensions.
  • Loading branch information
jfbu committed Mar 7, 2023
1 parent 13c779b commit c499e09
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 236 deletions.
61 changes: 26 additions & 35 deletions sphinx/texinputs/sphinx.sty
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{sphinx}[2023/03/06 v6.2.0 LaTeX package (Sphinx markup)]
\ProvidesPackage{sphinx}[2023/03/07 v6.2.0 LaTeX package (Sphinx markup)]

% provides \ltx@ifundefined
% (many packages load ltxcmds: graphicx does for pdftex and lualatex but
Expand Down Expand Up @@ -196,11 +196,15 @@ will be set to white}%
\DeclareStringOption[0.5pt]{hintborder}
\DeclareStringOption[0.5pt]{importantborder}
\DeclareStringOption[0.5pt]{tipborder}
\DeclareStringOption[1pt]{warningborder}
\DeclareStringOption[1pt]{cautionborder}
\DeclareStringOption[1pt]{attentionborder}
\DeclareStringOption[1pt]{dangerborder}
\DeclareStringOption[1pt]{errorborder}
% Compatibility layer to share for these notice types common code
% with the one for warning, danger, ..., in sphinxlatexadmonitions.sty
\def\spx@note@border {\spx@opt@noteborder}
\def\spx@hint@border {\spx@opt@hintborder}
\def\spx@important@border{\spx@opt@importantborder}
\def\spx@tip@border {\spx@opt@tipborder}
% The warning type notices are handled in the "CSS" section below.
% 6.2.0 does not define \spx@opt@warningborder etc... macros
% but only \spx@warning@border etc...
% footnotes
\DeclareStringOption[\mbox{ }]{AtStartFootnote}
% we need a public macro name for direct use in latex file
Expand Down Expand Up @@ -316,22 +320,9 @@ will be set to white}%
%
% Border keys
%
% The "special prefix" is for matters of keeping same naming conventions, for
% both "warning" and "note" type of notices, for the macro storing the border
% width (if setting same width for all borders). These macros were already
% defined above via a series of \DeclareStringOption and thus need a \spx@opt@
% prefix, whereas in general for matters of brievety only \spx@ prefix is being
% used by e.g. the macros storing the separate widths of the four borders.
%
% This \spx@opt@<notice type>border name is expected by sphinxadmonition
% environment, cf. file sphinxlatexadmonitions.sty, whereas we se for
% example \spx@pre@border not \spx@opt@preborder for code-blocks,
% or \spx@warning@border@top for macro holding top border width of
% warning type notice.
%
\def\spx@tempa#1#2{% #1 = macro prefix, #2 = special prefix
\def\spx@tempa#1{% #1 = macro prefix
\expandafter\spx@tempb
\csname #2border\expandafter\endcsname
\csname #1border\expandafter\endcsname
\csname #1border@top\expandafter\endcsname
\csname #1border@right\expandafter\endcsname
\csname #1border@bottom\expandafter\endcsname
Expand All @@ -356,20 +347,20 @@ will be set to white}%
\spx@tempc{#9}% option prefix
}
\def\spx@tempc #1#2{% #1 = option prefix, #2 = legacy option name
% keep backward legacy option as alias to new-named option
% keep legacy option names as aliases to new-named options
\expandafter\let\csname KV@sphinx@#2\expandafter\endcsname
\csname KV@sphinx@#1border-width\endcsname
% init border-width (fetches next argument)
\csname KV@sphinx@#1border-width\endcsname
}
% macro prefix special prefix option prefix legacy option init
\spx@tempa{spx@pre@} {spx@pre@} {pre_} {verbatimborder} \fboxrule
\spx@tempa{spx@topic@} {spx@topic@} {div.topic_} {shadowrule} \fboxrule
\spx@tempa{spx@warning@} {spx@opt@warning} {div.warning_} {warningborder} {1pt}
\spx@tempa{spx@caution@} {spx@opt@caution} {div.caution_} {cautionborder} {1pt}
\spx@tempa{spx@attention@}{spx@opt@attention}{div.attention_}{attentionborder}{1pt}
\spx@tempa{spx@danger@} {spx@opt@danger} {div.danger_} {dangerborder} {1pt}
\spx@tempa{spx@error@} {spx@opt@error} {div.error_} {errorborder} {1pt}
% macro prefix option prefix legacy option init value
\spx@tempa{spx@pre@} {pre_} {verbatimborder} \fboxrule
\spx@tempa{spx@topic@} {div.topic_} {shadowrule} \fboxrule
\spx@tempa{spx@warning@} {div.warning_} {warningborder} {1pt}
\spx@tempa{spx@caution@} {div.caution_} {cautionborder} {1pt}
\spx@tempa{spx@attention@}{div.attention_}{attentionborder}{1pt}
\spx@tempa{spx@danger@} {div.danger_} {dangerborder} {1pt}
\spx@tempa{spx@error@} {div.error_} {errorborder} {1pt}
% Set default box-decoration-break style for codeblocks to slice
\spx@pre@border@opentrue % new default at 6.0.0: slice, not clone

Expand Down Expand Up @@ -405,11 +396,11 @@ will be set to white}%
% from border width for warning type admonitions. The below
% keeps the legacy behavior of a constant borderwidth+padding.
% The dim expression is not evaluated yet, only at time of use.
\spx@tempa{spx@warning@} {div.warning_} {\dimexpr.6\baselineskip-\spx@opt@warningborder\relax}
\spx@tempa{spx@caution@} {div.caution_} {\dimexpr.6\baselineskip-\spx@opt@cautionborder\relax}
\spx@tempa{spx@attention@}{div.attention_} {\dimexpr.6\baselineskip-\spx@opt@attentionborder\relax}
\spx@tempa{spx@danger@} {div.danger_} {\dimexpr.6\baselineskip-\spx@opt@dangerborder\relax}
\spx@tempa{spx@error@} {div.error_} {\dimexpr.6\baselineskip-\spx@opt@errorborder\relax}
\spx@tempa{spx@warning@} {div.warning_} {\dimexpr.6\baselineskip-\spx@warning@border\relax}
\spx@tempa{spx@caution@} {div.caution_} {\dimexpr.6\baselineskip-\spx@caution@border\relax}
\spx@tempa{spx@attention@}{div.attention_} {\dimexpr.6\baselineskip-\spx@attention@border\relax}
\spx@tempa{spx@danger@} {div.danger_} {\dimexpr.6\baselineskip-\spx@danger@border\relax}
\spx@tempa{spx@error@} {div.error_} {\dimexpr.6\baselineskip-\spx@error@border\relax}
% define legacy verbatimsep key as alias of pre_padding key
\expandafter\let\expandafter\KV@sphinx@verbatimsep\csname KV@sphinx@pre_padding\endcsname
% define legacy shadowsep key as alias of div.topic_padding key
Expand Down
80 changes: 13 additions & 67 deletions sphinx/texinputs/sphinxlatexadmonitions.sty
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% NOTICES AND ADMONITIONS
%
% change this info string if making any custom modification
\ProvidesFile{sphinxlatexadmonitions.sty}[2023/03/06 admonitions]
\ProvidesFile{sphinxlatexadmonitions.sty}[2023/03/07 admonitions]

% Provides support for this output mark-up from Sphinx latex writer:
%
Expand Down Expand Up @@ -70,59 +70,17 @@
\definecolor{sphinxtipBgColor}{rgb}{1,1,1}

% Others get more distinction
% This setup macro has somewhat peculiar names for colors, this is
% heritage of the evolution of code while keeping a common base for both
% "warning"-type and "note"-type notices and backward compatibility
\def\spx@admonitions@boxes@fcolorbox@setup{%
\spx@boxes@border@top \@nameuse{spx@\spx@noticetype @border@top}%
\spx@boxes@border@right \@nameuse{spx@\spx@noticetype @border@right}%
\spx@boxes@border@bottom\@nameuse{spx@\spx@noticetype @border@bottom}%
\spx@boxes@border@left \@nameuse{spx@\spx@noticetype @border@left}%
\spx@boxes@border \spx@notice@border
%
\spx@boxes@padding@top \@nameuse{spx@\spx@noticetype @padding@top}%
\spx@boxes@padding@right \@nameuse{spx@\spx@noticetype @padding@right}%
\spx@boxes@padding@bottom\@nameuse{spx@\spx@noticetype @padding@bottom}%
\spx@boxes@padding@left \@nameuse{spx@\spx@noticetype @padding@left}%
%
\spx@boxes@radius@topleft \@nameuse{spx@\spx@noticetype @radius@topleft}%
\spx@boxes@radius@topright \@nameuse{spx@\spx@noticetype @radius@topright}%
\spx@boxes@radius@bottomright \@nameuse{spx@\spx@noticetype @radius@bottomright}%
\spx@boxes@radius@bottomleft \@nameuse{spx@\spx@noticetype @radius@bottomleft}%
\relax
\iftrue\@nameuse{ifspx@\spx@noticetype @withshadow}%
\spx@boxes@withshadowtrue
\spx@boxes@shadow@xoffset \@nameuse{spx@\spx@noticetype @shadow@xoffset}%
\spx@boxes@shadow@yoffset \@nameuse{spx@\spx@noticetype @shadow@yoffset}\relax
\else
\spx@boxes@withshadowfalse
\fi\@nameuse{fi}%
\iftrue\@nameuse{ifspx@\spx@noticetype @insetshadow}%
\spx@boxes@insetshadowtrue
\else
\spx@boxes@insetshadowfalse
\fi\@nameuse{fi}%
%
\iftrue\@nameuse{ifspx@\spx@noticetype @withbackgroundcolor}%
\spx@boxes@withbackgroundcolortrue
\else
\spx@boxes@withbackgroundcolorfalse
\fi\@nameuse{fi}%
\sphinxcolorlet{spx@boxes@backgroundcolor}{spx@notice@bgcolor}%
%
\iftrue\@nameuse{ifspx@\spx@noticetype @withbordercolor}%
\spx@boxes@withbordercolortrue
\else
\spx@boxes@withbordercolorfalse
\fi\@nameuse{fi}%
\sphinxcolorlet{spx@boxes@bordercolor}{spx@notice@bordercolor}%
%
\iftrue\@nameuse{ifspx@\spx@noticetype @withshadowcolor}%
\spx@boxes@withshadowcolortrue
\else
\spx@boxes@withshadowcolorfalse
\fi\@nameuse{fi}%
\sphinxcolorlet{spx@boxes@shadowcolor}{sphinx\spx@noticetype ShadowColor}%
\spx@boxes@fcolorbox@setup
{\spx@noticetype}{spx@notice@bordercolor}%
{spx@notice@bgcolor}%
{sphinx\spx@noticetype ShadowColor}%
}
% rescue code in case sphinxheavybox is used for note-like notices
% (executed if there is no radius.topleft associated macro)
% (this is executed if there is no radius.topleft associated macro)
\def\spx@admonitions@boxes@fcolorbox@setup@fallback{%
\spx@boxes@border@top \spx@notice@border
\spx@boxes@border@right \spx@notice@border
Expand Down Expand Up @@ -151,6 +109,7 @@
% time there is a public interface)
\spx@boxes@withbordercolortrue
\sphinxcolorlet{spx@boxes@bordercolor}{spx@notice@bordercolor}%
\def\spx@boxes@fcolorbox{\spx@boxes@fcolorbox@rectangle}%
}
% Code adapted from framed.sty's "snugshade" environment.
% Nesting works (inner frames do not allow page breaks).
Expand Down Expand Up @@ -193,21 +152,8 @@
\fi
\fi
\fi
\if1% use rounded boxes only if needed and possible
\ifdim\spx@boxes@radius@topleft >\z@0\fi
\ifdim\spx@boxes@radius@topright >\z@0\fi
\ifdim\spx@boxes@radius@bottomright>\z@0\fi
\ifdim\spx@boxes@radius@bottomleft >\z@0\fi
1\spx@boxes@fcolorbox{##1}%
\else
\spx@ifpackageloaded@pictiie
{\ifspx@boxes@insetshadow
\spx@boxes@fcolorbox{##1}%
\else
\spx@boxes@fcolorbox@rounded{##1}%
\fi}%
{\spx@boxes@fcolorbox{##1}}%
\fi
% "setup" macro MUST have been called before
\spx@boxes@fcolorbox{##1}%
\ifspx@boxes@withshadow
\ifspx@boxes@insetshadow\else
\ifdim\spx@boxes@shadow@xoffset>\z@\hskip-\spx@boxes@shadow@xoffset\relax
Expand Down Expand Up @@ -267,7 +213,7 @@
% set parameters of heavybox/lightbox
\sphinxcolorlet{spx@notice@bordercolor}{sphinx#1BorderColor}%
\sphinxcolorlet{spx@notice@bgcolor}{sphinx#1BgColor}%
\spx@notice@border \dimexpr\csname spx@opt@#1border\endcsname\relax
\spx@notice@border \dimexpr\csname spx@#1@border\endcsname\relax
% start specific environment, passing the heading as argument
\begin{sphinx#1}{#2}}
% workaround some LaTeX "feature" of \end command
Expand Down
84 changes: 16 additions & 68 deletions sphinx/texinputs/sphinxlatexliterals.sty
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% LITERAL BLOCKS
%
% change this info string if making any custom modification
\ProvidesFile{sphinxlatexliterals.sty}[2023/03/06 code-blocks and parsed literals]
\ProvidesFile{sphinxlatexliterals.sty}[2023/03/07 code-blocks and parsed literals]

% Provides support for this output mark-up from Sphinx latex writer:
%
Expand Down Expand Up @@ -119,7 +119,7 @@
% interline penalties and glues.
%
\long\def\spx@verb@FrameCommand #1#2#3{%
% The \spx@verb@boxes@fcolorbox@setup must have been executed beforehand.
% The \spx@verb@boxes@fcolorbox@setup MUST have been executed beforehand.
% These \hskips are for fancyvrb.sty measuring and will make the
% framing "adapt" to an indented context.
\hskip\@totalleftmargin
Expand All @@ -141,7 +141,7 @@
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth
}%
\long\def\spx@verb@fcolorbox #1#2#3{%
% The \spx@verb@boxes@fcolorbox@setup must have been executed beforehand.
% The \spx@verb@boxes@fcolorbox@setup MUST have been executed beforehand.
%
% MEMO: in the context of framed.sty this will always expand inside some
% \hbox isolated from other code, so we can use \box\z@, \box\tw@,...
Expand All @@ -159,21 +159,8 @@
+\spx@boxes@border@right\relax sp}%
\vbox{#1% continuation hint attached above frame, uses \spx@verb@fcolorbox@width@sp
% the boxes@fcolorbox constructs an \hbox with bbox containing the border
\if1% use rounded boxes only if needed and possible
\ifdim\spx@boxes@radius@topleft >\z@0\fi
\ifdim\spx@boxes@radius@topright >\z@0\fi
\ifdim\spx@boxes@radius@bottomright>\z@0\fi
\ifdim\spx@boxes@radius@bottomleft >\z@0\fi
1\spx@boxes@fcolorbox{\box\z@}% all radii vanish, do not use pict2e
\else
\spx@ifpackageloaded@pictiie
{\ifspx@boxes@insetshadow
\spx@boxes@fcolorbox{\box\z@}% inset shadow forces straight corners
\else
\spx@boxes@fcolorbox@rounded{\box\z@}% yes, rounded corners!
\fi}%
{\spx@boxes@fcolorbox{\box\z@}}% pict2e package not loaded, force straight
\fi
% \spx@verb@boxes@fcolorbox@setup MUST have been executed beforehand.
\spx@boxes@fcolorbox{\box\z@}%
% This \nointerlineskip to maintain legacy spacing when a \hrule was
% formerly last prior item in vertical list. TODO: remove this at 6.0.0 ?
\nointerlineskip
Expand Down Expand Up @@ -233,56 +220,17 @@
}%
%
\def\spx@verb@boxes@fcolorbox@setup{%
%
\spx@boxes@border@top \spx@pre@border@top
\spx@boxes@border@right \spx@pre@border@right
\spx@boxes@border@bottom\spx@pre@border@bottom
\spx@boxes@border@left \spx@pre@border@left
\spx@boxes@border \spx@pre@border\relax
%
\ifspx@opt@verbatimwithframe
\else
\spx@boxes@border@top\z@
\spx@boxes@border@right\z@
\spx@boxes@border@bottom\z@
\spx@boxes@border@left\z@
\spx@boxes@border\z@
\fi
%
\spx@boxes@padding@top \spx@pre@padding@top
\spx@boxes@padding@right \spx@pre@padding@right
\spx@boxes@padding@bottom\spx@pre@padding@bottom
\spx@boxes@padding@left \spx@pre@padding@left
%
\spx@boxes@radius@topleft \spx@pre@radius@topleft
\spx@boxes@radius@topright \spx@pre@radius@topright
\spx@boxes@radius@bottomright \spx@pre@radius@bottomright
\spx@boxes@radius@bottomleft \spx@pre@radius@bottomleft
%
%
\ifspx@pre@withshadow
\spx@boxes@withshadowtrue
\spx@boxes@shadow@xoffset \spx@pre@shadow@xoffset
\spx@boxes@shadow@yoffset \spx@pre@shadow@yoffset\relax
\else
\spx@boxes@withshadowfalse
\fi
\ifspx@pre@insetshadow\spx@boxes@insetshadowtrue\else
\spx@boxes@insetshadowfalse\fi
\ifspx@pre@withshadowcolor\spx@boxes@withshadowcolortrue\else
\spx@boxes@withshadowcolorfalse\fi
%
\ifspx@pre@withbackgroundcolor\spx@boxes@withbackgroundcolortrue\else
\spx@boxes@withbackgroundcolorfalse\fi
\sphinxcolorlet{spx@boxes@backgroundcolor}{VerbatimColor}%
%
\ifspx@pre@withbordercolor\spx@boxes@withbordercolortrue\else
\spx@boxes@withbordercolorfalse\fi
\sphinxcolorlet{spx@boxes@bordercolor}{VerbatimBorderColor}%
%
\ifspx@pre@withshadowcolor\spx@boxes@withshadowcolortrue\else
\spx@boxes@withshadowcolorfalse\fi
\sphinxcolorlet{spx@boxes@shadowcolor}{sphinxVerbatimShadowColor}%
% Prepares usage of \spx@boxes@fcolorbox
\spx@boxes@fcolorbox@setup
{pre}{VerbatimBorderColor}{VerbatimColor}{sphinxVerbatimShadowColor}%
\ifspx@opt@verbatimwithframe
\else
\spx@boxes@border@top\z@
\spx@boxes@border@right\z@
\spx@boxes@border@bottom\z@
\spx@boxes@border@left\z@
\spx@boxes@border\z@
\fi
}%
\def\spx@verb@boxes@fcolorbox@setuphook@openbottom{%
\spx@boxes@border@bottom \z@
Expand Down

0 comments on commit c499e09

Please sign in to comment.