diff options
| author | Georg Brandl <georg@python.org> | 2009-01-26 22:42:00 +0100 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2009-01-26 22:42:00 +0100 |
| commit | cfbfe8dc82f25bddfb6ec8a27fa78fd01c56c70a (patch) | |
| tree | a79ad31cdbfa03e3b03bb70232c71b61876df1d1 /sphinx/directives/code.py | |
| parent | 1eec03fe7471cbf63d6b5e0595b26f0c05ebb744 (diff) | |
| parent | f5e2ccd6c0728861b0eaf9794b021ca498f51896 (diff) | |
| download | sphinx-cfbfe8dc82f25bddfb6ec8a27fa78fd01c56c70a.tar.gz | |
merge with 0.5
Diffstat (limited to 'sphinx/directives/code.py')
| -rw-r--r-- | sphinx/directives/code.py | 77 |
1 files changed, 62 insertions, 15 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 8ce2eed9..d738a279 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -15,6 +15,7 @@ from docutils import nodes from docutils.parsers.rst import directives from sphinx import addnodes +from sphinx.util import parselinenos # ------ highlight directive -------------------------------------------------------- @@ -68,32 +69,78 @@ def literalinclude_directive(name, arguments, options, content, lineno, lineno - state_machine.input_offset - 1))) fn = path.normpath(path.join(source_dir, rel_fn)) + if 'pyobject' in options and 'lines' in options: + return [state.document.reporter.warning( + 'Cannot use both "pyobject" and "lines" options', line=lineno)] + encoding = options.get('encoding', env.config.source_encoding) try: f = codecs.open(fn, 'rU', encoding) - text = f.read() + lines = f.readlines() f.close() except (IOError, OSError): - retnode = state.document.reporter.warning( - 'Include file %r not found or reading it failed' % arguments[0], line=lineno) + return [state.document.reporter.warning( + 'Include file %r not found or reading it failed' % arguments[0], + line=lineno)] except UnicodeError: - retnode = state.document.reporter.warning( + return [state.document.reporter.warning( 'Encoding %r used for reading included file %r seems to ' 'be wrong, try giving an :encoding: option' % - (encoding, arguments[0])) - else: - retnode = nodes.literal_block(text, text, source=fn) - retnode.line = 1 - if options.get('language', ''): - retnode['language'] = options['language'] - if 'linenos' in options: - retnode['linenos'] = True - state.document.settings.env.note_dependency(rel_fn) + (encoding, arguments[0]))] + + objectname = options.get('pyobject') + if objectname is not None: + from sphinx.pycode import ModuleAnalyzer + analyzer = ModuleAnalyzer.for_file(fn, '') + tags = analyzer.find_tags() + if objectname not in tags: + return [state.document.reporter.warning( + 'Object named %r not found in include file %r' % + (objectname, arguments[0]), line=lineno)] + else: + lines = lines[tags[objectname][1] - 1 : tags[objectname][2] - 1] + + linespec = options.get('lines') + if linespec is not None: + try: + linelist = parselinenos(linespec, len(lines)) + except ValueError, err: + return [state.document.reporter.warning(str(err), line=lineno)] + lines = [lines[i] for i in linelist] + + startafter = options.get('start-after') + endbefore = options.get('end-before') + if startafter is not None or endbefore is not None: + use = not startafter + res = [] + for line in lines: + if not use and startafter in line: + use = True + elif use and endbefore in line: + use = False + break + elif use: + res.append(line) + lines = res + + text = ''.join(lines) + retnode = nodes.literal_block(text, text, source=fn) + retnode.line = 1 + if options.get('language', ''): + retnode['language'] = options['language'] + if 'linenos' in options: + retnode['linenos'] = True + state.document.settings.env.note_dependency(rel_fn) return [retnode] literalinclude_directive.options = {'linenos': directives.flag, - 'language': directives.unchanged, - 'encoding': directives.encoding} + 'language': directives.unchanged_required, + 'encoding': directives.encoding, + 'pyobject': directives.unchanged_required, + 'lines': directives.unchanged_required, + 'start-after': directives.unchanged_required, + 'end-before': directives.unchanged_required, + } literalinclude_directive.content = 0 literalinclude_directive.arguments = (1, 0, 0) directives.register_directive('literalinclude', literalinclude_directive) |
