summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeppe Pihl <jpihl08@gmail.com>2014-10-02 00:41:30 +0200
committerJeppe Pihl <jpihl08@gmail.com>2014-10-02 00:41:30 +0200
commit15f81d0de33e80311bfa312da4da05a5c4c7eb78 (patch)
tree84ed6afbb41a20db37befce262b66f32d7c3ee56
parent0467229ab6f1314c8e3ab4d9a551647c907d145d (diff)
downloadsphinx-15f81d0de33e80311bfa312da4da05a5c4c7eb78.tar.gz
fixed issues with line numbering not being showed correctly.
-rw-r--r--sphinx/directives/code.py56
-rw-r--r--tests/root/includes.txt2
2 files changed, 40 insertions, 18 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index f5a488fa..de724db8 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -197,6 +197,12 @@ class LiteralInclude(Directive):
'Cannot use both "lineno-match" and "lineno-start"',
line=self.lineno)]
+ if 'lineno-match' in self.options and \
+ (set(['append', 'prepend']) & set(self.options.keys())):
+ return [document.reporter.warning(
+ 'Cannot use "lineno-match" and "append" or "prepend"',
+ line=self.lineno)]
+
encoding = self.options.get('encoding', env.config.source_encoding)
codec_info = codecs.lookup(encoding)
@@ -220,7 +226,7 @@ class LiteralInclude(Directive):
self.arguments[0])
lines = list(diff)
- linenostart = self.options.get('lineno-start', None)
+ linenostart = self.options.get('lineno-start', 1)
objectname = self.options.get('pyobject')
if objectname is not None:
from sphinx.pycode import ModuleAnalyzer
@@ -231,20 +237,30 @@ class LiteralInclude(Directive):
'Object named %r not found in include file %r' %
(objectname, filename), line=self.lineno)]
else:
- linenostart = tags[objectname][1]
- lines = lines[linenostart-1: tags[objectname][2]-1]
+ lines = lines[tags[objectname][1]-1: tags[objectname][2]-1]
+ if 'lineno-match' in self.options:
+ linenostart = tags[objectname][1]
linespec = self.options.get('lines')
- if linespec is not None:
+ if linespec:
try:
linelist = parselinenos(linespec, len(lines))
- if 'lineno-match' in self.options:
- linenostart = linelist[0] + 1
except ValueError as err:
return [document.reporter.warning(str(err), line=self.lineno)]
- # just ignore nonexisting lines
- nlines = len(lines)
- lines = [lines[i] for i in linelist if i < nlines]
+
+ if 'lineno-match' in self.options:
+ # make sure the line list is not "disjoint".
+ previous = linelist[0]
+ for line_number in linelist[1:]:
+ if line_number == previous + 1:
+ previous = line_number
+ continue
+ return [document.reporter.warning(
+ 'Cannot use "lineno-match" with a disjoint set of '
+ '"lines"', line=self.lineno)]
+ linenostart = linelist[0] + 1
+ # just ignore non-existing lines
+ lines = [lines[i] for i in linelist if i < len(lines)]
if not lines:
return [document.reporter.warning(
'Line spec %r: no lines pulled from include file %r' %
@@ -261,25 +277,32 @@ class LiteralInclude(Directive):
startafter = self.options.get('start-after')
endbefore = self.options.get('end-before')
- prepend = self.options.get('prepend')
- append = self.options.get('append')
if startafter is not None or endbefore is not None:
use = not startafter
res = []
for line_number, line in enumerate(lines):
if not use and startafter and startafter in line:
if 'lineno-match' in self.options:
- linenostart = line_number + 2
+ linenostart += line_number + 2
use = True
elif use and endbefore and endbefore in line:
- use = False
break
elif use:
res.append(line)
lines = res
+ if 'lineno-match' in self.options:
+ # handle that preceding, empty lines ('\n') are removed.
+ for line in lines[linenostart-1:]:
+ if line != '\n':
+ break
+ linenostart += 1
+
+ prepend = self.options.get('prepend')
if prepend:
lines.insert(0, prepend + '\n')
+
+ append = self.options.get('append')
if append:
lines.append(append + '\n')
@@ -288,9 +311,9 @@ class LiteralInclude(Directive):
text = text.expandtabs(self.options['tab-width'])
retnode = nodes.literal_block(text, text, source=filename)
set_source_info(self, retnode)
- if diffsource is not None: # if diff is set, set udiff
+ if diffsource: # if diff is set, set udiff
retnode['language'] = 'udiff'
- if self.options.get('language', ''):
+ if 'language' in self.options:
retnode['language'] = self.options['language']
retnode['linenos'] = 'linenos' in self.options or \
'lineno-start' in self.options or \
@@ -298,8 +321,7 @@ class LiteralInclude(Directive):
extra_args = retnode['highlight_args'] = {}
if hl_lines is not None:
extra_args['hl_lines'] = hl_lines
- if linenostart is not None:
- extra_args['linenostart'] = linenostart
+ extra_args['linenostart'] = linenostart
env.note_dependency(rel_filename)
caption = self.options.get('caption')
diff --git a/tests/root/includes.txt b/tests/root/includes.txt
index 8d01e989..907b81e9 100644
--- a/tests/root/includes.txt
+++ b/tests/root/includes.txt
@@ -78,7 +78,7 @@ Literalinclude options
.. cssclass:: inc-lines-match
.. literalinclude:: literal.inc
- :lines: 6-7,9
+ :lines: 6-7,8
:lineno-match:
.. cssclass:: inc-startend-match