diff options
author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2011-09-17 21:03:28 +0000 |
---|---|---|
committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2011-09-17 21:03:28 +0000 |
commit | 8d4f176f509e1aaf3aa6f37a632f688ed77fe7b9 (patch) | |
tree | 7a4829cd346eed960d52875592b4f03d02d2734d /docutils/parsers/rst | |
parent | 27b584a0d24cd60e597d5d2ade889ec75b858356 (diff) | |
download | docutils-8d4f176f509e1aaf3aa6f37a632f688ed77fe7b9.tar.gz |
"code" option of the "include" directive, tests
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7129 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/parsers/rst')
-rw-r--r-- | docutils/parsers/rst/directives/body.py | 6 | ||||
-rw-r--r-- | docutils/parsers/rst/directives/misc.py | 55 | ||||
-rw-r--r-- | docutils/parsers/rst/roles.py | 2 |
3 files changed, 52 insertions, 11 deletions
diff --git a/docutils/parsers/rst/directives/body.py b/docutils/parsers/rst/directives/body.py index c3ee1e172..8f6d4360c 100644 --- a/docutils/parsers/rst/directives/body.py +++ b/docutils/parsers/rst/directives/body.py @@ -145,7 +145,7 @@ class CodeBlock(Directive): # set up lexical analyzer try: - tokens = Lexer(self.content, language, + tokens = Lexer(u'\n'.join(self.content), language, self.state.document.settings.syntax_highlight) except LexerError, error: raise self.warning(error) @@ -162,7 +162,9 @@ class CodeBlock(Directive): node = nodes.literal_block('\n'.join(self.content), classes=classes) self.add_name(node) - + # if called from "include", set the source + if 'source' in self.options: + node.attributes['source'] = self.options['source'] # analyze content and add nodes for every token for classes, value in tokens: # print (classes, value) diff --git a/docutils/parsers/rst/directives/misc.py b/docutils/parsers/rst/directives/misc.py index b68446bb4..8641b24e8 100644 --- a/docutils/parsers/rst/directives/misc.py +++ b/docutils/parsers/rst/directives/misc.py @@ -14,6 +14,8 @@ from docutils import io, nodes, statemachine, utils from docutils.error_reporting import SafeString, ErrorString from docutils.parsers.rst import Directive, convert_directive_function from docutils.parsers.rst import directives, roles, states +from docutils.parsers.rst.directives.body import CodeBlock, NumberLines +from docutils.parsers.rst.roles import set_classes from docutils.transforms import misc class Include(Directive): @@ -32,18 +34,23 @@ class Include(Directive): optional_arguments = 0 final_argument_whitespace = True option_spec = {'literal': directives.flag, + 'code': directives.unchanged, 'encoding': directives.encoding, 'tab-width': int, 'start-line': int, 'end-line': int, 'start-after': directives.unchanged_required, - 'end-before': directives.unchanged_required} + 'end-before': directives.unchanged_required, + # ignored except for 'literal' or 'code': + 'number-lines': directives.unchanged, # integer or None + 'class': directives.class_option, + 'name': directives.unchanged} standard_include_path = os.path.join(os.path.dirname(states.__file__), 'include') def run(self): - """Include a reST file as part of the content of this reST file.""" + """Include a file as part of the content of this reST file.""" if not self.state.document.settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) source = self.state_machine.input_lines.source( @@ -98,20 +105,52 @@ class Include(Directive): raise self.severe('Problem with "end-before" option of "%s" ' 'directive:\nText not found.' % self.name) rawtext = rawtext[:before_index] + + include_lines = statemachine.string2lines(rawtext, tab_width, + convert_whitespace=1) if 'literal' in self.options: # Convert tabs to spaces, if `tab_width` is positive. if tab_width >= 0: text = rawtext.expandtabs(tab_width) else: text = rawtext - literal_block = nodes.literal_block(rawtext, text, source=path) + literal_block = nodes.literal_block(rawtext, source=path, + classes=self.options.get('class', [])) literal_block.line = 1 + self.add_name(literal_block) + if 'number-lines' in self.options: + try: + startline = int(self.options['number-lines'] or 1) + except ValueError: + raise self.error(':number-lines: with non-integer ' + 'start value') + endline = startline + len(include_lines) + if text.endswith('\n'): + text = text[:-1] + tokens = NumberLines([([], text)], startline, endline) + for classes, value in tokens: + if classes: + literal_block += nodes.inline(value, value, + classes=classes) + else: + literal_block += nodes.Text(value, value) + else: + literal_block += nodes.Text(text, text) return [literal_block] - else: - include_lines = statemachine.string2lines( - rawtext, tab_width, convert_whitespace=1) - self.state_machine.insert_input(include_lines, path) - return [] + if 'code' in self.options: + self.options['source'] = path + codeblock = CodeBlock(self.name, + [self.options.pop('code')], # arguments + self.options, + include_lines, # content + self.lineno, + self.content_offset, + self.block_text, + self.state, + self.state_machine) + return codeblock.run() + self.state_machine.insert_input(include_lines, path) + return [] class Raw(Directive): diff --git a/docutils/parsers/rst/roles.py b/docutils/parsers/rst/roles.py index 0b7fc9467..1a8b0bf75 100644 --- a/docutils/parsers/rst/roles.py +++ b/docutils/parsers/rst/roles.py @@ -325,7 +325,7 @@ def code_role(role, rawtext, text, lineno, inliner, options={}, content=[]): classes.extend(options['classes']) try: - tokens = Lexer([utils.unescape(text, 1)], language, + tokens = Lexer(utils.unescape(text, 1), language, inliner.document.settings.syntax_highlight) except LexerError, error: msg = inliner.reporter.warning(error) |