X-Git-Url: https://git.llucax.com/z.facultad/75.00/informe.git/blobdiff_plain/03b62e9899469e294fbd7d7e1622ec27cac669c2..3574dc3e457331aed8212c161d539ad5f69665d5:/ext/aafig.py diff --git a/ext/aafig.py b/ext/aafig.py index 7850613..793977e 100644 --- a/ext/aafig.py +++ b/ext/aafig.py @@ -1,15 +1,15 @@ # -*- 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 \ + (based on sphinxcontrib.mscgen). :license: BSD, see LICENSE for details. """ @@ -74,6 +74,7 @@ def render_aafigure(self, text, options, prefix): 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) @@ -84,22 +85,40 @@ def render_aafigure(self, text, options, prefix): 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 @@ -109,9 +128,12 @@ def render_html(self, node, text, options, prefix=DEFAULT_PREFIX, imgcls=None): self.body.append(self.encode(text)) else: imgcss = imgcls and 'class="%s"' % imgcls or '' - # nothing in image map - self.body.append('%s\n' % - (fname, self.encode(text).strip(), imgcss)) + if options['format'].lower() == 'svg': + self.body.append('' + (fname, extra, imgcss)) + else: + self.body.append('%s\n' % + (fname, self.encode(text).strip(), imgcss)) self.body.append('

\n') raise nodes.SkipNode @@ -123,7 +145,7 @@ def html_visit(self, node): 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 @@ -136,8 +158,10 @@ def render_latex(self, node, text, options, prefix=DEFAULT_PREFIX): 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')