Example #1
0
File: svg.py Project: Kozea/pygal
    def add_styles(self):
        """Add the css to the svg"""
        colors = self.graph.style.get_colors(self.id, self.graph._order)
        strokes = self.get_strokes()
        all_css = []
        auto_css = ["file://base.css"]

        if self.graph.style._google_fonts:
            auto_css.append(
                "//fonts.googleapis.com/css?family=%s" % quote_plus("|".join(self.graph.style._google_fonts))
            )

        for css in auto_css + list(self.graph.css):
            css_text = None
            if css.startswith("inline:"):
                css_text = css[len("inline:") :]
            elif css.startswith("file://"):
                css = css[len("file://") :]

                if not os.path.exists(css):
                    css = os.path.join(os.path.dirname(__file__), "css", css)

                with io.open(css, encoding="utf-8") as f:
                    css_text = template(f.read(), style=self.graph.style, colors=colors, strokes=strokes, id=self.id)

            if css_text is not None:
                if not self.graph.pretty_print:
                    css_text = minify_css(css_text)
                all_css.append(css_text)
            else:
                if css.startswith("//") and self.graph.force_uri_protocol:
                    css = "%s:%s" % (self.graph.force_uri_protocol, css)
                self.processing_instructions.append(etree.PI(u("xml-stylesheet"), u('href="%s"' % css)))
        self.node(self.defs, "style", type="text/css").text = "\n".join(all_css)
Example #2
0
File: svg.py Project: nam4dev/pygal
    def add_styles(self):
        """Add the css to the svg"""
        colors = self.graph.style.get_colors(self.id, self.graph._order)
        strokes = self.get_strokes()
        all_css = []
        auto_css = ['template://{}'.format(CSS_BASE)]

        if self.graph.style._google_fonts:
            auto_css.append(
                '//fonts.googleapis.com/css?family=%s' %
                quote_plus('|'.join(self.graph.style._google_fonts)))

        for css in auto_css + list(self.graph.css):
            css_text = None
            if css.startswith('inline:'):
                css_text = css[len('inline:'):]

            elif css.startswith('template://'):
                css = css[len('template://'):]
                css_text = template(css,
                                    style=self.graph.style,
                                    colors=colors,
                                    strokes=strokes,
                                    id=self.id)

            elif css.startswith('file://'):

                css = css[len('file://'):]

                if not os.path.exists(css):
                    css = os.path.join(os.path.dirname(__file__), 'css', css)

                with io.open(css, encoding='utf-8') as f:
                    css_text = template(f.read(),
                                        style=self.graph.style,
                                        colors=colors,
                                        strokes=strokes,
                                        id=self.id)

            if css_text is not None:
                if not self.graph.pretty_print:
                    css_text = minify_css(css_text)
                all_css.append(css_text)
            else:
                if css.startswith('//') and self.graph.force_uri_protocol:
                    css = '%s:%s' % (self.graph.force_uri_protocol, css)
                self.processing_instructions.append(
                    etree.PI(u('xml-stylesheet'), u('href="%s"' % css)))
        self.node(self.defs, 'style',
                  type='text/css').text = '\n'.join(all_css)
Example #3
0
    def add_styles(self):
        """Add the css to the svg"""
        colors = self.graph.style.get_colors(self.id, self.graph._order)
        strokes = self.get_strokes()
        all_css = []
        auto_css = [base_css]

        if self.graph.style._google_fonts:
            auto_css.append(
                '//fonts.googleapis.com/css?family=%s' % quote_plus(
                    '|'.join(self.graph.style._google_fonts))
            )

        for css in auto_css + list(self.graph.css):
            css_text = None
            if type(css) == str and css.startswith('inline:'):
                css_text = css[len('inline:'):]
            elif type(css) == str and css.startswith('file://'):
                if not os.path.exists(css):
                    css = os.path.join(
                        os.path.dirname(__file__), 'css', css[len('file://'):])

                with io.open(css, encoding='utf-8') as f:
                    css_text = template(
                        f.read(),
                        style=self.graph.style,
                        colors=colors,
                        strokes=strokes,
                        id=self.id)
            elif not type(css) == str:
                css_text = template(
                    css.data,
                    style=self.graph.style,
                    colors=colors,
                    strokes=strokes,
                    id=self.id)

            if css_text is not None:
                if not self.graph.pretty_print:
                    css_text = minify_css(css_text)
                all_css.append(css_text)
            else:
                if type(css) == str and css.startswith('//') and self.graph.force_uri_protocol:
                    css = '%s:%s' % (self.graph.force_uri_protocol, css)
                self.processing_instructions.append(
                    etree.PI(
                        u('xml-stylesheet'), u('href="%s"' % css)))
        self.node(
            self.defs, 'style', type='text/css').text = '\n'.join(all_css)