From 64d2f22eb47e10e5b36791a0d018bb1fb3a3d932 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 30 Mar 2015 20:34:00 +0900 Subject: [PATCH] Fix #1790 literalinclude strips empty lines at the head and tail --- CHANGES | 1 + sphinx/directives/code.py | 9 --------- sphinx/highlighting.py | 12 ++++++------ sphinx/writers/latex.py | 3 +-- tests/roots/test-directive-code/empty.inc | 3 +++ tests/roots/test-directive-code/lineno_match.rst | 3 +++ tests/test_directive_code.py | 15 +++++++++++++++ 7 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 tests/roots/test-directive-code/empty.inc diff --git a/CHANGES b/CHANGES index 2d68c67ee..c25407e31 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,7 @@ Bugs fixed * #1788: graphviz extension raises exception when caption option is present. * #1789: ``:pyobject:`` option of ``literalinclude`` directive includes following lines after class definitions +* #1790: ``literalinclude`` strips empty lines at the head and tail Release 1.3.1 (released Mar 17, 2015) diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 5043a31e4..f31ba7a99 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -302,15 +302,6 @@ class LiteralInclude(Directive): res.append(line) 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') if prepend: lines.insert(0, prepend + '\n') diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 57a841899..dd40bc308 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -35,12 +35,12 @@ from pygments.util import ClassNotFound from sphinx.pygments_styles import SphinxStyle, NoneStyle lexers = dict( - none = TextLexer(), - python = PythonLexer(), - pycon = PythonConsoleLexer(), - pycon3 = PythonConsoleLexer(python3=True), - rest = RstLexer(), - c = CLexer(), + none = TextLexer(stripnl=False), + python = PythonLexer(stripnl=False), + pycon = PythonConsoleLexer(stripnl=False), + pycon3 = PythonConsoleLexer(python3=True, stripnl=False), + rest = RstLexer(stripnl=False), + c = CLexer(stripnl=False), ) for _lexer in lexers.values(): _lexer.add_filter('raiseonerror') diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 2940e4eb1..ffa8ae874 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1542,7 +1542,7 @@ class LaTeXTranslator(nodes.NodeVisitor): # most probably a parsed-literal block -- don't highlight self.body.append('\\begin{alltt}\n') else: - code = node.astext().rstrip('\n') + code = node.astext() lang = self.hlsettingstack[-1][0] linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1 highlight_args = node.get('highlight_args', {}) @@ -1573,7 +1573,6 @@ class LaTeXTranslator(nodes.NodeVisitor): self.table.has_verbatim = True # get consistent trailer hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim} - hlcode = hlcode.rstrip() + '\n' self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' % (self.table and 'Original' or '')) raise nodes.SkipNode diff --git a/tests/roots/test-directive-code/empty.inc b/tests/roots/test-directive-code/empty.inc new file mode 100644 index 000000000..b28b04f64 --- /dev/null +++ b/tests/roots/test-directive-code/empty.inc @@ -0,0 +1,3 @@ + + + diff --git a/tests/roots/test-directive-code/lineno_match.rst b/tests/roots/test-directive-code/lineno_match.rst index 4e3b38357..1015c8df7 100644 --- a/tests/roots/test-directive-code/lineno_match.rst +++ b/tests/roots/test-directive-code/lineno_match.rst @@ -15,3 +15,6 @@ Literal Includes with Line Numbers Matching :language: python :start-after: pass :lineno-match: + +.. literalinclude:: empty.inc + :lineno-match: diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index c09f97986..d75a29454 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -158,6 +158,7 @@ def test_literal_include_lineno_match(app, status, warning): lines = ( '
'
+        '5\n'
         '6\n'
         '7\n'
         '8\n'
@@ -166,6 +167,7 @@ def test_literal_include_lineno_match(app, status, warning):
 
     start_after = (
         '
'
+        ' 8\n'
         ' 9\n'
         '10\n'
         '11\n'
@@ -175,6 +177,19 @@ def test_literal_include_lineno_match(app, status, warning):
     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')
 def test_literalinclude_caption_html(app, status, warning):
     app.builder.build('index')