Skip to content

Commit

Permalink
Distinguish code blocks (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
astonzhang authored Jul 31, 2022
1 parent cbfbdf4 commit 4c6f55d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions d2lbook/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ def look_behind(i, cond, lines):
break
j += 1
i = j
elif line.startswith('.. code::'):
# reset LaTeX code-block rendering parameters
lines[i] = '.. raw:: latex\n\n \\diilbookstyleinputcell\n\n' + lines[i]
elif line.startswith('.. parsed-literal::'):
# reset LaTeX code-block rendering parameters
lines[i] = '.. raw:: latex\n\n \\diilbookstyleoutputcell\n\n' + lines[i]
# add a output class so we can add customized css
lines[i] += '\n :class: output'
i += 1
Expand Down
39 changes: 38 additions & 1 deletion d2lbook/sphinx_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,46 @@
\fancyhead[LE,RO]{{\py@HeaderFamily }}
}
\makeatother
% Defines macros for code-blocks styling
\definecolor{d2lbookOutputCellBackgroundColor}{RGB}{239,254,255}
\definecolor{d2lbookOutputCellBorderColor}{RGB}{204,204,204}
\def\diilbookstyleoutputcell
{\sphinxcolorlet{VerbatimColor}{d2lbookOutputCellBackgroundColor}%
\sphinxcolorlet{VerbatimBorderColor}{d2lbookOutputCellBorderColor}%
\sphinxsetup{verbatimwithframe,verbatimborder=3pt}%
}%
%
\definecolor{d2lbookInputCellBackgroundColor}{rgb}{.95,.95,.95}
\def\diilbookstyleinputcell
{\sphinxcolorlet{VerbatimColor}{d2lbookInputCellBackgroundColor}%
\sphinxsetup{verbatimwithframe=false,verbatimborder=0pt}%
}%
% memo: as this mark-up uses macros not environments we have to reset all changed
% settings at each input cell to not inherit those or previous output cell
% memo: Sphinx 5.1.0, 5.1.1 ignore verbatimwithframe Boolean, so for this
% reason we added an extra verbatimborder=0pt above.
''',
'sphinxsetup': 'verbatimwithframe=false, verbatimsep=2mm, VerbatimColor={rgb}{.95,.95,.95}'
'sphinxsetup': '''verbatimsep=2mm,
VerbatimColor={rgb}{.95,.95,.95},
VerbatimBorderColor={rgb}{.95,.95,.95},
pre_border-radius=3pt,
''',
}
# memo: Sphinx 5.1.0+ has a "feature" that if we don't set VerbatimColor to
# some value via the sphinxsetup key or via \sphinxsetup raw macro, it
# considers no colouring of background is required. Above we by-passed usage
# of \sphinxsetup, because \sphinxcolorlet was more convenient. So we set
# VerbatimColor in 'sphinxsetup' global key to work around that "feature".
# The exact same applies with VerbatimBorderColor: it has to be set at least
# once via 'sphinxsetup' or via \sphinxsetup raw macro else frame is black.
#
# memo: the Sphinx 5.1.0+ added pre_border-radius must be used in 'sphinxsetup'
# (it can be modified later via extra raw \sphinxsetup)
# because at end of preamble Sphinx decides whether or not to load extra package
# for rendering boxes with rounded corners. N.B.: pre_border-radius is
# unknown in Sphinx < 5.1.0 and will cause breakage.
SPHINX_CONFIGS
Expand Down

0 comments on commit 4c6f55d

Please sign in to comment.