summaryrefslogtreecommitdiffstats
path: root/share/extensions/jessyink_summary.py
blob: 698abbfdf95bb33374ee52f8a707b4d54cd00e42 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
#
# Copyright 2008, 2009 Hannes Hochreiner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/.
#
"""Effect to print out jessyInk summary"""

from collections import defaultdict
import inkex

from jessyink_install import JessyInkMixin, _


class Summary(JessyInkMixin, inkex.EffectExtension):
    """Print of jessyInk summary"""

    def add_arguments(self, pars):
        pars.add_argument("--tab")

    def effect(self):
        self.is_installed()

        # Find the script node, if present
        for node in self.svg.xpath("//svg:script[@id='JessyInk']"):
            version = node.get("jessyink:version")
            if version:
                self.msg(_("JessyInk script version {} installed.".format(version)))
            else:
                self.msg(_("JessyInk script installed."))

        slides = []
        master_slide = None

        for node in self.svg.descendants().get(inkex.Layer):
            if node.get("jessyink:master_slide"):
                master_slide = node
            else:
                slides.append(node)

        if master_slide is not None:
            self.msg(_("\nMaster slide:"))
            self.describe_node(
                master_slide,
                "\t",
                ["<the number of the slide>", len(slides), "<the title of the slide>"],
            )

        for i, slide in enumerate(slides):
            self.msg(_("\nSlide {0!s}:").format(i + 1))
            self.describe_node(slide, "\t", [i + 1, len(slides), slide.label])

    def describe_node(self, node, prefix, dat):
        """Standard print out formatter"""

        self.msg(_(f"{prefix}Layer name: {node.label}"))
        self.describe_transition(node, prefix, "In")
        self.describe_transition(node, prefix, "Out")
        self.describe_autotext(node, prefix, dat)
        self.describe_effects(node, prefix)

    def describe_transition(self, node, prefix, transition):
        """Display information about transitions."""
        trans = inkex.Style(node.get(f"jessyink:transition{transition}"))
        if trans:
            name = trans["name"]
            if name != "appear" and "length" in trans:
                length = int(trans["length"] / 1000.0)
                self.msg(
                    _(
                        "{0}Transition {1}: {2} ({3!s} s)".format(
                            prefix, transition, name, length
                        )
                    )
                )
            else:
                self.msg(_(f"{prefix}Transition {transition}: {name}"))

    def describe_autotext(self, node, prefix, dat):
        """Display information about auto-texts."""
        auto_texts = {"slide_num": dat[0], "num": dat[1], "title": dat[2]}
        for x, child in enumerate(node.xpath(".//*[@jessyink:autoText]")):
            if not x:
                self.msg(_("\n{0}Auto-texts:").format(prefix))

            pid = child.getparent().get("id")
            val = auto_texts[child.get("jessyink:autoText")]
            self.msg(
                _(
                    _('{0}\t"{1}" (object id "{2}") will be replaced by "{3}".').format(
                        prefix, child.text, pid, val
                    )
                )
            )

    def describe_effects(self, node, prefix):
        """Display information about effects."""
        effects = sorted(self.collect_effects(node), key=lambda val: val[0])
        for x, (enum, effect) in enumerate(effects):
            ret = ""

            order = effect[0]["order"]
            if not x:
                ret += _("\n{0}Initial effect (order number {1}):").format(
                    prefix, order
                )
            else:
                ret += _("\n{0}Effect {1} (order number {2}):").format(
                    prefix, enum, order
                )

            for item in effect:
                eid = item["id"]
                if item["type"] == "view":
                    ret += _('{0}\tView will be set according to object "{1}"').format(
                        prefix, eid
                    )
                else:
                    ret += _('{0}\tObject "{1}"').format(prefix, eid)

                    if item["direction"] == "in":
                        ret += _(" will appear")
                    elif item["direction"] == "out":
                        ret += _(" will disappear")

                if item["name"] != "appear":
                    ret += _(' using effect "{0}"').format(item["name"])

                if "length" in item:
                    ret += _(" in {0!s} s").format(int(item["length"]) / 1000.0)

            self.msg(ret + ".\n")

    @staticmethod
    def collect_effects(node):
        """Collect information about effects."""
        effects = defaultdict(list)
        for child in node.xpath(".//*[@jessyink:effectIn]"):
            effect_data = inkex.Style(child.get("jessyink:effectIn"))
            effect_data["direction"] = "in"
            effect_data["id"] = child.get("id")
            effect_data["type"] = "effect"
            effects[effect_data["order"]].append(effect_data)

        for child in node.xpath(".//*[@jessyink:effectOut]"):
            effect_data = inkex.Style(child.get("jessyink:effectOut"))
            effect_data["direction"] = "out"
            effect_data["id"] = child.get("id")
            effect_data["type"] = "effect"
            effects[effect_data["order"]].append(effect_data)

        for child in node.xpath(".//*[@jessyink:view]"):
            effect_data = inkex.Style(child.get("jessyink:view"))
            effect_data["id"] = child.get("id")
            effect_data["type"] = "view"
            effects[effect_data["order"]].append(effect_data)
        return effects


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