From 0a481165f011a89472cf6d9da1d1e0f607cb4c2f Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Tue, 11 Feb 2014 10:14:48 +0100 Subject: [PATCH] Add inline css + css tests --- pygal/graph/datey.py | 2 +- pygal/svg.py | 27 +++++++++++++++------------ pygal/test/test_config.py | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/pygal/graph/datey.py b/pygal/graph/datey.py index 77d9f00..bda6d6e 100644 --- a/pygal/graph/datey.py +++ b/pygal/graph/datey.py @@ -51,7 +51,7 @@ class DateY(XY): def _todate(self, d): """ Converts a number to a date """ currDateTime = self._offset + datetime.timedelta(seconds=d or 0) - return currDateTime.strftime( self.x_label_format ) + return currDateTime.strftime(self.x_label_format) def _tonumber(self, d): """ Converts a date to a number """ diff --git a/pygal/svg.py b/pygal/svg.py index 6de5272..76956fe 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -74,18 +74,21 @@ class Svg(object): etree.PI( u('xml-stylesheet'), u('href="%s"' % css))) else: - 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.config.style, - colors=colors, - font_sizes=self.graph.config.font_sizes(), - id=self.id) - if not self.graph.pretty_print: - css_text = minify_css(css_text) + if css.startswith('inline:'): + css_text = css[len('inline:'):] + else: + 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.config.style, + colors=colors, + font_sizes=self.graph.config.font_sizes(), + id=self.id) + if not self.graph.pretty_print: + css_text = minify_css(css_text) all_css.append(css_text) self.node( self.defs, 'style', type='text/css').text = '\n'.join(all_css) diff --git a/pygal/test/test_config.py b/pygal/test/test_config.py index 6cb63fe..bf0f29c 100644 --- a/pygal/test/test_config.py +++ b/pygal/test/test_config.py @@ -288,3 +288,29 @@ def test_include_x_axis(Chart): chart.include_x_axis = True q = chart.render_pyquery() assert '0.0' in q(yaxis).map(texts) + + +def test_css(Chart): + css = "{{ id }}text { fill: #bedead; }\n" + css_file = '/tmp/pygal_custom_style.css' + with open(css_file, 'w') as f: + f.write(css) + + config = Config() + config.css.append(css_file) + + chart = Chart(config) + chart.add('/', [10, 1, 5]) + svg = chart.render().decode('utf-8') + assert '#bedead' in svg + + +def test_inline_css(Chart): + css = "{{ id }}text { fill: #bedead; }\n" + + config = Config() + config.css.append('inline:' + css) + chart = Chart(config) + chart.add('/', [10, 1, 5]) + svg = chart.render().decode('utf-8') + assert '#bedead' in svg