summaryrefslogtreecommitdiff
path: root/sphinx/directives/code.py
diff options
context:
space:
mode:
authorRyan Hardin <ryan.hardin@nutanix.com>2016-08-10 10:49:00 -0400
committerRyan Hardin <ryan.hardin@nutanix.com>2016-08-10 11:15:10 -0400
commit560b5725c913c06731de3ea57d660488fba5faf6 (patch)
treecc4035d9a9319945ed0e815c891d8eebd7d18f8f /sphinx/directives/code.py
parent8a9459b3089af792c7d59b2932f656352032e717 (diff)
downloadsphinx-git-560b5725c913c06731de3ea57d660488fba5faf6.tar.gz
Added :start-at: and :end-at: parameters for literalinclude
This feature is discussed as part of issue #625.
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r--sphinx/directives/code.py36
1 files changed, 30 insertions, 6 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index f88b30987..cf45820c4 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -163,6 +163,8 @@ class LiteralInclude(Directive):
'lines': directives.unchanged_required,
'start-after': directives.unchanged_required,
'end-before': directives.unchanged_required,
+ 'start-at': directives.unchanged_required,
+ 'end-at': directives.unchanged_required,
'prepend': directives.unchanged_required,
'append': directives.unchanged_required,
'emphasize-lines': directives.unchanged_required,
@@ -213,6 +215,16 @@ class LiteralInclude(Directive):
'Cannot use "lineno-match" and "append" or "prepend"',
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)
codec_info = codecs.lookup(encoding)
@@ -285,17 +297,29 @@ class LiteralInclude(Directive):
else:
hl_lines = None
- startafter = self.options.get('start-after')
- endbefore = self.options.get('end-before')
- if startafter is not None or endbefore is not None:
- use = not startafter
+ start_str = self.options.get('start-after')
+ start_inclusive = False
+ if self.options.get('start-at') is not None:
+ 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 = []
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:
linenostart += line_number + 1
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
elif use:
res.append(line)