Commit 9759b300 by Denis V Seleznyov

Convert urls relative to current css files to full

If source CSS contain a relative URL not starting with slash (i.e.
relative to current file), those URL will be converted to full relative
path using MEDIA_URL, which should be the same as if COMPRESS is set to
False. When compress enabled, images will be accessible on the same URL.
parent e63a6234
...@@ -86,12 +86,29 @@ def concat(filenames, separator=''): ...@@ -86,12 +86,29 @@ def concat(filenames, separator=''):
""" """
Concatenate the files from the list of the ``filenames``, ouput separated with ``separator``. Concatenate the files from the list of the ``filenames``, ouput separated with ``separator``.
""" """
# find relative paths in css:
# url definition, any spacing, single or double quotes, no starting slash
rel_exp = re.compile(
'(url\s*\(\s*[\'"]?\s*)([^/\'"\s]\S+?)(\s*[\'"]?\s*\))',
flags=re.IGNORECASE)
r = '' r = ''
for filename in filenames: for filename in filenames:
fd = open(compress_source(filename), 'rb') fd = open(compress_source(filename), 'rb')
r += fd.read() contents = fd.read()
r += separator
fd.close() fd.close()
if filename.lower().endswith('.css') and \
django_settings.MEDIA_ROOT == settings.COMPRESS_SOURCE:
if django_settings.MEDIA_URL.endswith('/'):
abspath = os.path.normpath(os.path.dirname(filename))
else:
abspath = os.path.normpath(os.path.join('/',
os.path.dirname(filename)))
abspath = django_settings.MEDIA_URL + abspath + '/'
fixed = rel_exp.sub('\\1' + abspath + '\\2\\3', contents)
r += fixed
else:
r += contents
r += separator
return r return r
def save_file(filename, contents): def save_file(filename, contents):
......
...@@ -86,6 +86,32 @@ Network, you can use the optional _prefix_ parameter: ...@@ -86,6 +86,32 @@ Network, you can use the optional _prefix_ parameter:
In this example, the template tags will render _http://cdn.example.com/css/one_compressed.css_ in the link tag. You will need to manually put there after you build as part of your deployment process. In this example, the template tags will render _http://cdn.example.com/css/one_compressed.css_ in the link tag. You will need to manually put there after you build as part of your deployment process.
h4. CSS URL not starting with slash
If source CSS contain a relative URL not starting with slash (i.e. relative to current file), those URL will be converted to full relative path using MEDIA_URL. This is done _only when SOURCE_ROOT is the same as MEDIA_ROOT_ (default). This conversion is performed before any filters applied.
Example:
<pre><code>
media/js/fancybox/
fancybox.png
fancybox-x.png
fancybox-y.png
jquery.fancybox-1.3.4.css
jquery.fancybox-1.3.4.js
</code></pre>
jquery.fancybox-1.3.4.css contains:
<pre><code>
background-image: url('fancybox.png');
background-image: url('fancybox-x.png');
background-image: url('fancybox-y.png');
</code></pre>
In resulting CSS it will be rewritten to:
<pre><code>
background-image:url(/js/fancybox/fancybox.png);
background-image:url(/js/fancybox/fancybox-x.png);
background-image:url(/js/fancybox/fancybox-y.png);
</code></pre>
(Assuming MEDIA_URL is '' or '/', with non-empty MEDIA_URL result will be another).
h4. CSS URL Replace Filter h4. CSS URL Replace Filter
This is a really simple filter for doing Python string replacements on your CSS. The use case for this is when you have root relative This is a really simple filter for doing Python string replacements on your CSS. The use case for this is when you have root relative
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment