Index: graphviz/graphviz.py
===================================================================
--- graphviz/graphviz.py (revision 4543)
+++ graphviz/graphviz.py (working copy)
@@ -259,7 +259,6 @@
content - The text the user entered for the macro to process.
"""
- req = formatter.req
# check and load the configuration
errmsg = self._load_config()
@@ -307,6 +306,21 @@
tag.p(_("Graphviz macro processor error: "
"requested format (%(fmt)s) not valid.",
fmt=out_format)))
+
+ # Eventually use first line of content for specifying attributes
+ lines = content.splitlines()
+ img_width = img_height = None
+ img_alt = _("GraphViz image"),
+ if lines and lines[0].startswith("
+ if (img_width, img_height) == (None, None):
+ try:
+ f = open(img_path, 'r')
+ svg = f.readlines(1024) # don't read all
+ f.close()
+ svg = "".join(svg).replace('\n', '')
+ w = re.search('width="([0-9]+)(.*?)" ', svg)
+ h = re.search('height="([0-9]+)(.*?)"', svg)
+ (w_val, w_unit) = w.group(1,2)
+ (h_val, h_unit) = h.group(1,2)
+ # Graphviz seems to underestimate height/width for SVG
+ # images, so we have to adjust them.
+ # The correction factor seems to be constant
+ # (XXX it might be better to force the width to something
+ # like 100% anyway... at least doing so manually produces
+ # far better results)
+ w_val, h_val = [1.35 * float(x) for x in (w_val, h_val)]
+ img_width = unicode(w_val) + w_unit
+ img_height = unicode(h_val) + h_unit
+ except ValueError:
+ img_width = img_height = '100%'
# insert SVG, IE compatibility
return tag.object(
tag.embed(src=img_url, type="image/svg+xml",
- width=width, height=height),
+ width=img_width, height=img_height),
data=img_url, type="image/svg+xml",
- width=width, height=height)
+ width=img_width, height=img_height)
# for binary formats, add map
elif URL_in_graph and os.path.exists(map_path):
@@ -408,9 +428,10 @@
map = "".join(map).replace('\n', '')
return tag(tag.map(Markup(map), id='G'+sha_key, name='G'+sha_key),
tag.img(src=img_url, usemap="#G"+sha_key,
- alt=_("GraphViz image")))
+ width=img_width, height=img_height, alt=img_alt))
else:
- return tag.img(src=img_url, alt=_("GraphViz image"))
+ return tag.img(src=img_url, width=img_width, height=img_height,
+ alt=img_alt)
# Private methods