Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Apr 28, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 1 Feb 2012 at 2:35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant