ścieżki kopernika - test form
[edumed.git] / edumed / templatetags / inline_stylesheet.py
1 # -*- coding: utf-8 -*-
2 # https://github.com/jazzband/django-pipeline/issues/295#issuecomment-152095865
3 # adjusted for older pipeline
4 from __future__ import unicode_literals
5 import codecs
6
7 from django.contrib.staticfiles.storage import staticfiles_storage
8 from django.utils.safestring import mark_safe
9 from django import template
10 from pipeline.templatetags import compressed
11
12 register = template.Library()
13
14
15 class StylesheetNode(compressed.CompressedCSSNode):
16     def render_css(self, package, path):
17         return self.render_individual_css(package, [path])
18
19     def render_individual_css(self, package, paths, **kwargs):
20         html = []
21         for path in paths:
22             with codecs.open(staticfiles_storage.path(path), 'r', 'utf-8') as f:
23                 html.append(f.read())
24         html = '<style type="text/css">' + '\n'.join(html) + '</style>'
25         return mark_safe(html)
26
27
28 @register.tag
29 def inline_stylesheet(parser, token):
30     """ Template tag that mimics pipeline's stylesheet tag, but embeds
31     the resulting CSS directly in the page.
32     """
33     try:
34         tag_name, name = token.split_contents()
35     except ValueError:
36         raise template.TemplateSyntaxError(
37             '%r requires exactly one argument: the name of a group in the PIPELINE_CSS setting'
38             % token.split_contents()[0])
39     return StylesheetNode(name)