From b7ac64c30865491d7420b7554afa33042d91b38e Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Sat, 23 Oct 2021 21:49:30 +0200 Subject: [PATCH] added set_css_class extension --- .../box_maker_t_slot/box_maker_t_slot.inx | 4 +- .../fablabchemnitz/set_css_class/meta.json | 20 ++++++++++ .../set_css_class/set_css_class.inx | 19 +++++++++ .../set_css_class/set_css_class.py | 40 +++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 extensions/fablabchemnitz/set_css_class/meta.json create mode 100644 extensions/fablabchemnitz/set_css_class/set_css_class.inx create mode 100644 extensions/fablabchemnitz/set_css_class/set_css_class.py diff --git a/extensions/fablabchemnitz/box_maker_t_slot/box_maker_t_slot.inx b/extensions/fablabchemnitz/box_maker_t_slot/box_maker_t_slot.inx index 2ec441f8..f82c1a77 100644 --- a/extensions/fablabchemnitz/box_maker_t_slot/box_maker_t_slot.inx +++ b/extensions/fablabchemnitz/box_maker_t_slot/box_maker_t_slot.inx @@ -34,7 +34,9 @@ all - + + + + \ No newline at end of file diff --git a/extensions/fablabchemnitz/set_css_class/set_css_class.py b/extensions/fablabchemnitz/set_css_class/set_css_class.py new file mode 100644 index 00000000..91fba752 --- /dev/null +++ b/extensions/fablabchemnitz/set_css_class/set_css_class.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +""" +Sets a css class on selected elements, while optionally removing the elements' styling. +If inline styles are not removed, the css class might not have effect. + +Inspired by MergeStyles (and best used together with it). +""" + +__author__ = "Mois Moshev" +__email__ = "mois@monomon.me" +__copyright__ = "Copyright (C) 2017 Mois Moshev" +__license__ = "GPL" + +import inkex +import sys + +class SetCSSClass(inkex.EffectExtension): + + def add_arguments(self, pars): + pars.add_argument("--name", help="Name of css class to apply") + pars.add_argument("--clear_styles", type=inkex.Boolean, default=True, help="Name of css class to apply") + + def effect(self): + newclass = self.options.name + elements = self.svg.selected.values() + + for el in elements: + current_classes = el.attrib.has_key("class") and el.attrib["class"].split() or [] + + if newclass not in current_classes: + current_classes.append(newclass) + + if self.options.clear_styles: + el.attrib["style"] = "" + + el.attrib["class"] = " ".join(current_classes) + +if __name__ == "__main__": + SetCSSClass().run()