Example #1
0
    def input(self, _in, out, **kw):
        if self.replace not in (False, None) and not callable(self.replace):
            # For replace mode, make sure we have all the directories to be
            # rewritten in form of a url, so we can later easily match it
            # against the urls encountered in the CSS.
            replace_dict = False
            root = addsep(self.env.directory)
            replace_dict = OrderedDict()
            for repldir, sub in self.replace.items():
                repldir = addsep(os.path.normpath(join(root, repldir)))
                replurl = path2url(repldir[len(common_path_prefix([root, repldir])) :])
                replace_dict[replurl] = sub
            self.replace_dict = replace_dict

        return super(CSSRewrite, self).input(_in, out, **kw)
Example #2
0
    def input(self, _in, out, **kw):
        if self.replace not in (False, None) and not callable(self.replace):
            # For replace mode, make sure we have all the directories to be
            # rewritten in form of a url, so we can later easily match it
            # against the urls encountered in the CSS.
            replace_dict = False
            root = addsep(self.env.directory)
            replace_dict = OrderedDict()
            for repldir, sub in self.replace.items():
                repldir = addsep(os.path.normpath(join(root, repldir)))
                replurl = path2url(repldir[len(common_path_prefix([root, repldir])):])
                replace_dict[replurl] = sub
            self.replace_dict = replace_dict

        return super(CSSRewrite, self).input(_in, out, **kw)
Example #3
0
 def _find_base_path(self, paths):
     """Hmmm.  There should aways be some common base path."""
     if len(paths) == 1:
         return os.path.dirname(paths[0])
     return common_path_prefix(paths)
Example #4
0
 def _find_base_path(self, paths):
     """Hmmm.  There should aways be some common base path."""
     if len(paths) == 1:
         return os.path.dirname(paths[0])
     return common_path_prefix(paths)
Example #5
0
 def _find_base_path(self):
     """Hmmm.  There should aways be some common base path."""
     paths = [path for path, content in self.templates]
     if len(paths) == 1:
         return os.path.dirname(paths[0])
     return common_path_prefix(paths)
Example #6
0
 def _find_base_path(self):
     """Hmmm.  There should aways be some common base path."""
     paths = [path for path, content in self._templates]
     if len(paths) == 1:
         return os.path.dirname(paths[0])
     return common_path_prefix(paths)
Example #7
0
    def input(self, _in, out, source_path, output_path):
        # Get source and output path relative to media directory (they are
        # probably absolute paths, we need to work with them as env.url
        # based urls (e.g. the following code will consider them absolute
        # within a filesystem chrooted into env.url).
        root = addsep(self.env.directory)
        # To make common_path_prefix() work properly in all cases, make sure
        # we remove stuff like ../ from all paths.
        output_path = normpath(join(root, output_path))
        source_path = normpath(join(root, source_path))
        root = normpath(root)

        output_url = path2url(output_path[len(common_path_prefix([root, output_path])):])
        source_url = path2url(source_path[len(common_path_prefix([root, source_path])):])

        # For replace mode, make sure we have all the directories to be
        # rewritten in form of a url, so we can later easily match it
        # against the urls encountered in the CSS.
        replace = False
        if self.replace not in (False, None):
            replace = OrderedDict()
            for repldir, sub in self.replace.items():
                repldir = addsep(os.path.normpath(join(root, repldir)))
                replurl = path2url(repldir[len(common_path_prefix([root, repldir])):])
                replace[replurl] = sub

        def _rewrite(m):
            # Get the regex matches; note how we maintain the exact
            # whitespace around the actual url; we'll indeed only
            # replace the url itself.
            text_before = m.groups()[0]
            url = m.groups()[1]
            text_after = m.groups()[2]

            # normalize the url we got
            quotes_used = ''
            if url[:1] in '"\'':
                quotes_used = url[:1]
                url = url[1:]
            if url[-1:] in '"\'':
                url = url[:-1]

            # Replace mode: manually adjust the location of files
            if replace is not False:
                for to_replace, sub in replace.items():
                    targeturl = urlparse.urljoin(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
                if not url.startswith('/') and not (url.startswith('http://') or url.startswith('https://')):
                    # rewritten url: relative path from new location (output)
                    # to location of referenced file (source + current url)
                    url = urlpath.relpath(output_url,
                                          urlparse.urljoin(source_url, url))

            result = 'url(%s%s%s%s%s)' % (
                        text_before, quotes_used, url, quotes_used, text_after)
            return result

        out.write(urltag_re.sub(_rewrite, _in.read()))