Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    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