Fix #1790 literalinclude strips empty lines at the head and tail

This commit is contained in:
Takeshi KOMIYA 2015-03-30 20:34:00 +09:00
parent aa66e7860b
commit 64d2f22eb4
7 changed files with 29 additions and 17 deletions

View File

@ -7,6 +7,7 @@ Bugs fixed
* #1788: graphviz extension raises exception when caption option is present. * #1788: graphviz extension raises exception when caption option is present.
* #1789: ``:pyobject:`` option of ``literalinclude`` directive includes following * #1789: ``:pyobject:`` option of ``literalinclude`` directive includes following
lines after class definitions lines after class definitions
* #1790: ``literalinclude`` strips empty lines at the head and tail
Release 1.3.1 (released Mar 17, 2015) Release 1.3.1 (released Mar 17, 2015)

View File

@ -302,15 +302,6 @@ class LiteralInclude(Directive):
res.append(line) res.append(line)
lines = res lines = res
if 'lineno-match' in self.options:
# handle that docutils remove preceding lines which only contains
# line separation.
for line in lines:
# check if line contains anything else than line separation.
if line and line.splitlines()[0]:
break
linenostart += 1
prepend = self.options.get('prepend') prepend = self.options.get('prepend')
if prepend: if prepend:
lines.insert(0, prepend + '\n') lines.insert(0, prepend + '\n')

View File

@ -35,12 +35,12 @@ from pygments.util import ClassNotFound
from sphinx.pygments_styles import SphinxStyle, NoneStyle from sphinx.pygments_styles import SphinxStyle, NoneStyle
lexers = dict( lexers = dict(
none = TextLexer(), none = TextLexer(stripnl=False),
python = PythonLexer(), python = PythonLexer(stripnl=False),
pycon = PythonConsoleLexer(), pycon = PythonConsoleLexer(stripnl=False),
pycon3 = PythonConsoleLexer(python3=True), pycon3 = PythonConsoleLexer(python3=True, stripnl=False),
rest = RstLexer(), rest = RstLexer(stripnl=False),
c = CLexer(), c = CLexer(stripnl=False),
) )
for _lexer in lexers.values(): for _lexer in lexers.values():
_lexer.add_filter('raiseonerror') _lexer.add_filter('raiseonerror')

View File

@ -1542,7 +1542,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
# most probably a parsed-literal block -- don't highlight # most probably a parsed-literal block -- don't highlight
self.body.append('\\begin{alltt}\n') self.body.append('\\begin{alltt}\n')
else: else:
code = node.astext().rstrip('\n') code = node.astext()
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', {})
@ -1573,7 +1573,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.table.has_verbatim = True self.table.has_verbatim = True
# get consistent trailer # get consistent trailer
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim} hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
hlcode = hlcode.rstrip() + '\n'
self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' % self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' %
(self.table and 'Original' or '')) (self.table and 'Original' or ''))
raise nodes.SkipNode raise nodes.SkipNode

View File

@ -0,0 +1,3 @@

View File

@ -15,3 +15,6 @@ Literal Includes with Line Numbers Matching
:language: python :language: python
:start-after: pass :start-after: pass
:lineno-match: :lineno-match:
.. literalinclude:: empty.inc
:lineno-match:

View File

@ -158,6 +158,7 @@ def test_literal_include_lineno_match(app, status, warning):
lines = ( lines = (
'<td class="linenos"><div class="linenodiv"><pre>' '<td class="linenos"><div class="linenodiv"><pre>'
'5\n'
'6\n' '6\n'
'7\n' '7\n'
'8\n' '8\n'
@ -166,6 +167,7 @@ def test_literal_include_lineno_match(app, status, warning):
start_after = ( start_after = (
'<td class="linenos"><div class="linenodiv"><pre>' '<td class="linenos"><div class="linenodiv"><pre>'
' 8\n'
' 9\n' ' 9\n'
'10\n' '10\n'
'11\n' '11\n'
@ -175,6 +177,19 @@ def test_literal_include_lineno_match(app, status, warning):
assert start_after in html assert start_after in html
@with_app('latex', testroot='directive-code')
def test_literalinclude_file_whole_of_emptyline(app, status, warning):
app.builder.build_all()
latex = (app.outdir / 'Python.tex').text()
includes = (
'\\begin{Verbatim}[commandchars=\\\\\\{\\},numbers=left,firstnumber=1,stepnumber=1]\n'
'\n'
'\n'
'\n'
'\\end{Verbatim}\n')
assert includes in latex
@with_app('html', testroot='directive-code') @with_app('html', testroot='directive-code')
def test_literalinclude_caption_html(app, status, warning): def test_literalinclude_caption_html(app, status, warning):
app.builder.build('index') app.builder.build('index')