Hi Walter, et all,<div><br></div><div>This patch should go most of the way to fixing the bug. However, I haven't tested it locally & it contains a few assumptions about what the SVG files look like that that it's processing. I'm also not 100% sure about the elementtree implementation of ElementTree (am used to lxml). That said, I would like your feedback about whether you consider this to be an appropriate way to approach the problem.</div>
<div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace">diff --git a/taexporthtml.py b/taexporthtml.py</font></div><div><font class="Apple-style-span" face="'courier new', monospace">index 5a0d163..daad983 100644</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">--- a/taexporthtml.py</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+++ b/taexporthtml.py</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace">@@ -25,6 +25,78 @@ import os.path</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> from tautils import data_to_string, save_picture, image_to_base64</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> from gettext import gettext as _</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> </font></div><div><font class="Apple-style-span" face="'courier new', monospace">+try: import xml.etree.cElementTree as ET</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+except ImportError:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ try: import cElementTree as ET # python <2.5</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ except ImportError:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ try: import lxml.etree as ET</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ except ImportError:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ try: import xml.etree.ElementTree as ET</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ except ImportError:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ import elementtree.ElementTree as ET</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+def split_at_len(s, max_len=50, delimiter=None):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ """</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ Returns a list of strings, that are split from an</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ input string. Delimits on whitespace by default.</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ """</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ if delimiter is None:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ s = s.split()</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ else:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ s = s.split(delimiter)</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace">+</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ lines = []</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ current_line = ''</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ for word in s:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ if len(current_line) >= max_len:</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ short.append(current_line)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ current_line = word</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ else:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ current_line = '%s %s' % (current_line, word);</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ lines[0] = lines[0][1:] #hack required by string formatting</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ lines.append(current_line)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ return lines</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+def process_svg_text_element(el, max_len=50, line_height='1.1em'):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ """</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ Turns </font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ <text>long ... content</text></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ into </font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ <text></font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ <tspan>long</tspan><tspan>...</tspan><tspan>content</tspan></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ </text></font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ """</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace">+ # NOTE Assumes that there are no subelements this</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ # should be safe because TurtleArt shouldn't</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ # generate any</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ data = split_at_len(el.text, max_len=max_len)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ for line in data:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ ET.SubElement(el, 'tspan')</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ i = 0</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ while i < len(data):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ el[i].text = data[i]</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ el[i].attrib['dy'] = (i*line_height)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ i = i+1</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ return el</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+def process_text_in_svg(svg, max_len=50, line_height='1.1em'):</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ """</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ Takes an SVG as a string or file-like object</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ and returns a string with text elements split</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ into multiple lines surrounded by <tspan>.</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ """</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ tree = ET.parse(svg)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ svg_text = tree.findall('text')</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ if len(svg_text) > 0:</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ for el in svg_text:</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ # TODO check if this actually replaces the element in tree</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ el = process_svg_text_element(el, </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ max_len=max_len, </font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ line_height=line_height)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ return ET.tostring(tree)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> def save_html(self, tw, embed_flag=True):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> """ Either: Save canvas and code or pictures to HTML """</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> self.embed_images = embed_flag</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">@@ -85,7 +157,7 @@ def save_html(self, tw, embed_flag=True):</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> imgdata = f.read()</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> f.close()</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> if p.endswith(('.svg')):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">- tmp = imgdata</font></div><div><font class="Apple-style-span" face="'courier new', monospace">+ tmp = process_text_in_svg(imgdata)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> else:</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> pixbuf = gtk.gdk.pixbuf_new_from_file(p)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> imgdata = image_to_base64(pixbuf, tw.activity)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">@@ -97,7 +169,7 @@ def save_html(self, tw, embed_flag=True):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> f = open(p, "r")</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> imgdata = f.read()</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> f.close()</font></div><div><font class="Apple-style-span" face="'courier new', monospace">- tmp = imgdata</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">+ tmp = process_text_in_svg(imgdata)</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> else:</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> tmp = self.html_glue['img3'][0]</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> tmp += p</font></div>
</div>