summaryrefslogtreecommitdiff
path: root/sphinx/directives/code.py
diff options
context:
space:
mode:
authorJean-François B <jfbu@free.fr>2017-02-17 09:00:40 +0100
committerGitHub <noreply@github.com>2017-02-17 09:00:40 +0100
commit32817f9efac39b259e57c4bbdaa5395d5604b48e (patch)
treeae7afc7487af1b7e30eb68ca685f034ddcfc9fde /sphinx/directives/code.py
parent24fd651bbb701f53fe3ea4b3000dc6cc4b842895 (diff)
parenta7becf30ded7472c4ef8fbce632d4de46c416e66 (diff)
downloadsphinx-git-32817f9efac39b259e57c4bbdaa5395d5604b48e.tar.gz
Merge pull request #3413 from jfbu/literalincludelines
If ``:start-after:`` is used, make ``:lines:`` relative (close: #3412)
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r--sphinx/directives/code.py60
1 files changed, 32 insertions, 28 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index d66f40a77..2a263220f 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -267,6 +267,8 @@ class LiteralInclude(Directive):
lines = list(diff)
linenostart = self.options.get('lineno-start', 1)
+ if 'lineno-match' in self.options:
+ linenostart = 1
objectname = self.options.get('pyobject')
if objectname is not None:
from sphinx.pycode import ModuleAnalyzer
@@ -281,31 +283,6 @@ class LiteralInclude(Directive):
if 'lineno-match' in self.options:
linenostart = tags[objectname][1]
- linespec = self.options.get('lines')
- if linespec:
- try:
- linelist = parselinenos(linespec, len(lines))
- except ValueError as err:
- return [document.reporter.warning(str(err), line=self.lineno)]
-
- 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' %
- (linespec, filename), line=self.lineno)]
-
linespec = self.options.get('emphasize-lines')
if linespec:
try:
@@ -330,11 +307,13 @@ class LiteralInclude(Directive):
res = []
for line_number, line in enumerate(lines):
if not use and start_str and start_str in line:
- if 'lineno-match' in self.options:
- linenostart += line_number + 1
- use = True
if start_inclusive:
res.append(line)
+ if 'lineno-match' in self.options:
+ linenostart = line_number + 1
+ if not start_inclusive:
+ linenostart += 1
+ use = True
elif use and end_str and end_str in line:
if end_inclusive:
res.append(line)
@@ -343,6 +322,31 @@ class LiteralInclude(Directive):
res.append(line)
lines = res
+ linespec = self.options.get('lines')
+ if linespec:
+ try:
+ linelist = parselinenos(linespec, len(lines))
+ except ValueError as err:
+ return [document.reporter.warning(str(err), line=self.lineno)]
+
+ 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]
+ # 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' %
+ (linespec, filename), line=self.lineno)]
+
prepend = self.options.get('prepend')
if prepend:
lines.insert(0, prepend + '\n')