diff options
author | goodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2002-10-02 03:17:43 +0000 |
---|---|---|
committer | goodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2002-10-02 03:17:43 +0000 |
commit | 7b12a12a016617200529e8b611ad2f2a866ad7f9 (patch) | |
tree | 41b64216101ef6cb86a15d1c9fd798d474fabdd9 /docutils/parsers/rst/directives/images.py | |
parent | a93ae74d425bb3a12c671949a57a92cc83ec6dce (diff) | |
download | docutils-7b12a12a016617200529e8b611ad2f2a866ad7f9.tar.gz |
Updated all directive functions to new API, including better reporting.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@744 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/parsers/rst/directives/images.py')
-rw-r--r-- | docutils/parsers/rst/directives/images.py | 121 |
1 files changed, 44 insertions, 77 deletions
diff --git a/docutils/parsers/rst/directives/images.py b/docutils/parsers/rst/directives/images.py index bb997bc58..1617947ba 100644 --- a/docutils/parsers/rst/directives/images.py +++ b/docutils/parsers/rst/directives/images.py @@ -1,12 +1,10 @@ -#! /usr/bin/env python +# Author: David Goodger +# Contact: goodger@users.sourceforge.net +# Revision: $Revision$ +# Date: $Date$ +# Copyright: This module has been placed in the public domain. """ -:Author: David Goodger -:Contact: goodger@users.sourceforge.net -:Revision: $Revision$ -:Date: $Date$ -:Copyright: This module has been placed in the public domain. - Directives for figures and simple images. """ @@ -23,81 +21,50 @@ align_values = ('top', 'middle', 'bottom', 'left', 'center', 'right') def align(argument): return directives.choice(argument, align_values) -image_option_spec = {'alt': directives.unchanged, - 'height': int, - 'width': int, - 'scale': int, - 'align': align} - -def image(match, type_name, data, state, state_machine, option_presets): - lineno = state_machine.abs_line_number() - line_offset = state_machine.line_offset - datablock, indent, offset, blank_finish = \ - state_machine.get_first_known_indented(match.end(), until_blank=1) - blocktext = '\n'.join(state_machine.input_lines[ - line_offset : line_offset + len(datablock) + 1]) - for i in range(len(datablock)): - if datablock[i][:1] == ':': - attlines = datablock[i:] - datablock = datablock[:i] - break - else: - attlines = [] - if not datablock: - error = state_machine.reporter.error( - 'Missing image URI argument.', '', - nodes.literal_block(blocktext, blocktext), line=lineno) - return [error], blank_finish - attoffset = line_offset + i - reference = ''.join([line.strip() for line in datablock]) +def image(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + reference = ''.join(arguments[0].split('\n')) if reference.find(' ') != -1: error = state_machine.reporter.error( 'Image URI contains whitespace.', '', - nodes.literal_block(blocktext, blocktext), line=lineno) - return [error], blank_finish - if attlines: - success, data, blank_finish = state.parse_extension_options( - image_option_spec, attlines, blank_finish) - if success: # data is a dict of options - option_presets.update(data) - else: # data is an error string - error = state_machine.reporter.error( - 'Error in "%s" directive options:\n%s.' - % (match.group(1), data), '', - nodes.literal_block(blocktext, blocktext), line=lineno) - return [error], blank_finish - option_presets['uri'] = reference - imagenode = nodes.image(blocktext, **option_presets) - return [imagenode], blank_finish + nodes.literal_block(block_text, block_text), line=lineno) + return [error] + options['uri'] = reference + image_node = nodes.image(block_text, **options) + return [image_node] + +image.arguments = (1, 0, 1) +image.options = {'alt': directives.unchanged, + 'height': directives.nonnegative_int, + 'width': directives.nonnegative_int, + 'scale': directives.nonnegative_int, + 'align': align} -def figure(match, type_name, data, state, state_machine, option_presets): - lineno = state_machine.abs_line_number() - line_offset = state_machine.line_offset - (imagenode,), blank_finish = image(match, type_name, data, state, - state_machine, option_presets) - indented, indent, offset, blank_finish \ - = state_machine.get_first_known_indented(sys.maxint) - blocktext = '\n'.join(state_machine.input_lines[ - line_offset : state_machine.line_offset + 1]) - if isinstance(imagenode, nodes.system_message): - if indented: - imagenode[-1] = nodes.literal_block(blocktext, blocktext) - return [imagenode], blank_finish - figurenode = nodes.figure('', imagenode) - if indented: +def figure(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + (image_node,) = image(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine) + if isinstance(image_node, nodes.system_message): + return [image_node] + figure_node = nodes.figure('', image_node) + if content: node = nodes.Element() # anonymous container for parsing - state.nested_parse(indented, line_offset, node) - firstnode = node[0] - if isinstance(firstnode, nodes.paragraph): - caption = nodes.caption(firstnode.rawsource, '', - *firstnode.children) - figurenode += caption - elif not (isinstance(firstnode, nodes.comment) - and len(firstnode) == 0): + state.nested_parse(content, content_offset, node) + first_node = node[0] + if isinstance(first_node, nodes.paragraph): + caption = nodes.caption(first_node.rawsource, '', + *first_node.children) + figure_node += caption + elif not (isinstance(first_node, nodes.comment) + and len(first_node) == 0): error = state_machine.reporter.error( 'Figure caption must be a paragraph or empty comment.', '', - nodes.literal_block(blocktext, blocktext), line=lineno) - return [figurenode, error], blank_finish + nodes.literal_block(block_text, block_text), line=lineno) + return [figure_node, error] if len(node) > 1: - figurenode += nodes.legend('', *node[1:]) - return [figurenode], blank_finish + figure_node += nodes.legend('', *node[1:]) + return [figure_node] + +figure.arguments = (1, 0, 1) +figure.options = image.options +figure.content = 1 |