-
Notifications
You must be signed in to change notification settings - Fork 2k
/
fanstatic_extensions.py
116 lines (91 loc) · 3.91 KB
/
fanstatic_extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# encoding: utf-8
import fanstatic.core as core
class CkanCustomRenderer(object):
''' Allows for in-line js and IE conditionals via fanstatic. '''
def __init__(self, script=None, renderer=None, condition=None,
other_browsers=False):
self.script = script
self.other_browsers = other_browsers
self.renderer = renderer
start = ''
end = ''
# IE conditionals
if condition:
start = '<!--[if %s]>' % condition
end = '<![endif]-->'
if other_browsers:
start += '<!-->'
end = '<!--' + end
self.start = start
self.end = end
def __call__(self, url):
if self.script:
return '%s<script>%s</script>%s' % (self.start,
self.script,
self.end)
return '%s%s%s' % (self.start,
self.renderer(url),
self.end)
def render_js(url):
return '<script src="%s"></script>' % (url,)
# __ __ _ ____ _ _
# | \/ | ___ _ __ | | _____ _ _ | _ \ __ _| |_ ___| |__
# | |\/| |/ _ \| '_ \| |/ / _ \ | | | | |_) / _` | __/ __| '_ \
# | | | | (_) | | | | < __/ |_| | | __/ (_| | || (__| | | |
# |_| |_|\___/|_| |_|_|\_\___|\__, | |_| \__,_|\__\___|_| |_|
# |___/
def render(self, library_url):
paths = [resource.relpath for resource in self._resources]
# MONKEY PATCH
# We have changed the relpath to just use the bundle path as we don't
# care about the dir name of the resource as that is now part of the
# resource name. We need this so that we can actually get to all the
# resources regardless of the actual directory they are in.
# relpath = ''.join([self.dirname, BUNDLE_PREFIX, ';'.join(paths)])
relpath = ''.join([core.BUNDLE_PREFIX, ';'.join(paths)])
return self.renderer('%s/%s' % (library_url, relpath))
core.Bundle.render = render
def fits(self, resource):
if resource.dont_bundle:
return False
# an empty resource fits anything
if not self._resources:
return True
# a resource fits if it's like the resources already inside
bundle_resource = self._resources[0]
return (resource.library is bundle_resource.library and
resource.renderer is bundle_resource.renderer and
# MONKEY PATCH
# We allow .js files to be bundled even if they are in different
# directories as the directory location doesn't actually matter
# to javascript files just css files.
(resource.ext == '.js' or
resource.dirname == bundle_resource.dirname))
core.Bundle.fits = fits
def sort_resources(resources):
"""Sort resources for inclusion on web page.
A number of rules are followed:
* resources are always grouped per renderer (.js, .css, etc)
* resources that depend on other resources are sorted later
* resources are grouped by library, if the dependencies allow it
* libraries are sorted by name, if dependencies allow it
* resources are sorted by resource path if they both would be
sorted the same otherwise.
The only purpose of sorting on library is so we can
group resources per library, so that bundles can later be created
of them if bundling support is enabled.
Note this sorting algorithm guarantees a consistent ordering, no
matter in what order resources were needed.
"""
for resource in resources:
resource.library.init_library_nr()
def key(resource):
return (
resource.order,
resource.library.library_nr,
resource.library.name,
resource.custom_order, # Added in MONKEY PATCH
resource.dependency_nr,
resource.relpath)
return sorted(resources, key=key)
core.sort_resources = sort_resources