Start 0.07: move scour functionality into a module-level function and only call when __main__. Move tests around a bit. Add starter testscour.py
|
Before Width: | Height: | Size: 2.3 MiB After Width: | Height: | Size: 2.3 MiB |
|
Before Width: | Height: | Size: 94 KiB After Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 274 B After Width: | Height: | Size: 274 B |
|
Before Width: | Height: | Size: 117 B After Width: | Height: | Size: 117 B |
|
Before Width: | Height: | Size: 69 B After Width: | Height: | Size: 69 B |
|
Before Width: | Height: | Size: 208 KiB After Width: | Height: | Size: 208 KiB |
|
Before Width: | Height: | Size: 250 KiB After Width: | Height: | Size: 250 KiB |
|
Before Width: | Height: | Size: 393 B After Width: | Height: | Size: 393 B |
|
Before Width: | Height: | Size: 144 B After Width: | Height: | Size: 144 B |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
|
@ -1,4 +1,4 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
SCOURVER="0.05"
|
SCOURVER="0.06"
|
||||||
tar cvf scour-$SCOURVER.tar scour.py LICENSE NOTICE README.txt
|
tar cvf scour-$SCOURVER.tar scour.py LICENSE NOTICE README.txt
|
||||||
gzip scour-$SCOURVER.tar
|
gzip scour-$SCOURVER.tar
|
||||||
107
scour.py
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/local/bin/python
|
#!/usr/local/bin/python
|
||||||
# Scour
|
# Scour
|
||||||
# Version 0.06
|
# Version 0.07
|
||||||
#
|
#
|
||||||
# Copyright 2009 Jeff Schiller
|
# Copyright 2009 Jeff Schiller
|
||||||
#
|
#
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
# Notes:
|
# Notes:
|
||||||
|
|
||||||
# rubys path-crunching ideas here: http://intertwingly.net/code/svgtidy/spec.rb
|
# rubys' path-crunching ideas here: http://intertwingly.net/code/svgtidy/spec.rb
|
||||||
# (and implemented here: http://intertwingly.net/code/svgtidy/svgtidy.rb )
|
# (and implemented here: http://intertwingly.net/code/svgtidy/svgtidy.rb )
|
||||||
|
|
||||||
# Yet more ideas here: http://wiki.inkscape.org/wiki/index.php/Save_Cleaned_SVG
|
# Yet more ideas here: http://wiki.inkscape.org/wiki/index.php/Save_Cleaned_SVG
|
||||||
|
|
@ -45,13 +45,10 @@
|
||||||
# * Process Transformations
|
# * Process Transformations
|
||||||
# * Process quadratic Bezier curves
|
# * Process quadratic Bezier curves
|
||||||
# * Collapse all group based transformations
|
# * Collapse all group based transformations
|
||||||
# * Output Standard SVG
|
|
||||||
# * Use viewPort instead of document width/height
|
|
||||||
|
|
||||||
# Next Up:
|
# Next Up:
|
||||||
# + Prevent error when stroke-width property value has a unit
|
# + move all functionality into a module level function named 'scour' and only call it
|
||||||
# + Convert width/height into a viewBox where possible
|
# when being run as main (prepare for unit testing)
|
||||||
# + Convert all referenced rasters into base64 encoded URLs if the files can be found
|
|
||||||
# - Removed duplicate gradient stops
|
# - Removed duplicate gradient stops
|
||||||
# - Convert all colors to #RRGGBB format
|
# - Convert all colors to #RRGGBB format
|
||||||
# -
|
# -
|
||||||
|
|
@ -70,7 +67,7 @@ import os.path
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
APP = 'scour'
|
APP = 'scour'
|
||||||
VER = '0.06'
|
VER = '0.07'
|
||||||
COPYRIGHT = 'Copyright Jeff Schiller, 2009'
|
COPYRIGHT = 'Copyright Jeff Schiller, 2009'
|
||||||
|
|
||||||
NS = { 'SVG': 'http://www.w3.org/2000/svg',
|
NS = { 'SVG': 'http://www.w3.org/2000/svg',
|
||||||
|
|
@ -550,45 +547,14 @@ def properlySizeDoc(docElement):
|
||||||
docElement.removeAttribute('width')
|
docElement.removeAttribute('width')
|
||||||
docElement.removeAttribute('height')
|
docElement.removeAttribute('height')
|
||||||
|
|
||||||
# parse command-line arguments
|
# this is the main method
|
||||||
args = sys.argv[1:]
|
# input is a string representation of the input XML
|
||||||
|
# returns a string representation of the output XML
|
||||||
# by default the input and output are the standard streams
|
def scour(in_string):
|
||||||
input = sys.stdin
|
global numAttrsRemoved
|
||||||
output = sys.stdout
|
global numStylePropsFixed
|
||||||
|
global numElemsRemoved
|
||||||
# if -i or -o is supplied, switch the stream to the file
|
doc = xml.dom.minidom.parseString(in_string)
|
||||||
if len(args) == 2:
|
|
||||||
if args[0] == '-i' :
|
|
||||||
input = open(args[1], 'r')
|
|
||||||
elif args[0] == '-o' :
|
|
||||||
output = open(args[1], 'w')
|
|
||||||
else:
|
|
||||||
printSyntaxAndQuit()
|
|
||||||
|
|
||||||
# if both -o and -o are supplied, switch streams to the files
|
|
||||||
elif len(args) == 4 :
|
|
||||||
if args[0] == '-i' and args[2] == '-o' :
|
|
||||||
input = open(args[1], 'r')
|
|
||||||
output = open(args[3], 'w')
|
|
||||||
elif args[0] == '-o' and args[2] == 'i' :
|
|
||||||
output = open(args[1], 'w')
|
|
||||||
input = open(args[3], 'r')
|
|
||||||
else:
|
|
||||||
printSyntaxAndQuit()
|
|
||||||
|
|
||||||
# else invalid syntax
|
|
||||||
elif len(args) != 0 :
|
|
||||||
printSyntaxAndQuit()
|
|
||||||
|
|
||||||
# if we are not sending to stdout, then print out app information
|
|
||||||
bOutputReport = False
|
|
||||||
if output != sys.stdout :
|
|
||||||
bOutputReport = True
|
|
||||||
printHeader()
|
|
||||||
|
|
||||||
# build DOM in memory
|
|
||||||
doc = xml.dom.minidom.parse(input)
|
|
||||||
|
|
||||||
# for whatever reason this does not always remove all inkscape/sodipodi attributes/elements
|
# for whatever reason this does not always remove all inkscape/sodipodi attributes/elements
|
||||||
# on the first pass, so we do it multiple times
|
# on the first pass, so we do it multiple times
|
||||||
|
|
@ -646,7 +612,52 @@ for elem in doc.documentElement.getElementsByTagNameNS(NS['SVG'], 'image') :
|
||||||
properlySizeDoc(doc.documentElement)
|
properlySizeDoc(doc.documentElement)
|
||||||
|
|
||||||
# output the document
|
# output the document
|
||||||
doc.documentElement.writexml(output)
|
out_string = doc.documentElement.toxml()
|
||||||
|
return out_string
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
# parse command-line arguments
|
||||||
|
args = sys.argv[1:]
|
||||||
|
|
||||||
|
# by default the input and output are the standard streams
|
||||||
|
input = sys.stdin
|
||||||
|
output = sys.stdout
|
||||||
|
|
||||||
|
# if -i or -o is supplied, switch the stream to the file
|
||||||
|
if len(args) == 2:
|
||||||
|
if args[0] == '-i' :
|
||||||
|
input = open(args[1], 'r')
|
||||||
|
elif args[0] == '-o' :
|
||||||
|
output = open(args[1], 'w')
|
||||||
|
else:
|
||||||
|
printSyntaxAndQuit()
|
||||||
|
|
||||||
|
# if both -o and -o are supplied, switch streams to the files
|
||||||
|
elif len(args) == 4 :
|
||||||
|
if args[0] == '-i' and args[2] == '-o' :
|
||||||
|
input = open(args[1], 'r')
|
||||||
|
output = open(args[3], 'w')
|
||||||
|
elif args[0] == '-o' and args[2] == 'i' :
|
||||||
|
output = open(args[1], 'w')
|
||||||
|
input = open(args[3], 'r')
|
||||||
|
else:
|
||||||
|
printSyntaxAndQuit()
|
||||||
|
|
||||||
|
# else invalid syntax
|
||||||
|
elif len(args) != 0 :
|
||||||
|
printSyntaxAndQuit()
|
||||||
|
|
||||||
|
# if we are not sending to stdout, then print out app information
|
||||||
|
bOutputReport = False
|
||||||
|
if output != sys.stdout :
|
||||||
|
bOutputReport = True
|
||||||
|
printHeader()
|
||||||
|
|
||||||
|
# do the work
|
||||||
|
in_string = input.read()
|
||||||
|
out_string = scour(in_string)
|
||||||
|
output.write(out_string)
|
||||||
|
|
||||||
# Close input and output files
|
# Close input and output files
|
||||||
input.close()
|
input.close()
|
||||||
|
|
|
||||||
23
testscour.py
Executable file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#!/usr/local/bin/python
|
||||||
|
# Test Harness for Scour
|
||||||
|
#
|
||||||
|
# Copyright 2009 Jeff Schiller
|
||||||
|
#
|
||||||
|
# This file is part of Scour, http://www.codedread.com/scour/
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import scour
|
||||||
|
|
||||||
|
print "done"
|
||||||