hauptmech / pysvg

Automatically exported from code.google.com/p/pysvg
0 stars 0 forks source link

problems with parsing and saving a file containing unicode and "&" #7

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The patch below is a little hackish but it fixes problems with files containing 
non-ascii chars inside <text>/<tspan> elements

Index: pySVG/src/pysvg/core.py
===================================================================
--- pySVG/src/pysvg/core.py (revision 30)
+++ pySVG/src/pysvg/core.py (working copy)
@@ -6,6 +6,9 @@
 '''
 from attributes import CoreAttrib, ConditionalAttrib, StyleAttrib, GraphicalEventsAttrib, PaintAttrib, OpacityAttrib, GraphicsAttrib, CursorAttrib, FilterAttrib, MaskAttrib, ClipAttrib

+
+import codecs
+
 class BaseElement:
     """
     This is the base class for all svg elements like title etc. It provides common functionality.
@@ -86,16 +89,19 @@
             if value != None:
                 xml+=key+'="'+self.quote_attrib(str(value))+'" '
         if  len(self._subElements)==0: #self._textContent==None and
-            xml+=' />\n'
+            xml+=' />'
         else:
-            xml+=' >\n'
+            xml+=' >'
             #if self._textContent==None:
             for subelement in self._subElements:
-                xml+=str(subelement.getXML())
+                s = subelement.getXML()
+                if type(s) != unicode:
+                    s = str(s)
+                xml+=s
             #else:
             #if self._textContent!=None:
             #    xml+=self._textContent
-            xml+='</'+self._elementName+'>\n'
+            xml+='</'+self._elementName+'>'
         #print xml
         return xml

@@ -137,8 +143,10 @@
         Stores any element in a svg file (including header). 
         Calling this method only makes sense if the root element is an svg elemnt
         """
-        f = open(filename, 'w')
-        f.write(self.wrap_xml(self.getXML(), encoding, standalone))
+        f = codecs.open(filename, 'w', encoding)
+        s = self.wrap_xml(self.getXML(), encoding, standalone)
+        s = s.replace("&", "&amp;")
+        f.write(s)
         f.close()

     def quote_attrib(self, inStr):

Original issue reported on code.google.com by rob...@muth.org on 1 Feb 2012 at 2:35

GoogleCodeExporter commented 9 years ago
Will have a look and incorporate it. Thanks for your help !!

Original comment by kerim.ma...@gmail.com on 5 Nov 2012 at 8:19