fixed issues with line numbering not being showed correctly.

This commit is contained in:
Jeppe Pihl 2014-10-02 00:41:30 +02:00
parent 7265fedd68
commit bbfe4d3bb9
2 changed files with 40 additions and 18 deletions

View File

@ -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')

View File

@ -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