summaryrefslogtreecommitdiffstats
path: root/share/extensions/jessyink_video.py
blob: 63b0afbad37366565c88307f0e5c8fda039253db (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
#!/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/.
#
"""Add a video to the slideshow"""

import re
from copy import deepcopy

import inkex
from jessyink_install import JessyInkMixin, _


class Video(JessyInkMixin, inkex.EffectExtension):
    """Add jessyink video"""

    def add_arguments(self, pars):
        self.arg_parser.add_argument("--tab", dest="what")

    def effect(self):
        self.is_installed()
        # Check version.
        base_view = self.svg.xpath("//sodipodi:namedview[@id='base']")
        if base_view is None:
            raise inkex.AbortExtension(
                _(
                    "Could not obtain the selected layer for inclusion of the video element."
                )
            )

        layer = self.svg.get_current_layer()
        if layer is None:
            raise inkex.AbortExtension(
                _(
                    "Could not obtain the selected layer for inclusion of the video element.\n\n"
                )
            )

        template = inkex.load_svg(self.get_resource("jessyink_video.svg"))
        root = template.getroot()

        elem = layer.add(
            root.getElement("//svg:g[@jessyink:element='core.video']").copy()
        )
        node_dict = find_internal_links(elem, root, {})
        delete_ids(elem)

        for node in node_dict.values():
            elem.insert(0, node)

        for node in node_dict.values():
            node.set_id(self.svg.get_unique_id("jessyink.core.video"), backlinks=True)


def find_internal_links(node, svg, node_dict):
    """Get all clone links and css links"""
    for entry in re.findall(rb"url\(#.*\)", node.tostring()):
        entry = entry.decode()
        link_id = entry[5 : len(entry) - 1]

        if link_id not in node_dict:
            node_dict[link_id] = deepcopy(svg.getElementById(link_id))
            find_internal_links(node_dict[link_id], svg, node_dict)

    for entry in node.iter():
        if entry.get("xlink:href"):
            link_id = entry.get("xlink:href")
            if link_id not in node_dict:
                node_dict[link_id] = deepcopy(svg.getElementById(link_id))
                find_internal_links(node_dict[link_id], svg, node_dict)

    return node_dict


def delete_ids(node):
    """Delete ids in the given node's children"""
    for entry in node.iter():
        if "id" in entry.attrib:
            del entry.attrib["id"]


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