Merge pull request #3340 from jfbu/wraplonglines

Fix #3308: Parsed-literals don't wrap very long lines with pdf builder
This commit is contained in:
Jean-François B 2017-01-16 00:31:20 +01:00 committed by GitHub
commit 5e0a764e6b
4 changed files with 104 additions and 19 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

@ -1,6 +1,6 @@
\NeedsTeXFormat{LaTeX2e} \NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{footnotehyper-sphinx}% \ProvidesPackage{footnotehyper-sphinx}%
[2016/10/27 v0.9f hyperref aware footnote.sty for sphinx (JFB)] [2017/01/16 v1.5.2 hyperref aware footnote.sty for sphinx (JFB)]
%% %%
%% Package: footnotehyper-sphinx %% Package: footnotehyper-sphinx
%% Version: based on footnotehyper.sty v0.9f (2016/10/03) %% Version: based on footnotehyper.sty v0.9f (2016/10/03)
@ -15,6 +15,7 @@
%% 4. \sphinxfootnotemark, and use of \spx@opt@BeforeFootnote from sphinx.sty. %% 4. \sphinxfootnotemark, and use of \spx@opt@BeforeFootnote from sphinx.sty.
%% Note: with \footnotemark[N]/\footnotetext[N] syntax, hyperref %% Note: with \footnotemark[N]/\footnotetext[N] syntax, hyperref
%% does not insert an hyperlink. This is _not_ improved here. %% does not insert an hyperlink. This is _not_ improved here.
%% 5. use of \sphinxunactivateextrasandspace for parsed literals
%% %%
\DeclareOption*{\PackageWarning{footnotehyper}{Option `\CurrentOption' is unknown}}% \DeclareOption*{\PackageWarning{footnotehyper}{Option `\CurrentOption' is unknown}}%
\ProcessOptions\relax \ProcessOptions\relax
@ -92,7 +93,8 @@
%% \spx@opt@BeforeFootnote is defined in sphinx.sty %% \spx@opt@BeforeFootnote is defined in sphinx.sty
\def\FNH@fixed@footnote {\spx@opt@BeforeFootnote\ifx\@currenvir\fn@footnote \def\FNH@fixed@footnote {\spx@opt@BeforeFootnote\ifx\@currenvir\fn@footnote
\expandafter\FNH@footnoteenv\else\expandafter\fn@latex@@footnote\fi }% \expandafter\FNH@footnoteenv\else\expandafter\fn@latex@@footnote\fi }%
\def\FNH@footnoteenv {\@ifnextchar[\FNH@xfootnoteenv%] \def\FNH@footnoteenv {\catcode13=5\sphinxunactivateextrasandspace
\@ifnextchar[\FNH@xfootnoteenv%]
{\stepcounter\@mpfn {\stepcounter\@mpfn
\protected@xdef\@thefnmark{\thempfn}\@footnotemark \protected@xdef\@thefnmark{\thempfn}\@footnotemark
\def\FNH@endfntext@next{\FNH@endfntext@link}\fn@startfntext}}% \def\FNH@endfntext@next{\FNH@endfntext@link}\fn@startfntext}}%

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/16 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
@ -397,6 +399,7 @@
\def\sphinx@verbatim@nolig@list {\do \`}% \def\sphinx@verbatim@nolig@list {\do \`}%
% Some characters . , ; ? ! / are not pygmentized. % Some characters . , ; ? ! / are not pygmentized.
% This macro makes them "active" and they will insert potential linebreaks % This macro makes them "active" and they will insert potential linebreaks
% In math mode, the characters are de-activated.
\newcommand*\sphinxbreaksbeforeactivelist {}% none \newcommand*\sphinxbreaksbeforeactivelist {}% none
\newcommand*\sphinxbreaksafteractivelist {\do\.\do\,\do\;\do\?\do\!\do\/} \newcommand*\sphinxbreaksafteractivelist {\do\.\do\,\do\;\do\?\do\!\do\/}
\newcommand*\sphinxbreaksviaactive {% \newcommand*\sphinxbreaksviaactive {%
@ -411,6 +414,15 @@
\lccode`\~`\~ \lccode`\~`\~
} }
% If the linebreak is at a space, the latter will be displayed as visible
% space at end of first line, and a continuation symbol starts next line.
% Stretch/shrink are however usually zero for typewriter font.
\def\spx@verbatim@space {%
\nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font
\discretionary{\copy\sphinxvisiblespacebox}{\sphinxafterbreak}
{\kern\fontdimen2\font}%
}%
% needed to create wrapper environments of fancyvrb's Verbatim % needed to create wrapper environments of fancyvrb's Verbatim
\newcommand*{\sphinxVerbatimEnvironment}{\gdef\FV@EnvironName{sphinxVerbatim}} \newcommand*{\sphinxVerbatimEnvironment}{\gdef\FV@EnvironName{sphinxVerbatim}}
% Sphinx <1.5 optional argument was in fact mandatory. It is now really % Sphinx <1.5 optional argument was in fact mandatory. It is now really
@ -466,14 +478,7 @@
\doublehyphendemerits\z@\finalhyphendemerits\z@ \doublehyphendemerits\z@\finalhyphendemerits\z@
\strut ##1\strut}% \strut ##1\strut}%
}% }%
% If the linebreak is at a space, the latter will be displayed as visible \let\FV@Space\spx@verbatim@space
% space at end of first line, and a continuation symbol starts next line.
% Stretch/shrink are however usually zero for typewriter font.
\def\FV@Space {%
\nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font
\discretionary{\copy\sphinxvisiblespacebox}{\sphinxafterbreak}
{\kern\fontdimen2\font}%
}%
% Allow breaks at special characters using \PYG... macros. % Allow breaks at special characters using \PYG... macros.
\sphinxbreaksatspecials \sphinxbreaksatspecials
% Breaks at punctuation characters . , ; ? ! and / (needs catcode activation) % Breaks at punctuation characters . , ; ? ! and / (needs catcode activation)
@ -535,6 +540,68 @@
\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{\let~}\spx@verbatim@space\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\sphinxunactivateextrasandspace\hyper@n@rmalise}%
\else
\PackageWarning{sphinx}{Could not patch \string\url\space command.%
^^J Not using extra active characters in alltt environment}%
\sphinxunactivateextras
\fi
}%
\newcommand*{\sphinxunactivateextras}{\let\do\@makeother
\sphinxbreaksbeforeactivelist\sphinxbreaksafteractivelist\do\-}%
\newcommand*{\sphinxunactivateextrasandspace}{\catcode32=10\relax
\sphinxunactivateextras}%
% now for the modified alltt environment
\newenvironment{sphinxalltt}
{% at start of next line to work around Emacs/AUCTeX issue with this file
\begin{alltt}%
\ifspx@opt@parsedliteralwraps
\sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}%
\sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}%
\sphinxbreaksattexescapedchars
\sphinxbreaksviaactiveinparsedliteral
\sphinxbreaksatspaceinparsedliteral
\spx@hack@hyper@normalise
% alltt takes care of the ' as derivative ("prime") in math mode
\everymath\expandafter{\the\everymath\sphinxunactivateextrasandspace
\catcode`\<=12\catcode`\>=12\catcode`\^=7\catcode`\_=8 }%
\everydisplay\expandafter{\the\everydisplay\sphinxunactivateextrasandspace
\catcode`\<=12\catcode`\>=12\catcode`\^=7\catcode`\_=8 }%
\fi }
{\end{alltt}}
% 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

@ -1030,14 +1030,20 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.body.append('%%\n\\begin{footnotetext}[%s]' self.body.append('%%\n\\begin{footnotetext}[%s]'
'\\sphinxAtStartFootnote\n' % node['number']) '\\sphinxAtStartFootnote\n' % node['number'])
else: else:
self.body.append('%%\n\\begin{footnote}[%s]' if self.in_parsed_literal:
'\\sphinxAtStartFootnote\n' % node['number']) self.body.append('\\begin{footnote}[%s]' % node['number'])
else:
self.body.append('%%\n\\begin{footnote}[%s]' % node['number'])
self.body.append('\\sphinxAtStartFootnote\n')
def depart_collected_footnote(self, node): def depart_collected_footnote(self, node):
if 'footnotetext' in node: if 'footnotetext' in node:
self.body.append('%\n\\end{footnotetext}') self.body.append('%\n\\end{footnotetext}')
else: else:
self.body.append('%\n\\end{footnote}') if self.in_parsed_literal:
self.body.append('\\end{footnote}')
else:
self.body.append('%\n\\end{footnote}')
self.in_footnote -= 1 self.in_footnote -= 1
def visit_label(self, node): def visit_label(self, node):
@ -1434,6 +1440,9 @@ class LaTeXTranslator(nodes.NodeVisitor):
attrs = node.attributes attrs = node.attributes
pre = [] # in reverse order pre = [] # in reverse order
post = [] post = []
if self.in_parsed_literal:
pre = ['\\begingroup\\sphinxunactivateextrasandspace\\relax ']
post = ['\\endgroup ']
include_graphics_options = [] include_graphics_options = []
is_inline = self.is_inline(node) is_inline = self.is_inline(node)
if 'width' in attrs: if 'width' in attrs:
@ -1929,7 +1938,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 +1998,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