# -*- coding: utf-8 -*-
"""
- sphinxcontrib.aafigure
- ~~~~~~~~~~~~~~~~~~~~~~
+ sphinxcontrib.aafig
+ ~~~~~~~~~~~~~~~~~~~
- Allow mscgen-formatted :abbr:`MSC (Message Sequence Chart)` graphs to be
- included in Sphinx-generated documents inline.
+ Allow embeded ASCII art to be rendered as nice looking images
+ using the aafigure reStructuredText extension.
See the README file for details.
- :copyright: Copyright 2009 by Leandro Lucarella (based on \
- sphinx.ext.graphviz).
+ :copyright: Copyright 2009 by Leandro Lucarella <llucax@gmail.com> \
+ (based on sphinxcontrib.mscgen).
:license: BSD, see LICENSE for details.
"""
hashkey = text.encode('utf-8') + str(options)
id = sha(hashkey).hexdigest()
fname = '%s-%s.%s' % (prefix, id, options['format'])
+ metadata_fname = '%s.aafig' % fname
if hasattr(self.builder, 'imgpath'):
# HTML
relfn = posixpath.join(self.builder.imgpath, fname)
outfn = path.join(self.builder.outdir, fname)
if path.isfile(outfn):
- return relfn, outfn, id
+ extra = None
+ if options['format'].lower() == 'svg':
+ f = file(metadata_fname, 'r')
+ extra = f.read()
+ f.close()
+ return relfn, outfn, id, extra
ensuredir(path.dirname(outfn))
+ # merge default options
+ for (k, v) in self.builder.config.aafig_default_options.items():
+ if k not in options:
+ options[k] = v
+
try:
- aafigure.render(text, outfn, options)
+ (visitor, output) = aafigure.render(text, outfn, options)
+ output.close()
except aafigure.UnsupportedFormatError, e:
raise MscgenError(str(e))
- return relfn, outfn, id
+ extra = None
+ if options['format'].lower() == 'svg':
+ extra = visitor.get_size_attrs()
+ f = file(metadata_fname, 'w')
+ f.write(extra)
+ f.close()
+
+ return relfn, outfn, id, extra
def render_html(self, node, text, options, prefix=DEFAULT_PREFIX, imgcls=None):
try:
options['format'] = self.builder.config.aafig_format['html']
- fname, outfn, id = render_aafigure(self, text, options, prefix)
+ fname, outfn, id, extra = render_aafigure(self, text, options, prefix)
except AafigError, exc:
self.builder.warn('aafigure error: ' + str(exc))
raise nodes.SkipNode
self.body.append(self.encode(text))
else:
imgcss = imgcls and 'class="%s"' % imgcls or ''
- # nothing in image map
- self.body.append('<img src="%s" alt="%s" %s/>\n' %
- (fname, self.encode(text).strip(), imgcss))
+ if options['format'].lower() == 'svg':
+ self.body.append('<object type="image/svg+xml" data="%s" %s %s />'
+ (fname, extra, imgcss))
+ else:
+ self.body.append('<img src="%s" alt="%s" %s/>\n' %
+ (fname, self.encode(text).strip(), imgcss))
self.body.append('</p>\n')
raise nodes.SkipNode
def render_latex(self, node, text, options, prefix=DEFAULT_PREFIX):
try:
options['format'] = self.builder.config.aafig_format['latex']
- fname, outfn, id = render_aafigure(self, text, options, prefix)
+ fname, outfn, id, extra = render_aafigure(self, text, options, prefix)
except AafigError, exc:
self.builder.warn('aafigure error: ' + str(exc))
raise nodes.SkipNode
def latex_visit(self, node):
render_latex(self, node, node['text'], node['options'])
+
def setup(app):
app.add_node(aafig, html=(html_visit, None), latex=(latex_visit, None))
app.add_directive('aafig', Aafig)
app.add_config_value('aafig_format', dict(html='svg', latex='pdf'), 'html')
+ app.add_config_value('aafig_default_options', dict(), 'html')