mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #2843 from rthardin/issue_625_start_at_end_at
Add :start-at: and :end-at: parameters for literalinclude
This commit is contained in:
commit
111b7f4c72
@ -170,6 +170,8 @@ class LiteralInclude(Directive):
|
|||||||
'lines': directives.unchanged_required,
|
'lines': directives.unchanged_required,
|
||||||
'start-after': directives.unchanged_required,
|
'start-after': directives.unchanged_required,
|
||||||
'end-before': directives.unchanged_required,
|
'end-before': directives.unchanged_required,
|
||||||
|
'start-at': directives.unchanged_required,
|
||||||
|
'end-at': directives.unchanged_required,
|
||||||
'prepend': directives.unchanged_required,
|
'prepend': directives.unchanged_required,
|
||||||
'append': directives.unchanged_required,
|
'append': directives.unchanged_required,
|
||||||
'emphasize-lines': directives.unchanged_required,
|
'emphasize-lines': directives.unchanged_required,
|
||||||
@ -220,6 +222,16 @@ class LiteralInclude(Directive):
|
|||||||
'Cannot use "lineno-match" and "append" or "prepend"',
|
'Cannot use "lineno-match" and "append" or "prepend"',
|
||||||
line=self.lineno)]
|
line=self.lineno)]
|
||||||
|
|
||||||
|
if 'start-after' in self.options and 'start-at' in self.options:
|
||||||
|
return [document.reporter.warning(
|
||||||
|
'Cannot use both "start-after" and "start-at" options',
|
||||||
|
line=self.lineno)]
|
||||||
|
|
||||||
|
if 'end-before' in self.options and 'end-at' in self.options:
|
||||||
|
return [document.reporter.warning(
|
||||||
|
'Cannot use both "end-before" and "end-at" options',
|
||||||
|
line=self.lineno)]
|
||||||
|
|
||||||
encoding = self.options.get('encoding', env.config.source_encoding)
|
encoding = self.options.get('encoding', env.config.source_encoding)
|
||||||
codec_info = codecs.lookup(encoding)
|
codec_info = codecs.lookup(encoding)
|
||||||
|
|
||||||
@ -292,17 +304,29 @@ class LiteralInclude(Directive):
|
|||||||
else:
|
else:
|
||||||
hl_lines = None
|
hl_lines = None
|
||||||
|
|
||||||
startafter = self.options.get('start-after')
|
start_str = self.options.get('start-after')
|
||||||
endbefore = self.options.get('end-before')
|
start_inclusive = False
|
||||||
if startafter is not None or endbefore is not None:
|
if self.options.get('start-at') is not None:
|
||||||
use = not startafter
|
start_str = self.options.get('start-at')
|
||||||
|
start_inclusive = True
|
||||||
|
end_str = self.options.get('end-before')
|
||||||
|
end_inclusive = False
|
||||||
|
if self.options.get('end-at') is not None:
|
||||||
|
end_str = self.options.get('end-at')
|
||||||
|
end_inclusive = True
|
||||||
|
if start_str is not None or end_str is not None:
|
||||||
|
use = not start_str
|
||||||
res = []
|
res = []
|
||||||
for line_number, line in enumerate(lines):
|
for line_number, line in enumerate(lines):
|
||||||
if not use and startafter and startafter in line:
|
if not use and start_str and start_str in line:
|
||||||
if 'lineno-match' in self.options:
|
if 'lineno-match' in self.options:
|
||||||
linenostart += line_number + 1
|
linenostart += line_number + 1
|
||||||
use = True
|
use = True
|
||||||
elif use and endbefore and endbefore in line:
|
if start_inclusive:
|
||||||
|
res.append(line)
|
||||||
|
elif use and end_str and end_str in line:
|
||||||
|
if end_inclusive:
|
||||||
|
res.append(line)
|
||||||
break
|
break
|
||||||
elif use:
|
elif use:
|
||||||
res.append(line)
|
res.append(line)
|
||||||
|
@ -16,5 +16,11 @@ Literal Includes with Line Numbers Matching
|
|||||||
:start-after: pass
|
:start-after: pass
|
||||||
:lineno-match:
|
:lineno-match:
|
||||||
|
|
||||||
|
.. literalinclude:: literal.inc
|
||||||
|
:language: python
|
||||||
|
:start-at: class Bar:
|
||||||
|
:end-at: pass
|
||||||
|
:lineno-match:
|
||||||
|
|
||||||
.. literalinclude:: empty.inc
|
.. literalinclude:: empty.inc
|
||||||
:lineno-match:
|
:lineno-match:
|
||||||
|
@ -222,6 +222,13 @@ def test_literal_include_lineno_match(app, status, warning):
|
|||||||
'14</pre></div></td>')
|
'14</pre></div></td>')
|
||||||
assert start_after in html
|
assert start_after in html
|
||||||
|
|
||||||
|
start_at_end_at = (
|
||||||
|
'<td class="linenos"><div class="linenodiv"><pre>'
|
||||||
|
' 9\n'
|
||||||
|
'10\n'
|
||||||
|
'11</pre></div></td>')
|
||||||
|
assert start_at_end_at in html
|
||||||
|
|
||||||
|
|
||||||
@with_app('latex', testroot='directive-code')
|
@with_app('latex', testroot='directive-code')
|
||||||
def test_literalinclude_file_whole_of_emptyline(app, status, warning):
|
def test_literalinclude_file_whole_of_emptyline(app, status, warning):
|
||||||
|
Loading…
Reference in New Issue
Block a user