summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 384f5cb)
raw | patch | inline | side by side (parent: 384f5cb)
author | JazzyNico <nicoduf@yahoo.fr> | |
Fri, 5 Feb 2010 22:38:46 +0000 (23:38 +0100) | ||
committer | JazzyNico <nicoduf@yahoo.fr> | |
Fri, 5 Feb 2010 22:38:46 +0000 (23:38 +0100) |
share/extensions/scour.py | patch | blob | history |
index b2b2c178ea2a0b81a31ce2e5c4a5357479a46e98..b851e48c3de3610c08e7214539bd64586c8d60b0 100755 (executable)
# 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 <?xml?> and <svg>)
# - add an option to remove ids if they match the Inkscape-style of IDs
# - investigate point-reducing algorithms
# - parse transform attribute
pass
APP = 'scour'
-VER = '0.23'
+VER = '0.24'
COPYRIGHT = 'Copyright Jeff Schiller, 2010'
NS = { 'SVG': 'http://www.w3.org/2000/svg',
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
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 = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' + os.linesep
+ total_output = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' + 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