summaryrefslogtreecommitdiffstats
path: root/share/extensions/output_scour.py
blob: 185ea97a16220c9ce01d41b95161dfbbdddb4969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
"""
Run the scour module on the svg output.
"""


import inkex
from inkex.localization import inkex_gettext as _

try:
    from packaging.version import Version
except ImportError:
    raise inkex.DependencyError(
        _(
            """Failed to import module 'packaging'.
Please make sure it is installed (e.g. using 'pip install packaging'
or 'sudo apt-get install python3-packaging') and try again.
"""
        )
    )

try:
    import scour
    from scour.scour import scourString
except ImportError:
    raise inkex.DependencyError(
        _(
            """Failed to import module 'scour'.
Please make sure it is installed (e.g. using 'pip install scour'
  or 'sudo apt-get install python3-scour') and try again.
"""
        )
    )


class ScourInkscape(inkex.OutputExtension):
    """Scour Inkscape Extension"""

    # Scour options
    def add_arguments(self, pars):
        pars.add_argument("--tab")
        pars.add_argument("--simplify-colors", type=inkex.Boolean, dest="simple_colors")
        pars.add_argument("--style-to-xml", type=inkex.Boolean)
        pars.add_argument(
            "--group-collapsing", type=inkex.Boolean, dest="group_collapse"
        )
        pars.add_argument("--create-groups", type=inkex.Boolean, dest="group_create")
        pars.add_argument("--enable-id-stripping", type=inkex.Boolean, dest="strip_ids")
        pars.add_argument("--shorten-ids", type=inkex.Boolean)
        pars.add_argument("--shorten-ids-prefix")
        pars.add_argument("--embed-rasters", type=inkex.Boolean)
        pars.add_argument(
            "--keep-unreferenced-defs", type=inkex.Boolean, dest="keep_defs"
        )
        pars.add_argument("--keep-editor-data", type=inkex.Boolean)
        pars.add_argument("--remove-metadata", type=inkex.Boolean)
        pars.add_argument("--strip-xml-prolog", type=inkex.Boolean)
        pars.add_argument("--set-precision", type=int, dest="digits")
        pars.add_argument("--indent", dest="indent_type")
        pars.add_argument("--nindent", type=int, dest="indent_depth")
        pars.add_argument("--line-breaks", type=inkex.Boolean, dest="newlines")
        pars.add_argument(
            "--strip-xml-space", type=inkex.Boolean, dest="strip_xml_space_attribute"
        )
        pars.add_argument("--protect-ids-noninkscape", type=inkex.Boolean)
        pars.add_argument("--protect-ids-list")
        pars.add_argument("--protect-ids-prefix")
        pars.add_argument("--enable-viewboxing", type=inkex.Boolean)
        pars.add_argument(
            "--enable-comment-stripping", type=inkex.Boolean, dest="strip_comments"
        )
        pars.add_argument("--renderer-workaround", type=inkex.Boolean)

        # options for internal use of the extension
        pars.add_argument("--scour-version")
        pars.add_argument("--scour-version-warn-old", type=inkex.Boolean)

    def save(self, stream):
        # version check if enabled in options
        if self.options.scour_version_warn_old:
            scour_version = scour.__version__
            scour_version_min = self.options.scour_version
            if Version(scour_version) < Version(scour_version_min):
                raise inkex.AbortExtension(
                    f"""
The extension 'Optimized SVG Output' is designed for Scour {scour_version_min} or later but you're
 using the older version Scour {scour_version}.

Note: You can permanently disable this message on the 'About' tab of the extension window."""
                )
        del self.options.scour_version
        del self.options.scour_version_warn_old

        # do the scouring
        stream.write(scourString(self.svg.tostring(), self.options).encode("utf8"))


if __name__ == "__main__":
    ScourInkscape().run()