Fix #3308: Parsed-literals don't wrap very long lines with pdf builder

This commit is contained in:
jfbu 2017-01-14 12:32:20 +01:00
parent c844404741
commit 8c21abeccc
3 changed files with 75 additions and 6 deletions

View File

@ -143,14 +143,21 @@ Here are the currently available options together with their default values.
LaTeX requires ``true`` or ``false`` to be specified in *lowercase*. LaTeX requires ``true`` or ``false`` to be specified in *lowercase*.
``verbatimwrapslines`` ``verbatimwrapslines``
default ``true``. Tells whether long lines in :rst:dir:`code-block`\ s default ``true``. Tells whether long lines in :rst:dir:`code-block`\ 's
should be wrapped. contents should wrap.
.. (comment) It is theoretically possible to customize this even .. (comment) It is theoretically possible to customize this even
more and decide at which characters a line-break can occur and whether more and decide at which characters a line-break can occur and whether
before or after, but this is accessible currently only by re-defining some before or after, but this is accessible currently only by re-defining some
macros with complicated LaTeX syntax from :file:`sphinx.sty`. macros with complicated LaTeX syntax from :file:`sphinx.sty`.
``parsedliteralwraps``
default ``true``. Tells whether long lines in :dudir:`parsed-literal`\ 's
contents should wrap.
.. versionadded:: 1.5.2
set this option value to ``false`` to recover former behaviour.
``inlineliteralwraps`` ``inlineliteralwraps``
default ``true``. Allows linebreaks inside inline literals: but extra default ``true``. Allows linebreaks inside inline literals: but extra
potential break-points (additionally to those allowed by LaTeX at spaces potential break-points (additionally to those allowed by LaTeX at spaces
@ -160,7 +167,7 @@ Here are the currently available options together with their default values.
(or shrinked) in order to accomodate the linebreak. (or shrinked) in order to accomodate the linebreak.
.. versionadded:: 1.5 .. versionadded:: 1.5
set this option to ``false`` to recover former behaviour. set this option value to ``false`` to recover former behaviour.
``verbatimvisiblespace`` ``verbatimvisiblespace``
default ``\textcolor{red}{\textvisiblespace}``. When a long code line is default ``\textcolor{red}{\textvisiblespace}``. When a long code line is

View File

@ -6,7 +6,7 @@
% %
\NeedsTeXFormat{LaTeX2e}[1995/12/01] \NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{sphinx}[2017/01/09 v1.5.2 LaTeX package (Sphinx markup)] \ProvidesPackage{sphinx}[2017/01/14 v1.5.2 LaTeX package (Sphinx markup)]
% we delay handling of options to after having loaded packages, because % we delay handling of options to after having loaded packages, because
% of the need to use \definecolor. % of the need to use \definecolor.
@ -91,6 +91,8 @@
\DeclareBoolOption[true]{verbatimwithframe} \DeclareBoolOption[true]{verbatimwithframe}
\DeclareBoolOption[true]{verbatimwrapslines} \DeclareBoolOption[true]{verbatimwrapslines}
\DeclareBoolOption[true]{inlineliteralwraps} \DeclareBoolOption[true]{inlineliteralwraps}
% parsed literal
\DeclareBoolOption[true]{parsedliteralwraps}
% \textvisiblespace for compatibility with fontspec+XeTeX/LuaTeX % \textvisiblespace for compatibility with fontspec+XeTeX/LuaTeX
\DeclareStringOption[\textcolor{red}{\textvisiblespace}]{verbatimvisiblespace} \DeclareStringOption[\textcolor{red}{\textvisiblespace}]{verbatimvisiblespace}
\DeclareStringOption % must use braces to hide the brackets \DeclareStringOption % must use braces to hide the brackets
@ -535,6 +537,66 @@
\begin{sphinxVerbatim}} \begin{sphinxVerbatim}}
{\end{sphinxVerbatim}} {\end{sphinxVerbatim}}
% Parsed literal: allow long lines to wrap like they do in code-blocks
% this should be kept in sync with definitions in sphinx.util.texescape
\newcommand*\sphinxbreaksattexescapedchars{%
\def\do##1##2% put potential break point before character
{\def##1{\discretionary{}{\sphinxafterbreak\char`##2}{\char`##2}}}%
\do\{\{\do\textless\<\do\#\#\do\%\%\do\$\$% {, <, #, %, $
\def\do##1##2% put potential break point after character
{\def##1{\discretionary{\char`##2}{\sphinxafterbreak}{\char`##2}}}%
\do\_\_\do\}\}\do\textasciicircum\^\do\&\&% _, }, ^, &,
\do\textgreater\>\do\textasciitilde\~% >, ~
}
\newcommand*\sphinxbreaksviaactiveinparsedliteral{%
\sphinxbreaksviaactive % by default handles . , ; ? ! /
\do\-% we need also the hyphen character
\lccode`\~`\~ %
% update \dospecials as it is used by \url
% but deactivation will already have been done hence commented out
% \expandafter\def\expandafter\dospecials\expandafter{\dospecials
% \sphinxbreaksbeforeactivelist\sphinxbreaksafteractivelist\do\-}%
}
\newcommand*\sphinxbreaksatspaceinparsedliteral{%
\lccode`~32 \lowercase{\def~}{\leavevmode
\nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font
\discretionary{\copy\sphinxvisiblespacebox}{\sphinxafterbreak}
{\kern\fontdimen2\font}%
}\lccode`\~`\~
}
% now the hack for \url to work (hyperref is required dependency):
% the aim it to deactivate - . , ; ? ! / in \url's argument
\def\spx@hack@hyper@normalise {%
\expandafter\spx@hack@hyper@normalise@aux\hyper@normalise
\spx@hack@hyper@normalise@aux\hyper@n@rmalise\relax\spx@undefined
}%
\long\def\spx@hack@hyper@normalise@aux#1\hyper@n@rmalise#2#3\spx@undefined{%
\ifx\spx@hack@hyper@normalise@aux#2%
\def\hyper@normalise
{#1\let\do\@makeother
\sphinxbreaksbeforeactivelist\sphinxbreaksafteractivelist\do\-%
\hyper@n@rmalise}%
\else
\PackageWarning{sphinx}{Could not patch \string\url\space command.%
^^J Not using active characters in alltt environment}%
\let\do\@makeother
\sphinxbreaksbeforeactivelist\sphinxbreaksafteractivelist\do\-%
\fi
}%
% now for the modified alltt environment
\newenvironment{sphinxalltt}
{\begin{alltt}%
\ifspx@opt@parsedliteralwraps
\sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}%
\sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}%
\sphinxbreaksattexescapedchars
\sphinxbreaksviaactiveinparsedliteral
\sphinxbreaksatspaceinparsedliteral
\spx@hack@hyper@normalise
\fi }
{\end{alltt}}% \end{alltt} (<-- only to fix Emacs/AUCTeX fontification issue)
% Topic boxes % Topic boxes
% Again based on use of "framed.sty", this allows breakable framed boxes. % Again based on use of "framed.sty", this allows breakable framed boxes.

View File

@ -1929,7 +1929,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
if node.rawsource != node.astext(): if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight # most probably a parsed-literal block -- don't highlight
self.in_parsed_literal += 1 self.in_parsed_literal += 1
self.body.append('\\begin{alltt}\n') self.body.append('\\begin{sphinxalltt}\n')
else: else:
ids = '' ids = ''
for id in self.pop_hyperlink_ids('code-block'): for id in self.pop_hyperlink_ids('code-block'):
@ -1989,7 +1989,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
raise nodes.SkipNode raise nodes.SkipNode
def depart_literal_block(self, node): def depart_literal_block(self, node):
self.body.append('\n\\end{alltt}\n') self.body.append('\n\\end{sphinxalltt}\n')
self.in_parsed_literal -= 1 self.in_parsed_literal -= 1
visit_doctest_block = visit_literal_block visit_doctest_block = visit_literal_block
depart_doctest_block = depart_literal_block depart_doctest_block = depart_literal_block