Updated polish localization.
[wolnelektury.git] / compress / templatetags / compressed.py
1 import os
2
3 from django import template
4
5 from django.conf import settings as django_settings
6
7 from compress.conf import settings
8 from compress.utils import media_root, media_url, needs_update, filter_css, filter_js, get_output_filename, get_version
9
10 register = template.Library()
11
12 def render_common(template_name, obj, filename, version):
13     if settings.COMPRESS:
14         filename = get_output_filename(filename, version)
15
16     context = obj.get('extra_context', {})
17     context['url'] = media_url(filename)
18
19     return template.loader.render_to_string(template_name, context)
20
21 def render_css(css, filename, version=None):
22     return render_common(css.get('template_name', 'compress/css.html'), css, filename, version)
23
24 def render_js(js, filename, version=None):
25     return render_common(js.get('template_name', 'compress/js.html'), js, filename, version)
26
27 class CompressedCSSNode(template.Node):
28     def __init__(self, name):
29         self.name = name
30
31     def render(self, context):
32         css_name = template.Variable(self.name).resolve(context)
33
34         try:
35             css = settings.COMPRESS_CSS[css_name]
36         except KeyError:
37             return '' # fail silently, do not return anything if an invalid group is specified
38
39         if settings.COMPRESS:
40
41             version = None
42
43             if settings.COMPRESS_AUTO:
44                 u, version = needs_update(css['output_filename'], css['source_filenames'])
45                 if u:
46                     filter_css(css)
47
48             return render_css(css, css['output_filename'], version)
49         else:
50             # output source files
51             r = ''
52             for source_file in css['source_filenames']:
53                 r += render_css(css, source_file)
54
55             return r
56
57 class CompressedJSNode(template.Node):
58     def __init__(self, name):
59         self.name = name
60
61     def render(self, context):
62         js_name = template.Variable(self.name).resolve(context)
63
64         try:
65             js = settings.COMPRESS_JS[js_name]
66         except KeyError:
67             return '' # fail silently, do not return anything if an invalid group is specified
68
69         if settings.COMPRESS:
70
71             version = None
72
73             if settings.COMPRESS_AUTO:
74                 u, version = needs_update(js['output_filename'], js['source_filenames'])
75                 if u:
76                     filter_js(js)
77
78             return render_js(js, js['output_filename'], version)
79         else:
80             # output source files
81             r = ''
82             for source_file in js['source_filenames']:
83                 r += render_js(js, source_file)
84             return r
85
86 #@register.tag
87 def compressed_css(parser, token):
88     try:
89         tag_name, name = token.split_contents()
90     except ValueError:
91         raise template.TemplateSyntaxError, '%r requires exactly one argument: the name of a group in the COMPRESS_CSS setting' % token.split_contents()[0]
92
93     return CompressedCSSNode(name)
94 compressed_css = register.tag(compressed_css)
95
96 #@register.tag
97 def compressed_js(parser, token):
98     try:
99         tag_name, name = token.split_contents()
100     except ValueError:
101         raise template.TemplateSyntaxError, '%r requires exactly one argument: the name of a group in the COMPRESS_JS setting' % token.split_contents()[0]
102
103     return CompressedJSNode(name)
104 compressed_js = register.tag(compressed_js)