From 419896117434f55bdaefd2b9cc88a4b7f76f3be9 Mon Sep 17 00:00:00 2001 From: JSCHILL1 Date: Fri, 13 Nov 2009 10:44:18 -0600 Subject: [PATCH] Attempt to handle line endings in a cross-platform manner --- package.sh | 2 +- release-notes.html | 10 ++++++++++ scour.py | 30 +++++++++++++++--------------- testscour.py | 17 +++++++++++++---- unittests/whitespace-important.svg | 2 +- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/package.sh b/package.sh index b992af8..8a3fb6a 100755 --- a/package.sh +++ b/package.sh @@ -1,5 +1,5 @@ #!/bin/bash -SCOURVER="0.22" +SCOURVER="0.23" cd .. zip scour/tarballs/scour-$SCOURVER.zip scour/scour.py scour/yocto_css.py scour/svg_regex.py scour/LICENSE scour/NOTICE scour/README.txt scour/release-notes.html cd scour diff --git a/release-notes.html b/release-notes.html index 54c5c3a..1c7662f 100644 --- a/release-notes.html +++ b/release-notes.html @@ -9,6 +9,16 @@

Copyright 2009, Jeff Schiller

+
+
+

Version 0.23

+
+

TBD

+ +
+

Version 0.22

diff --git a/scour.py b/scour.py index c68295c..6bf14fa 100755 --- a/scour.py +++ b/scour.py @@ -2092,7 +2092,7 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False): if preserveWhitespace: outString += serializeXML(child, options, 0, preserveWhitespace) else: - outString += '\n' + serializeXML(child, options, indent + 1, preserveWhitespace) + outString += os.linesep + serializeXML(child, options, indent + 1, preserveWhitespace) onNewLine = True # text node elif child.nodeType == 3: @@ -2114,10 +2114,10 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False): if onNewLine: outString += (I * ind) outString += '' - if indent > 0: outString += '\n' + if indent > 0: outString += os.linesep else: outString += '/>' - if indent > 0: outString += '\n' + if indent > 0: outString += os.linesep return outString @@ -2273,7 +2273,7 @@ def scourString(in_string, options=None): # http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/ # rolled our own serialize function here to save on space, put id first, customize indentation, etc # out_string = doc.documentElement.toprettyxml(' ') - out_string = serializeXML(doc.documentElement, options) + out_string = serializeXML(doc.documentElement, options) + os.linesep # now strip out empty lines lines = [] @@ -2284,7 +2284,7 @@ def scourString(in_string, options=None): # return the string stripped of empty lines if options.strip_xml_prolog == False: - xmlprolog = '\n' + xmlprolog = '' + os.linesep else: xmlprolog = "" @@ -2385,15 +2385,15 @@ def parse_args(args=None): return options, [infile, outfile] def getReport(): - return ' Number of elements removed: ' + str(numElemsRemoved) + \ - '\n Number of attributes removed: ' + str(numAttrsRemoved) + \ - '\n Number of unreferenced id attributes removed: ' + str(numIDsRemoved) + \ - '\n Number of style properties fixed: ' + str(numStylePropsFixed) + \ - '\n Number of raster images embedded inline: ' + str(numRastersEmbedded) + \ - '\n Number of path segments reduced/removed: ' + str(numPathSegmentsReduced) + \ - '\n Number of bytes saved in path data: ' + str(numBytesSavedInPathData) + \ - '\n Number of bytes saved in colors: ' + str(numBytesSavedInColors) + \ - '\n Number of points removed from polygons: ' + str(numPointsRemovedFromPolygon) + return ' Number of elements removed: ' + str(numElemsRemoved) + os.linesep + \ + ' Number of attributes removed: ' + str(numAttrsRemoved) + os.linesep + \ + ' Number of unreferenced id attributes removed: ' + str(numIDsRemoved) + os.linesep + \ + ' Number of style properties fixed: ' + str(numStylePropsFixed) + os.linesep + \ + ' Number of raster images embedded inline: ' + str(numRastersEmbedded) + os.linesep + \ + ' Number of path segments reduced/removed: ' + str(numPathSegmentsReduced) + os.linesep + \ + ' Number of bytes saved in path data: ' + str(numBytesSavedInPathData) + os.linesep + \ + ' Number of bytes saved in colors: ' + str(numBytesSavedInColors) + os.linesep + \ + ' Number of points removed from polygons: ' + str(numPointsRemovedFromPolygon) if __name__ == '__main__': if sys.platform == "win32": @@ -2423,7 +2423,7 @@ if __name__ == '__main__': # GZ: unless silenced by -q or something? # GZ: not using globals would be good too print >>sys.stderr, ' File:', input.name, \ - '\n Time taken:', str(end-start) + 's\n', \ + os.linesep + ' Time taken:', str(end-start) + 's' + os.linesep, \ getReport() oldsize = len(in_string) diff --git a/testscour.py b/testscour.py index 10c5547..bb59323 100755 --- a/testscour.py +++ b/testscour.py @@ -834,7 +834,8 @@ class CDATAInXml(unittest.TestCase): -''', + +''', 'Improperly serialized the cdata unit tests') class WellFormedXMLLesserThanInAttrValue(unittest.TestCase): @@ -933,7 +934,8 @@ class DoNotPrettyPrintWhenWhitespacePreserved(unittest.TestCase): ''' This is some messed-up markup -''', + +''', 'Whitespace not preserved') class DoNotPrettyPrintWhenNestedWhitespacePreserved(unittest.TestCase): @@ -942,7 +944,8 @@ class DoNotPrettyPrintWhenNestedWhitespacePreserved(unittest.TestCase): ''' Use bold text -''', + +''', 'Whitespace not preserved when nested') class GetAttrPrefixRight(unittest.TestCase): @@ -954,7 +957,7 @@ class GetAttrPrefixRight(unittest.TestCase): class EnsurePreserveWhitespaceOnNonTextElements(unittest.TestCase): def runTest(self): s = scour.scourString(open('unittests/no-collapse-lines.svg').read()) - self.assertEquals( s.count('\n'), 5, + self.assertEquals( len(s.splitlines()), 6, 'Did not properly preserve whitespace on elements even if they were not textual') class HandleEmptyStyleElement(unittest.TestCase): @@ -967,6 +970,12 @@ class HandleEmptyStyleElement(unittest.TestCase): self.assertEquals( fail, False, 'Could not handle an empty style element') +class EnsureLineEndings(unittest.TestCase): + def runTest(self): + s = scour.scourString(open('unittests/whitespace-important.svg').read()) + self.assertEquals( len(s.splitlines()), 4, + 'Did not output line ending character correctly') + # TODO; write a test for embedding rasters # TODO: write a test for --disable-embed-rasters # TODO: write tests for --keep-editor-data diff --git a/unittests/whitespace-important.svg b/unittests/whitespace-important.svg index e829569..6918044 100644 --- a/unittests/whitespace-important.svg +++ b/unittests/whitespace-important.svg @@ -1,4 +1,4 @@ This is some messed-up markup - \ No newline at end of file +