Leandro Lucarella avatar Leandro Lucarella committed 372d33b

aafigure: Fix SVG output for HTML writer

The SVG output was broken for the HTML writer because <img> tags don't work
with SVG images, an <object> tag must be used instead. This change needs
aafigure r5978 or later in order to be able to get the SVG size attributes.

Comments (0)

Files changed (3)

 ------------------------
 
 * Add ``aafig_default_options`` configuration option.
+* Fix SVG output for HTML writer (this need aafigure r5978 or later).
 
 
 Version 0.1 (2009-06-05)
 Requirements
 ============
 
-* aafigure_ (r5974 or later).
+* aafigure_ (r5978 or later).
 * reportlab_ (for LaTeX/PDF output)
 * PIL_ (for any image format other than SVG or PDF)
 

aafig/sphinxcontrib/aafig.py

     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))
 
             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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.