+ aafig_options = dict()
+ image_attrs = dict()
+ own_options_keys = self.own_option_spec.keys() + ['scale']
+ for (k, v) in self.options.items():
+ if k in own_options_keys:
+ # convert flags to booleans
+ if v is None:
+ v = True
+ # convert percentage to float
+ if k == 'scale' or k == 'aspect':
+ v = float(v) / 100.0
+ aafig_options[k] = v
+ del self.options[k]
+ self.arguments = ['']
+ (image_node,) = directives.images.Image.run(self)
+ if isinstance(image_node, nodes.system_message):
+ return [image_node]
+ text = '\n'.join(self.content)
+ image_node.aafig = dict(options = aafig_options, text = text)
+ return [image_node]
+
+
+def render_aafig_images(app, doctree):
+ format_map = app.builder.config.aafig_format
+ merge_dict(format_map, DEFAULT_FORMATS)
+ for img in doctree.traverse(nodes.image):
+ if not hasattr(img, 'aafig'):
+ continue
+
+ options = img.aafig['options']
+ text = img.aafig['text']
+ format = app.builder.format
+ merge_dict(options, app.builder.config.aafig_default_options)
+ if format in format_map:
+ options['format'] = format_map[format]
+ else:
+ app.builder.warn('unsupported builder format "%s", please '
+ 'add a custom entry in aafig_format config option '
+ 'for this builder' % format)
+ img.replace_self(nodes.literal_block(text, text))
+ continue
+ if options['format'] is None:
+ img.replace_self(nodes.literal_block(text, text))
+ continue
+ try:
+ fname, outfn, id, extra = render_aafigure(app, text, options)
+ except AafigError, exc:
+ app.builder.warn('aafigure error: ' + str(exc))
+ img.replace_self(nodes.literal_block(text, text))
+ continue
+ img['uri'] = fname
+ # FIXME: find some way to avoid this hack in aafigure
+ if extra:
+ (width, height) = [x.split('"')[1] for x in extra.split()]
+ if not img.has_key('width'):
+ img['width'] = width
+ if not img.has_key('height'):
+ img['height'] = height
+
+
+def render_aafigure(app, text, options):