Commit 3e188779 by Timothée Peignier

small fix on compressor logic

parent 5ae685a6
...@@ -10,7 +10,7 @@ from pipeline.utils import to_class ...@@ -10,7 +10,7 @@ from pipeline.utils import to_class
MAX_IMAGE_SIZE = 32700 MAX_IMAGE_SIZE = 32700
EMBEDDABLE = r'[\A\/]embed\/' EMBEDDABLE = r'[/]?embed/'
URL_DETECTOR = r'url\([\'"]?([^\s)]+\.[a-z]+)[\'"]?\)' URL_DETECTOR = r'url\([\'"]?([^\s)]+\.[a-z]+)[\'"]?\)'
URL_REPLACER = r'url\(__EMBED__(.+?)(\?\d+)?\)' URL_REPLACER = r'url\(__EMBED__(.+?)(\?\d+)?\)'
...@@ -93,11 +93,12 @@ class Compressor(object): ...@@ -93,11 +93,12 @@ class Compressor(object):
]) ])
def template_name(self, path, base): def template_name(self, path, base):
name = os.path.basename(path) """Find out the name of a JS template"""
if base: if not base:
name = re.sub(r"^%s\/?(.*)%s$" % ( path = os.path.basename(path)
re.escape(base), re.escape(settings.PIPELINE_TEMPLATE_EXT) name = re.sub(r"^%s\/?(.*)%s$" % (
), r"\1", path) re.escape(base), re.escape(settings.PIPELINE_TEMPLATE_EXT)
), r"\1", path)
return re.sub(r"[\/\\]", "_", name) return re.sub(r"[\/\\]", "_", name)
def concatenate_and_rewrite(self, paths, variant=None): def concatenate_and_rewrite(self, paths, variant=None):
...@@ -121,6 +122,7 @@ class Compressor(object): ...@@ -121,6 +122,7 @@ class Compressor(object):
return "(function() { %s }).call(this);" % content return "(function() { %s }).call(this);" % content
def construct_asset_path(self, asset_path, css_path, variant=None): def construct_asset_path(self, asset_path, css_path, variant=None):
"""Return a rewritten asset URL for a stylesheet"""
public_path = self.absolute_path(asset_path, css_path) public_path = self.absolute_path(asset_path, css_path)
if self.embeddable(public_path, variant): if self.embeddable(public_path, variant):
return "__EMBED__%s" % public_path return "__EMBED__%s" % public_path
...@@ -129,11 +131,12 @@ class Compressor(object): ...@@ -129,11 +131,12 @@ class Compressor(object):
return urlparse.urljoin(settings.PIPELINE_URL, asset_path[1:]) return urlparse.urljoin(settings.PIPELINE_URL, asset_path[1:])
def embeddable(self, path, variant): def embeddable(self, path, variant):
"""Is the asset embeddable ?"""
name, ext = os.path.splitext(path) name, ext = os.path.splitext(path)
font = ext in FONT_EXTS font = ext in FONT_EXTS
if not variant: if not variant:
return False return False
if not re.match(path, EMBEDDABLE) and not os.path.exists(path): if not (re.search(EMBEDDABLE, path) and storage.exists(path)):
return False return False
if not ext in EMBED_EXTS: if not ext in EMBED_EXTS:
return False return False
...@@ -173,6 +176,7 @@ class Compressor(object): ...@@ -173,6 +176,7 @@ class Compressor(object):
return ''.join([part for parts in output for part in parts]) return ''.join([part for parts in output for part in parts])
def encoded_content(self, path): def encoded_content(self, path):
"""Return the base64 encoded contents"""
if path in self.__class__.asset_contents: if path in self.__class__.asset_contents:
return self.__class__.asset_contents[path] return self.__class__.asset_contents[path]
data = self.read_file(path) data = self.read_file(path)
...@@ -180,10 +184,15 @@ class Compressor(object): ...@@ -180,10 +184,15 @@ class Compressor(object):
return self.__class__.asset_contents[path] return self.__class__.asset_contents[path]
def mime_type(self, path): def mime_type(self, path):
"""Get mime-type from filename"""
name, ext = os.path.splitext(path) name, ext = os.path.splitext(path)
return MIME_TYPES[ext] return MIME_TYPES[ext]
def absolute_path(self, asset_path, css_path): def absolute_path(self, asset_path, css_path):
"""
Return the absolute public path for an asset,
given the path of the stylesheet that contains it.
"""
if os.path.isabs(asset_path): if os.path.isabs(asset_path):
path = os.path.join(settings.PIPELINE_ROOT, asset_path) path = os.path.join(settings.PIPELINE_ROOT, asset_path)
else: else:
...@@ -191,6 +200,7 @@ class Compressor(object): ...@@ -191,6 +200,7 @@ class Compressor(object):
return os.path.normpath(path) return os.path.normpath(path)
def relative_path(self, absolute_path): def relative_path(self, absolute_path):
"""Rewrite paths relative to the output stylesheet path"""
compress_root = os.path.normpath(settings.PIPELINE_ROOT) compress_root = os.path.normpath(settings.PIPELINE_ROOT)
return os.path.join('../', absolute_path.replace(compress_root, '')) return os.path.join('../', absolute_path.replace(compress_root, ''))
......
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