Browse Source

Rework css / js uri mechanism

pull/242/head
Florian Mounier 10 years ago
parent
commit
ce4b1cfe60
  1. 11
      docs/documentation/configuration/rendering.rst
  2. 8
      pygal/config.py
  3. 27
      pygal/svg.py

11
docs/documentation/configuration/rendering.rst

@ -122,7 +122,14 @@ Default:
.. code-block:: python .. 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 js
-- --
@ -130,7 +137,7 @@ js
.. code-block:: python .. code-block:: python
js = [ 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 <https://github.com/Kozea/pygal.js/>`_ See `pygal.js <https://github.com/Kozea/pygal.js/>`_

8
pygal/config.py

@ -226,9 +226,9 @@ class Config(CommonConfig):
DefaultStyle, Style, "Style", "Style holding values injected in css") DefaultStyle, Style, "Style", "Style holding values injected in css")
css = Key( css = Key(
('style.css', 'graph.css'), list, "Style", ('file://style.css', 'file://graph.css'), list, "Style",
"List of css file", "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) str)
# Look # # Look #
@ -475,9 +475,9 @@ class Config(CommonConfig):
# Misc # # Misc #
js = Key( 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", 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) str)
disable_xml_declaration = Key( disable_xml_declaration = Key(

27
pygal/svg.py

@ -85,18 +85,14 @@ class Svg(object):
colors = self.graph.style.get_colors(self.id, self.graph._order) colors = self.graph.style.get_colors(self.id, self.graph._order)
strokes = self.get_strokes() strokes = self.get_strokes()
all_css = [] all_css = []
for css in ['base.css'] + list(self.graph.css): for css in ['file://base.css'] + list(self.graph.css):
if '://' in css: css_text = None
self.processing_instructions.append(
etree.PI(
u('xml-stylesheet'), u('href="%s"' % css)))
else:
if css.startswith('inline:'): if css.startswith('inline:'):
css_text = css[len('inline:'):] css_text = css[len('inline:'):]
else: elif css.startswith('file://'):
if not os.path.exists(css): if not os.path.exists(css):
css = os.path.join( css = os.path.join(
os.path.dirname(__file__), 'css', css) os.path.dirname(__file__), 'css', css[len('file://'):])
class FontSizes(object): class FontSizes(object):
@ -118,9 +114,15 @@ class Svg(object):
strokes=strokes, strokes=strokes,
font_sizes=fs, font_sizes=fs,
id=self.id) id=self.id)
if css_text is not None:
if not self.graph.pretty_print: if not self.graph.pretty_print:
css_text = minify_css(css_text) css_text = minify_css(css_text)
all_css.append(css_text) all_css.append(css_text)
else:
self.processing_instructions.append(
etree.PI(
u('xml-stylesheet'), u('href="%s"' % css)))
self.node( self.node(
self.defs, 'style', type='text/css').text = '\n'.join(all_css) self.defs, 'style', type='text/css').text = '\n'.join(all_css)
@ -147,13 +149,12 @@ class Svg(object):
get_js_dict(), default=json_default))) get_js_dict(), default=json_default)))
for js in self.graph.js: for js in self.graph.js:
if '://' in js: if js.startswith('file://'):
self.node(
self.defs, 'script', type='text/javascript', href=js)
else:
script = self.node(self.defs, 'script', type='text/javascript') 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() 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): def node(self, parent=None, tag='g', attrib=None, **extras):
"""Make a new svg node""" """Make a new svg node"""

Loading…
Cancel
Save