def replace_url(self, url): # Replace mode: manually adjust the location of files if callable(self.replace): return self.replace(url) elif self.replace is not False: for to_replace, sub in self.replace_dict.items(): targeturl = urlparse.urljoin(self.source_url, url) if targeturl.startswith(to_replace): url = "%s%s" % (sub, targeturl[len(to_replace) :]) # Only apply the first match break # Default mode: auto correct relative urls else: # If path is an absolute one, keep it parsed = urlparse.urlparse(url) if not parsed.scheme and not parsed.path.startswith("/"): abs_source_url = urlparse.urljoin(self.source_url, url) # relpath() will not detect this case if urlparse.urlparse(abs_source_url).scheme: return abs_source_url # rewritten url: relative path from new location (output) # to location of referenced file (source + current url) url = urlpath.relpath(self.output_url, abs_source_url) return url
def replace_url(self, url): # Replace mode: manually adjust the location of files if callable(self.replace): return self.replace(url) elif self.replace is not False: for to_replace, sub in self.replace_dict.items(): targeturl = urlparse.urljoin(self.source_url, url) if targeturl.startswith(to_replace): url = "%s%s" % (sub, targeturl[len(to_replace):]) # Only apply the first match break # Default mode: auto correct relative urls else: # If path is an absolute one, keep it parsed = urlparse.urlparse(url) if not parsed.scheme and not parsed.path.startswith('/'): abs_source_url = urlparse.urljoin(self.source_url, url) # relpath() will not detect this case if urlparse.urlparse(abs_source_url).scheme: return abs_source_url # rewritten url: relative path from new location (output) # to location of referenced file (source + current url) url = urlpath.relpath(self.output_url, abs_source_url) return url
def replace_url(self, url): # Replace mode: manually adjust the location of files if callable(self.replace): return self.replace(url) elif self.replace is not False: for to_replace, sub in self.replace_dict.items(): targeturl = urlparse.urljoin(self.source_url, url) if targeturl.startswith(to_replace): url = "%s%s" % (sub, targeturl[len(to_replace):]) # Only apply the first match break # Default mode: auto correct relative urls else: # If path is an absolute one, keep it parsed = urlparse.urlparse(url) if not parsed.scheme and not parsed.path.startswith('/'): abs_source_url = urlparse.urljoin(self.source_url, url) parsed_source_url = urlparse.urlparse(abs_source_url) parsed_output_url = urlparse.urlparse(self.output_url) # relpath() will not detect this case, where we have a full # url but not a full output_url if parsed_source_url.scheme and not parsed_output_url.scheme: return abs_source_url # relpath() will not detect these neither, when we have two # full urls, but on different hosts:ports or different schemes # (http vs https for example) if parsed_source_url.netloc != parsed_output_url.netloc: return abs_source_url if parsed_source_url.scheme != parsed_output_url.scheme: return abs_source_url # rewritten url: relative path from new location (output) # to location of referenced file (source + current url) url = urlpath.relpath(self.output_url, abs_source_url) return url
def replace_url(self, url): if url.startswith('data:'): # Don't even both sending data: through urlparse(), # who knows how well it'll deal with a lot of data. return # Ignore any urls which are not relative parsed = urlparse.urlparse(url) if parsed.scheme or parsed.netloc or parsed.path.startswith('/'): return # Since this runs BEFORE cssrewrite, we can thus assume that urls # will be relative to the file location. # # Notes: # - Django might need to override this filter for staticfiles if it # it should be possible to resolve cross-references between # different directories. # - For Flask-Assets blueprints, the logic might need to be: # 1) Take source_path, convert into correct url via absurl(). # 2) Join with the URL be be replaced. # 3) Convert url back to the filesystem path to which the url # would map (the hard part?). # filename = os.path.join(os.path.dirname(self.source_path), url) try: if os.stat(filename).st_size <= (self.max_size or 2048): with open(filename, 'rb') as f: data = b64encode(f.read()) return 'data:%s;base64,%s' % ( mimetypes.guess_type(filename)[0], data.decode()) except (OSError, IOError): # Ignore the file not existing. # TODO: When we have a logging system, this could produce a warning return