From ce4b1cfe604bdd15d3c4eff5686908843d6b8b70 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Thu, 16 Jul 2015 10:22:49 +0200 Subject: [PATCH] Rework css / js uri mechanism --- .../documentation/configuration/rendering.rst | 11 ++- pygal/config.py | 8 +- pygal/svg.py | 77 ++++++++++--------- 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/docs/documentation/configuration/rendering.rst b/docs/documentation/configuration/rendering.rst index 225f3f2..14163b8 100644 --- a/docs/documentation/configuration/rendering.rst +++ b/docs/documentation/configuration/rendering.rst @@ -122,7 +122,14 @@ Default: .. code-block:: python - css = ['style.css', 'graph.css'] + css = ['file://style.css', 'file://graph.css'] + + +Css can also specified inline by prepending `inline:` to the css: + +.. code-block:: python + css = ['inline:.rect { fill: blue; }'] + js -- @@ -130,7 +137,7 @@ js .. code-block:: python js = [ - 'http://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js' + '//kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js' ] See `pygal.js `_ diff --git a/pygal/config.py b/pygal/config.py index 87a86b8..0d0d4c4 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -226,9 +226,9 @@ class Config(CommonConfig): DefaultStyle, Style, "Style", "Style holding values injected in css") css = Key( - ('style.css', 'graph.css'), list, "Style", + ('file://style.css', 'file://graph.css'), list, "Style", "List of css file", - "It can be an absolute file path or an external link", + "It can be any uri from file:///tmp/style.css to //domain/style.css", str) # Look # @@ -475,9 +475,9 @@ class Config(CommonConfig): # Misc # js = Key( - ('http://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js',), + ('//kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js',), list, "Misc", "List of js file", - "It can be a filepath or an external link", + "It can be any uri from file:///tmp/ext.js to //domain/ext.js", str) disable_xml_declaration = Key( diff --git a/pygal/svg.py b/pygal/svg.py index fe85b71..1745967 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -85,42 +85,44 @@ class Svg(object): colors = self.graph.style.get_colors(self.id, self.graph._order) strokes = self.get_strokes() all_css = [] - for css in ['base.css'] + list(self.graph.css): - if '://' in css: - self.processing_instructions.append( - etree.PI( - u('xml-stylesheet'), u('href="%s"' % css))) - else: - 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) - - class FontSizes(object): - - """Container for font sizes""" - - fs = FontSizes() - for name in dir(self.graph.state): - if name.endswith('_font_size'): - setattr( - fs, - name.replace('_font_size', ''), - ('%dpx' % getattr(self.graph, name))) - - with io.open(css, encoding='utf-8') as f: - css_text = template( - f.read(), - style=self.graph.style, - colors=colors, - strokes=strokes, - font_sizes=fs, - id=self.id) + for css in ['file://base.css'] + list(self.graph.css): + css_text = None + if css.startswith('inline:'): + css_text = css[len('inline:'):] + elif css.startswith('file://'): + if not os.path.exists(css): + css = os.path.join( + os.path.dirname(__file__), 'css', css[len('file://'):]) + + class FontSizes(object): + + """Container for font sizes""" + + fs = FontSizes() + for name in dir(self.graph.state): + if name.endswith('_font_size'): + setattr( + fs, + name.replace('_font_size', ''), + ('%dpx' % getattr(self.graph, name))) + + with io.open(css, encoding='utf-8') as f: + css_text = template( + f.read(), + style=self.graph.style, + colors=colors, + strokes=strokes, + font_sizes=fs, + 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: + 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) @@ -147,13 +149,12 @@ class Svg(object): get_js_dict(), default=json_default))) for js in self.graph.js: - if '://' in js: - self.node( - self.defs, 'script', type='text/javascript', href=js) - else: + if js.startswith('file://'): script = self.node(self.defs, 'script', type='text/javascript') - with io.open(js, encoding='utf-8') as f: + with io.open(js[len('file://'):], encoding='utf-8') as f: script.text = f.read() + else: + self.node(self.defs, 'script', type='text/javascript', href=js) def node(self, parent=None, tag='g', attrib=None, **extras): """Make a new svg node"""