From: JazzyNico Date: Fri, 5 Feb 2010 22:38:46 +0000 (+0100) Subject: Extensions. Scour update (0.24) X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=f38954576c3ff3d56385fd638186170f94b92d92;p=inkscape.git Extensions. Scour update (0.24) --- diff --git a/share/extensions/scour.py b/share/extensions/scour.py index b2b2c178e..b851e48c3 100755 --- a/share/extensions/scour.py +++ b/share/extensions/scour.py @@ -34,8 +34,8 @@ # at rounded corners) # Next Up: +# - Bug 511186: option to keep XML comments between prolog and root element # - only remove unreferenced elements if they are not children of a referenced element -# - TODO: fix the removal of comment elements (between and ) # - add an option to remove ids if they match the Inkscape-style of IDs # - investigate point-reducing algorithms # - parse transform attribute @@ -72,7 +72,7 @@ except ImportError: pass APP = 'scour' -VER = '0.23' +VER = '0.24' COPYRIGHT = 'Copyright Jeff Schiller, 2010' NS = { 'SVG': 'http://www.w3.org/2000/svg', @@ -2021,21 +2021,17 @@ def remapNamespacePrefix(node, oldprefix, newprefix): remapNamespacePrefix(child, oldprefix, newprefix) def makeWellFormed(str): - newstr = str - - # encode & as & ( must do this first so that < does not become &lt; ) - if str.find('&') != -1: - newstr = str.replace('&', '&') + xml_ents = { '<':'<', '>':'>', '&':'&', "'":''', '"':'"'} + +# starr = [] +# for c in str: +# if c in xml_ents: +# starr.append(xml_ents[c]) +# else: +# starr.append(c) - # encode < as < - if str.find("<") != -1: - newstr = str.replace('<', '<') - - # encode > as > (TODO: is this necessary?) - if str.find('>') != -1: - newstr = str.replace('>', '>') - - return newstr + # this list comprehension is short-form for the above for-loop: + return ''.join([xml_ents[c] if c in xml_ents else c for c in str]) # hand-rolled serialization function that has the following benefits: # - pretty printing @@ -2295,13 +2291,19 @@ def scourString(in_string, options=None): if line.strip(): lines.append(line) - # return the string stripped of empty lines + # return the string with its XML prolog and surrounding comments if options.strip_xml_prolog == False: - xmlprolog = '' + os.linesep + total_output = '' + os.linesep else: - xmlprolog = "" + total_output = "" + + for child in doc.childNodes: + if child.nodeType == 1: + total_output += "".join(lines) + else: # doctypes, entities, comments + total_output += child.toxml() + os.linesep - return xmlprolog + "".join(lines) + return total_output # used mostly by unit tests # input is a filename