Support `:emphasize-lines:` in PDF output (closes #1238)

This commit is contained in:
jfbu 2017-12-10 12:16:35 +01:00
parent 90ee039e34
commit 70f8d4ddfc
9 changed files with 47 additions and 2 deletions

View File

@ -256,6 +256,11 @@ The available styling options
``VerbatimBorderColor`` ``VerbatimBorderColor``
default ``{rgb}{0,0,0}``. The frame color, defaults to black. default ``{rgb}{0,0,0}``. The frame color, defaults to black.
``VerbatimHighlightColor``
default ``{rgb}{0.878,1,1}``. The color for highlighted lines.
.. versionadded:: 1.6.6
``verbatimsep`` ``verbatimsep``
default ``\fboxsep``. The separation between code lines and the frame. default ``\fboxsep``. The separation between code lines and the frame.

View File

@ -223,6 +223,9 @@ Includes
.. versionchanged:: 1.6 .. versionchanged:: 1.6
With both ``start-after`` and ``lines`` in use, the first line as per With both ``start-after`` and ``lines`` in use, the first line as per
``start-after`` is considered to be with line number ``1`` for ``lines``. ``start-after`` is considered to be with line number ``1`` for ``lines``.
.. versionchanged:: 1.6.6
LaTeX supports the ``emphasize-lines`` option.
Caption and name Caption and name
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^

View File

@ -159,6 +159,7 @@
% For highlighted code. % For highlighted code.
\RequirePackage{fancyvrb} \RequirePackage{fancyvrb}
\fvset{fontsize=\small} \fvset{fontsize=\small}
\define@key{FV}{hllines}{\def\sphinx@verbatim@checkifhl##1{\in@{, ##1,}{#1}}}
% For hyperlinked footnotes in tables; also for gathering footnotes from % For hyperlinked footnotes in tables; also for gathering footnotes from
% topic and warning blocks. Also to allow code-blocks in footnotes. % topic and warning blocks. Also to allow code-blocks in footnotes.
\RequirePackage{footnotehyper-sphinx} \RequirePackage{footnotehyper-sphinx}
@ -299,6 +300,8 @@
\sphinxDeclareColorOption{OuterLinkColor}{{rgb}{0.216,0.439,0.388}} \sphinxDeclareColorOption{OuterLinkColor}{{rgb}{0.216,0.439,0.388}}
\sphinxDeclareColorOption{VerbatimColor}{{rgb}{1,1,1}} \sphinxDeclareColorOption{VerbatimColor}{{rgb}{1,1,1}}
\sphinxDeclareColorOption{VerbatimBorderColor}{{rgb}{0,0,0}} \sphinxDeclareColorOption{VerbatimBorderColor}{{rgb}{0,0,0}}
% also no prefix for this one, for consistency (sigh). Color as in minted!
\sphinxDeclareColorOption{VerbatimHighlightColor}{{rgb}{0.878,1,1}}
% now the colours defined with "sphinx" prefix in their names % now the colours defined with "sphinx" prefix in their names
\newcommand*{\sphinxDeclareSphinxColorOption}[2]{% \newcommand*{\sphinxDeclareSphinxColorOption}[2]{%
% set the initial default % set the initial default
@ -886,11 +889,24 @@
\expandafter\def\expandafter\FV@SetupFont\expandafter \expandafter\def\expandafter\FV@SetupFont\expandafter
{\FV@SetupFont\sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}% {\FV@SetupFont\sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}%
\sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}}% \sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}}%
\def\FancyVerbFormatLine ##1{\hsize\linewidth \def\FancyVerbFormatLine ##1{%
\expandafter\sphinx@verbatim@checkifhl
\expandafter{\the\value{FancyVerbLine}}%
\edef\sphinx@restorefboxsep{\fboxsep\the\fboxsep\relax}%
\toks@{%
\hsize\linewidth
\vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@ \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
\doublehyphendemerits\z@\finalhyphendemerits\z@ \doublehyphendemerits\z@\finalhyphendemerits\z@
\strut ##1\strut}% \strut ##1\strut}%
}% }%
\ifin@
\fboxsep0pt
\colorbox{VerbatimHighlightColor}{\sphinx@restorefboxsep\the\toks@}%
\sphinx@restorefboxsep
\else
\the\toks@
\fi
}%
\let\FV@Space\spx@verbatim@space \let\FV@Space\spx@verbatim@space
% Allow breaks at special characters using \PYG... macros. % Allow breaks at special characters using \PYG... macros.
\sphinxbreaksatspecials \sphinxbreaksatspecials

View File

@ -2270,6 +2270,8 @@ class LaTeXTranslator(nodes.NodeVisitor):
lang = self.hlsettingstack[-1][0] lang = self.hlsettingstack[-1][0]
linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1 linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1
highlight_args = node.get('highlight_args', {}) highlight_args = node.get('highlight_args', {})
hllines = '\\fvset{hllines={, %s,}}%%' %\
str(highlight_args.get('hl_lines', []))[1:-1]
if 'language' in node: if 'language' in node:
# code-block directives # code-block directives
lang = node['language'] lang = node['language']
@ -2308,6 +2310,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
hlcode += '\\end{sphinxVerbatimintable}' hlcode += '\\end{sphinxVerbatimintable}'
else: else:
hlcode += '\\end{sphinxVerbatim}' hlcode += '\\end{sphinxVerbatim}'
self.body.append('\n' + hllines)
self.body.append('\n' + hlcode + '\n') self.body.append('\n' + hlcode + '\n')
if ids: if ids:
self.body.append('\\let\\sphinxLiteralBlockLabel\\empty\n') self.body.append('\\let\\sphinxLiteralBlockLabel\\empty\n')

View File

@ -0,0 +1,7 @@
Literal Includes with Highlighted Lines
=======================================
.. literalinclude:: target.py
:language: python
:emphasize-lines: 5-6, 13-15, 24-

View File

@ -27,6 +27,7 @@ header2
\endlastfoot \endlastfoot
\fvset{hllines={, ,}}%
\begin{sphinxVerbatimintable}[commandchars=\\\{\}] \begin{sphinxVerbatimintable}[commandchars=\\\{\}]
\PYG{n}{hello} \PYG{n}{world} \PYG{n}{hello} \PYG{n}{world}
\end{sphinxVerbatimintable} \end{sphinxVerbatimintable}

View File

@ -10,6 +10,7 @@ header1
header2 header2
\unskip}\relax \\ \unskip}\relax \\
\hline \hline
\fvset{hllines={, ,}}%
\begin{sphinxVerbatimintable}[commandchars=\\\{\}] \begin{sphinxVerbatimintable}[commandchars=\\\{\}]
\PYG{n}{hello} \PYG{n}{world} \PYG{n}{hello} \PYG{n}{world}
\end{sphinxVerbatimintable} \end{sphinxVerbatimintable}

View File

@ -349,6 +349,14 @@ def test_code_block_namedlink_latex(app, status, warning):
assert link2 in latex assert link2 in latex
@pytest.mark.sphinx('latex', testroot='directive-code')
def test_code_block_emphasize_latex(app, status, warning):
app.builder.build(['emphasize'])
latex = (app.outdir / 'Python.tex').text(encoding='utf-8').replace('\r\n', '\n')
includes = '\\fvset{hllines={, 5, 6, 13, 14, 15, 24, 25, 26, 27,}}%\n'
assert includes in latex
@pytest.mark.sphinx('xml', testroot='directive-code') @pytest.mark.sphinx('xml', testroot='directive-code')
def test_literal_include(app, status, warning): def test_literal_include(app, status, warning):
app.builder.build(['index']) app.builder.build(['index'])

View File

@ -211,7 +211,8 @@ def get_verifier(verify, verify_re):
'verify', 'verify',
u'::\n\n\\∞${}', u'::\n\n\\∞${}',
None, None,
(u'\\begin{sphinxVerbatim}[commandchars=\\\\\\{\\}]\n' (u'\\fvset{hllines={, ,}}%\n'
u'\\begin{sphinxVerbatim}[commandchars=\\\\\\{\\}]\n'
u'@\\(\\Gamma\\)\\PYGZbs{}\\(\\infty\\)\\PYGZdl{}\\PYGZob{}\\PYGZcb{}\n' u'@\\(\\Gamma\\)\\PYGZbs{}\\(\\infty\\)\\PYGZdl{}\\PYGZob{}\\PYGZcb{}\n'
u'\\end{sphinxVerbatim}'), u'\\end{sphinxVerbatim}'),
), ),