1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8 -*-
4
from scour import scourString
3
import sys, platform, inkex
7
from scour.scour import scourString
9
inkex.errormsg("Failed to import Python module 'scour'.\nPlease make sure it is installed (e.g. using 'pip install scour' or 'sudo apt-get install python-scour') and try again.")
10
inkex.errormsg("\nDetails:\n" + str(e))
15
except Exception as e:
16
inkex.errormsg("Failed to import Python module 'six'.\nPlease make sure it is installed (e.g. using 'pip install six' or 'sudo apt-get install python-six') and try again.")
17
inkex.errormsg("\nDetails:\n" + str(e))
6
20
class ScourInkscape (inkex.Effect):
9
23
inkex.Effect.__init__(self)
10
self.OptionParser.add_option("--tab",
11
action="store", type="string",
13
self.OptionParser.add_option("--simplify-colors", type="inkbool",
14
action="store", dest="simple_colors", default=True,
15
help="won't convert all colors to #RRGGBB format")
16
self.OptionParser.add_option("--style-to-xml", type="inkbool",
17
action="store", dest="style_to_xml", default=True,
18
help="won't convert styles into XML attributes")
19
self.OptionParser.add_option("--group-collapsing", type="inkbool",
20
action="store", dest="group_collapse", default=True,
21
help="won't collapse <g> elements")
22
self.OptionParser.add_option("--create-groups", type="inkbool",
23
action="store", dest="group_create", default=False,
24
help="create <g> elements for runs of elements with identical attributes")
25
self.OptionParser.add_option("--enable-id-stripping", type="inkbool",
26
action="store", dest="strip_ids", default=False,
27
help="remove all un-referenced ID attributes")
28
self.OptionParser.add_option("--shorten-ids", type="inkbool",
29
action="store", dest="shorten_ids", default=False,
30
help="shorten all ID attributes to the least number of letters possible")
31
self.OptionParser.add_option("--embed-rasters", type="inkbool",
32
action="store", dest="embed_rasters", default=True,
33
help="won't embed rasters as base64-encoded data")
34
self.OptionParser.add_option("--keep-editor-data", type="inkbool",
35
action="store", dest="keep_editor_data", default=False,
36
help="won't remove Inkscape, Sodipodi or Adobe Illustrator elements and attributes")
37
self.OptionParser.add_option("--remove-metadata", type="inkbool",
38
action="store", dest="remove_metadata", default=False,
39
help="remove <metadata> elements (which may contain license metadata etc.)")
40
self.OptionParser.add_option("--strip-xml-prolog", type="inkbool",
41
action="store", dest="strip_xml_prolog", default=False,
42
help="won't output the <?xml ?> prolog")
43
self.OptionParser.add_option("-p", "--set-precision",
44
action="store", type=int, dest="digits", default=5,
45
help="set number of significant digits (default: %default)")
46
self.OptionParser.add_option("--indent",
47
action="store", type="string", dest="indent_type", default="space",
48
help="indentation of the output: none, space, tab (default: %default)")
49
self.OptionParser.add_option("--protect-ids-noninkscape", type="inkbool",
50
action="store", dest="protect_ids_noninkscape", default=False,
51
help="don't change IDs not ending with a digit")
52
self.OptionParser.add_option("--protect-ids-list",
53
action="store", type="string", dest="protect_ids_list", default=None,
54
help="don't change IDs given in a comma-separated list")
55
self.OptionParser.add_option("--protect-ids-prefix",
56
action="store", type="string", dest="protect_ids_prefix", default=None,
57
help="don't change IDs starting with the given prefix")
58
self.OptionParser.add_option("--enable-viewboxing", type="inkbool",
59
action="store", dest="enable_viewboxing", default=False,
60
help="changes document width/height to 100%/100% and creates viewbox coordinates")
61
self.OptionParser.add_option("--enable-comment-stripping", type="inkbool",
62
action="store", dest="strip_comments", default=False,
63
help="remove all <!-- --> comments")
64
self.OptionParser.add_option("--renderer-workaround", type="inkbool",
65
action="store", dest="renderer_workaround", default=False,
66
help="work around various renderer bugs (currently only librsvg)")
24
self.OptionParser.add_option("--tab", type="string", action="store", dest="tab")
25
self.OptionParser.add_option("--simplify-colors", type="inkbool", action="store", dest="simple_colors")
26
self.OptionParser.add_option("--style-to-xml", type="inkbool", action="store", dest="style_to_xml")
27
self.OptionParser.add_option("--group-collapsing", type="inkbool", action="store", dest="group_collapse")
28
self.OptionParser.add_option("--create-groups", type="inkbool", action="store", dest="group_create")
29
self.OptionParser.add_option("--enable-id-stripping", type="inkbool", action="store", dest="strip_ids")
30
self.OptionParser.add_option("--shorten-ids", type="inkbool", action="store", dest="shorten_ids")
31
self.OptionParser.add_option("--shorten-ids-prefix", type="string", action="store", dest="shorten_ids_prefix", default="")
32
self.OptionParser.add_option("--embed-rasters", type="inkbool", action="store", dest="embed_rasters")
33
self.OptionParser.add_option("--keep-unreferenced-defs", type="inkbool", action="store", dest="keep_defs")
34
self.OptionParser.add_option("--keep-editor-data", type="inkbool", action="store", dest="keep_editor_data")
35
self.OptionParser.add_option("--remove-metadata", type="inkbool", action="store", dest="remove_metadata")
36
self.OptionParser.add_option("--strip-xml-prolog", type="inkbool", action="store", dest="strip_xml_prolog")
37
self.OptionParser.add_option("--set-precision", type=int, action="store", dest="digits")
38
self.OptionParser.add_option("--indent", type="string", action="store", dest="indent_type")
39
self.OptionParser.add_option("--nindent", type=int, action="store", dest="indent_depth")
40
self.OptionParser.add_option("--line-breaks", type="inkbool", action="store", dest="newlines")
41
self.OptionParser.add_option("--strip-xml-space", type="inkbool", action="store", dest="strip_xml_space_attribute")
42
self.OptionParser.add_option("--protect-ids-noninkscape", type="inkbool", action="store", dest="protect_ids_noninkscape")
43
self.OptionParser.add_option("--protect-ids-list", type="string", action="store", dest="protect_ids_list")
44
self.OptionParser.add_option("--protect-ids-prefix", type="string", action="store", dest="protect_ids_prefix")
45
self.OptionParser.add_option("--enable-viewboxing", type="inkbool", action="store", dest="enable_viewboxing")
46
self.OptionParser.add_option("--enable-comment-stripping", type="inkbool", action="store", dest="strip_comments")
47
self.OptionParser.add_option("--renderer-workaround", type="inkbool", action="store", dest="renderer_workaround")
69
input = file(self.args[0], "r")
70
self.options.infilename=self.args[0]
71
sys.stdout.write(scourString(input.read(), self.options).encode("UTF-8"))
51
input = file(self.args[0], "r")
52
self.options.infilename = self.args[0]
53
sys.stdout.write(scourString(input.read(), self.options).encode("UTF-8"))
56
except Exception as e:
57
inkex.errormsg("Error during optimization.")
58
inkex.errormsg("\nDetails:\n" + str(e))
59
inkex.errormsg("\nOS version: " + platform.platform())
60
inkex.errormsg("Python version: " + sys.version)
61
inkex.errormsg("Scour version: " + scour.__version__)
76
64
if __name__ == '__main__':
77
65
e = ScourInkscape()