mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add "object" option to literalinclude directive.
This commit is contained in:
parent
ed60ae4118
commit
ddee927c46
@ -68,20 +68,35 @@ def literalinclude_directive(name, arguments, options, content, lineno,
|
|||||||
lineno - state_machine.input_offset - 1)))
|
lineno - state_machine.input_offset - 1)))
|
||||||
fn = path.normpath(path.join(source_dir, rel_fn))
|
fn = path.normpath(path.join(source_dir, rel_fn))
|
||||||
|
|
||||||
|
fromline = toline = None
|
||||||
|
objectname = options.get('object')
|
||||||
|
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:
|
||||||
|
fromline = tags[objectname][1] - 1
|
||||||
|
toline = tags[objectname][2] - 1
|
||||||
|
|
||||||
encoding = options.get('encoding', env.config.source_encoding)
|
encoding = options.get('encoding', env.config.source_encoding)
|
||||||
try:
|
try:
|
||||||
f = codecs.open(fn, 'r', encoding)
|
f = codecs.open(fn, 'r', encoding)
|
||||||
text = f.read()
|
lines = f.readlines()
|
||||||
f.close()
|
f.close()
|
||||||
except (IOError, OSError):
|
except (IOError, OSError):
|
||||||
retnode = state.document.reporter.warning(
|
return [state.document.reporter.warning(
|
||||||
'Include file %r not found or reading it failed' % arguments[0], line=lineno)
|
'Include file %r not found or reading it failed' % arguments[0],
|
||||||
|
line=lineno)]
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
retnode = state.document.reporter.warning(
|
return [state.document.reporter.warning(
|
||||||
'Encoding %r used for reading included file %r seems to '
|
'Encoding %r used for reading included file %r seems to '
|
||||||
'be wrong, try giving an :encoding: option' %
|
'be wrong, try giving an :encoding: option' %
|
||||||
(encoding, arguments[0]))
|
(encoding, arguments[0]))]
|
||||||
else:
|
text = ''.join(lines[fromline:toline])
|
||||||
retnode = nodes.literal_block(text, text, source=fn)
|
retnode = nodes.literal_block(text, text, source=fn)
|
||||||
retnode.line = 1
|
retnode.line = 1
|
||||||
if options.get('language', ''):
|
if options.get('language', ''):
|
||||||
@ -92,8 +107,13 @@ def literalinclude_directive(name, arguments, options, content, lineno,
|
|||||||
return [retnode]
|
return [retnode]
|
||||||
|
|
||||||
literalinclude_directive.options = {'linenos': directives.flag,
|
literalinclude_directive.options = {'linenos': directives.flag,
|
||||||
'language': directives.unchanged,
|
'language': directives.unchanged_required,
|
||||||
'encoding': directives.encoding}
|
'encoding': directives.encoding,
|
||||||
|
'object': directives.unchanged_required,
|
||||||
|
#'lines': directives.unchanged_required,
|
||||||
|
#'start-after': directives.unchanged_required,
|
||||||
|
#'end-before': directives.unchanged_required,
|
||||||
|
}
|
||||||
literalinclude_directive.content = 0
|
literalinclude_directive.content = 0
|
||||||
literalinclude_directive.arguments = (1, 0, 0)
|
literalinclude_directive.arguments = (1, 0, 0)
|
||||||
directives.register_directive('literalinclude', literalinclude_directive)
|
directives.register_directive('literalinclude', literalinclude_directive)
|
||||||
|
@ -220,7 +220,7 @@ class ModuleAnalyzer(object):
|
|||||||
dtype, fullname, startline, _ = stack.pop()
|
dtype, fullname, startline, _ = stack.pop()
|
||||||
endline = epos[0]
|
endline = epos[0]
|
||||||
namespace.pop()
|
namespace.pop()
|
||||||
result[dtype, fullname] = (startline, endline)
|
result[fullname] = (dtype, startline, endline)
|
||||||
expect_indent = False
|
expect_indent = False
|
||||||
if tok in ('def', 'class'):
|
if tok in ('def', 'class'):
|
||||||
name = tokeniter.next()[1]
|
name = tokeniter.next()[1]
|
||||||
@ -239,7 +239,7 @@ class ModuleAnalyzer(object):
|
|||||||
dtype, fullname, startline, _ = stack.pop()
|
dtype, fullname, startline, _ = stack.pop()
|
||||||
endline = spos[0]
|
endline = spos[0]
|
||||||
namespace.pop()
|
namespace.pop()
|
||||||
result[dtype, fullname] = (startline, endline)
|
result[fullname] = (dtype, startline, endline)
|
||||||
elif type == token.NEWLINE:
|
elif type == token.NEWLINE:
|
||||||
# if this line contained a definition, expect an INDENT to start the
|
# if this line contained a definition, expect an INDENT to start the
|
||||||
# suite; if there is no such INDENT it's a one-line definition
|
# suite; if there is no such INDENT it's a one-line definition
|
||||||
|
Loading…
Reference in New Issue
Block a user