Ejemplo n.º 1
0
def resolve_image(record, src):
    if record is None:
        return None
    url = urlparse(src)
    if url.scheme or url.netloc:
        return None

    path = url.path
    if not path.startswith('/'):
        assert record.path is not None  # FIXME: better error reporting?
        path = join_path(record.path, path)
    source = record.pad.get(path)
    if isinstance(source, Image):
        return source
Ejemplo n.º 2
0
    def url_to(self,
               path,
               alt=None,
               absolute=None,
               external=None,
               base_url=None):
        """Calculates the URL from the current source object to the given
        other source object.  Alternatively a path can also be provided
        instead of a source object.  If the path starts with a leading
        bang (``!``) then no resolving is performed.

        If a `base_url` is provided then it's used instead of the URL of
        the record itself.
        """
        if alt is None:
            alt = getattr(path, "alt", None)
            if alt is None:
                alt = self.alt

        resolve = True
        path = getattr(path, "url_path", path)
        if path[:1] == "!":
            resolve = False
            path = path[1:]

        if resolve:
            if not path.startswith("/"):
                if self.path is None:
                    raise RuntimeError("Cannot use relative URL generation "
                                       "from sources that do not have a "
                                       "path.  The source object without "
                                       "a path is %r" % self)
                path = join_path(self.path, path)
            source = self.pad.get(path, alt=alt)
            if source is not None:
                path = source.url_path
        else:
            path = posixpath.join(self.url_path, path)

        if absolute:
            return path
        if base_url is None:
            base_url = self.url_path
        return self.pad.make_url(path, base_url, absolute, external)
Ejemplo n.º 3
0
    def url_to(self, path, alt=None, absolute=None, external=None,
               base_url=None):
        """Calculates the URL from the current source object to the given
        other source object.  Alternatively a path can also be provided
        instead of a source object.  If the path starts with a leading
        bang (``!``) then no resolving is performed.

        If a `base_url` is provided then it's used instead of the URL of
        the record itself.
        """
        if alt is None:
            alt = getattr(path, 'alt', None)
            if alt is None:
                alt = self.alt

        resolve = True
        path = getattr(path, 'url_path', path)
        if path[:1] == '!':
            resolve = False
            path = path[1:]

        if resolve:
            if not path.startswith('/'):
                if self.path is None:
                    raise RuntimeError('Cannot use relative URL generation '
                                       'from sources that do not have a '
                                       'path.  The source object without '
                                       'a path is %r' % self)
                path = join_path(self.path, path)
            source = self.pad.get(path, alt=alt)
            if source is not None:
                path = source.url_path
        else:
            path = posixpath.join(self.url_path, path)

        if absolute:
            return path
        if base_url is None:
            base_url = self.url_path
        return self.pad.make_url(path, base_url, absolute, external)
Ejemplo n.º 4
0
def test_join_path():
    from lektor.utils import join_path

    assert join_path('a', 'b') == 'a/b'
    assert join_path('/a', 'b') == '/a/b'
    assert join_path('a@b', 'c') == 'a@b/c'
    assert join_path('a@b', '') == 'a@b'
    assert join_path('a@b', '@c') == 'a@c'
    assert join_path('a@b/c', 'a@b') == 'a/a@b'

    assert join_path('blog@archive', '2015') == 'blog@archive/2015'
    assert join_path('blog@archive/2015', '..') == 'blog@archive'
    assert join_path('blog@archive/2015', '@archive') == 'blog@archive'
    assert join_path('blog@archive', '..') == 'blog'
    assert join_path('blog@archive', '.') == 'blog@archive'
    assert join_path('blog@archive', '') == 'blog@archive'

    # special behavior: parent of pagination paths is always the actual
    # page parent.
    assert join_path('/blog@1', '..') == '/'
    assert join_path('/blog@2', '..') == '/'

    # But joins on the same level keep the path
    assert join_path('/blog@1', '.') == '/blog@1'
    assert join_path('/blog@2', '.') == '/blog@2'
    assert join_path('/blog@1', '') == '/blog@1'
    assert join_path('/blog@2', '') == '/blog@2'
Ejemplo n.º 5
0
def test_join_path():
    from lektor.utils import join_path

    assert join_path("a", "b") == "a/b"
    assert join_path("/a", "b") == "/a/b"
    assert join_path("a@b", "c") == "a@b/c"
    assert join_path("a@b", "") == "a@b"
    assert join_path("a@b", "@c") == "a@c"
    assert join_path("a@b/c", "a@b") == "a/a@b"

    assert join_path("blog@archive", "2015") == "blog@archive/2015"
    assert join_path("blog@archive/2015", "..") == "blog@archive"
    assert join_path("blog@archive/2015", "@archive") == "blog@archive"
    assert join_path("blog@archive", "..") == "blog"
    assert join_path("blog@archive", ".") == "blog@archive"
    assert join_path("blog@archive", "") == "blog@archive"

    # special behavior: parent of pagination paths is always the actual
    # page parent.
    assert join_path("/blog@1", "..") == "/"
    assert join_path("/blog@2", "..") == "/"

    # But joins on the same level keep the path
    assert join_path("/blog@1", ".") == "/blog@1"
    assert join_path("/blog@2", ".") == "/blog@2"
    assert join_path("/blog@1", "") == "/blog@1"
    assert join_path("/blog@2", "") == "/blog@2"
Ejemplo n.º 6
0
def test_join_path():
    from lektor.utils import join_path

    assert join_path('a', 'b') == 'a/b'
    assert join_path('/a', 'b') == '/a/b'
    assert join_path('a@b', 'c') == 'a@b/c'
    assert join_path('a@b', '') == 'a@b'
    assert join_path('a@b', '@c') == 'a@c'
    assert join_path('a@b/c', 'a@b') == 'a/a@b'

    assert join_path('blog@archive', '2015') == 'blog@archive/2015'
    assert join_path('blog@archive/2015', '..') == 'blog@archive'
    assert join_path('blog@archive/2015', '@archive') == 'blog@archive'
    assert join_path('blog@archive', '..') == 'blog'
    assert join_path('blog@archive', '.') == 'blog@archive'
    assert join_path('blog@archive', '') == 'blog@archive'

    # special behavior: parent of pagination paths is always the actual
    # page parent.
    assert join_path('/blog@1', '..') == '/'
    assert join_path('/blog@2', '..') == '/'

    # But joins on the same level keep the path
    assert join_path('/blog@1', '.') == '/blog@1'
    assert join_path('/blog@2', '.') == '/blog@2'
    assert join_path('/blog@1', '') == '/blog@1'
    assert join_path('/blog@2', '') == '/blog@2'