Beispiel #1
0
    def hostname_or_filename(self):
        """
        Return the hostname or filename of this document.

        Returns the hostname part of the document's URI, e.g.
        'www.example.com' for 'http://www.example.com/example.html'.

        If the URI is a file:// URI then return the filename part of it
        instead.

        The returned hostname or filename is escaped and safe to be rendered.

        If it contains escaped characters the returned value will be a Markup
        object so that it doesn't get double-escaped.

        """
        if self.filename:
            return jinja2.escape(url_unquote(self.filename))
        else:
            hostname = urlparse.urlparse(self.uri).hostname

            # urlparse()'s .hostname is sometimes None.
            hostname = hostname or ''

            return jinja2.escape(hostname)
Beispiel #2
0
    def hostname_or_filename(self):
        """
        Return the hostname or filename of this document.

        Returns the hostname part of the document's URI, e.g.
        'www.example.com' for 'http://www.example.com/example.html'.

        If the URI is a file:// URI then return the filename part of it
        instead.

        The returned hostname or filename is escaped and safe to be rendered.

        If it contains escaped characters the returned value will be a Markup
        object so that it doesn't get double-escaped.

        """
        if self.filename:
            return jinja2.escape(url_unquote(self.filename))
        else:
            hostname = urlparse.urlparse(self.uri).hostname

            # urlparse()'s .hostname is sometimes None.
            hostname = hostname or ''

            return jinja2.escape(hostname)
Beispiel #3
0
    def link_text(self):
        """
        Return some link text for this document.

        Return a text representation of this document suitable for use as the
        link text in a link like <a ...>{link_text}</a>.

        Returns the document's title if it has one, or failing that uses part
        of the document's URI if it has one.

        The link text is escaped and safe for rendering.

        If it contains escaped characters the returned value will be a
        Markup object so it doesn't get double-escaped.

        """
        title = jinja2.escape(self.title)

        # Sometimes self.title is the annotated document's URI (if the document
        # has no title). In those cases we want to remove the http(s):// from
        # the front and unquote it for link text.
        lower = title.lower()
        if lower.startswith('http://') or lower.startswith('https://'):
            parts = urlparse.urlparse(title)
            return url_unquote(parts.netloc + parts.path)

        else:
            return title
Beispiel #4
0
    def link_text(self):
        """
        Return some link text for this document.

        Return a text representation of this document suitable for use as the
        link text in a link like <a ...>{link_text}</a>.

        Returns the document's title if it has one, or failing that uses part
        of the document's URI if it has one.

        The link text is escaped and safe for rendering.

        If it contains escaped characters the returned value will be a
        Markup object so it doesn't get double-escaped.

        """
        title = jinja2.escape(self.title)

        # Sometimes self.title is the annotated document's URI (if the document
        # has no title). In those cases we want to remove the http(s):// from
        # the front and unquote it for link text.
        lower = title.lower()
        if lower.startswith('http://') or lower.startswith('https://'):
            parts = urlparse.urlparse(title)
            return url_unquote(parts.netloc + parts.path)

        else:
            return title
Beispiel #5
0
def pretty_link(url):
    """
    Return a nicely formatted version of a URL.

    This strips off 'visual noise' from the URL including common schemes
    (HTTP, HTTPS), domain prefixes ('www.') and query strings.
    """
    parsed = urlparse.urlparse(url)
    if parsed.scheme not in ['http', 'https']:
        return url
    netloc = parsed.netloc
    if netloc.startswith('www.'):
        netloc = netloc[4:]
    return url_unquote(netloc + parsed.path)
Beispiel #6
0
def pretty_link(url):
    """
    Return a nicely formatted version of a URL.

    This strips off 'visual noise' from the URL including common schemes
    (HTTP, HTTPS), domain prefixes ('www.') and query strings.
    """
    parsed = urlparse.urlparse(url)
    if parsed.scheme not in ['http', 'https']:
        return url
    netloc = parsed.netloc
    if netloc.startswith('www.'):
        netloc = netloc[4:]
    return url_unquote(netloc + parsed.path)
Beispiel #7
0
    def title(self):
        """
        Return a title for this document.

        Return the document's title or if the document has no title then return
        its filename (if it's a file:// URI) or its URI for non-file URIs.

        The title is escaped and safe to be rendered.

        If it contains escaped characters then the title will be a
        Markup object, so that it won't be double-escaped.

        """
        title = self.document.title
        if title:
            # Convert non-string titles into strings.
            # We're assuming that title cannot be a byte string.
            title = text_type(title)
            return jinja2.escape(title)

        if self.filename:
            return jinja2.escape(url_unquote(self.filename))
        else:
            return jinja2.escape(url_unquote(self.uri))
Beispiel #8
0
    def title(self):
        """
        Return a title for this document.

        Return the document's title or if the document has no title then return
        its filename (if it's a file:// URI) or its URI for non-file URIs.

        The title is escaped and safe to be rendered.

        If it contains escaped characters then the title will be a
        Markup object, so that it won't be double-escaped.

        """
        title = self.document.title
        if title:
            # Convert non-string titles into strings.
            # We're assuming that title cannot be a byte string.
            title = text_type(title)
            return jinja2.escape(title)

        if self.filename:
            return jinja2.escape(url_unquote(self.filename))
        else:
            return jinja2.escape(url_unquote(self.uri))
Beispiel #9
0
def _normalize_pathsegment(segment):
    return url_quote(url_unquote(segment), safe=UNRESERVED_PATHSEGMENT)
Beispiel #10
0
Datei: uri.py Projekt: JJediny/h
def _normalize_pathsegment(segment):
    return url_quote(url_unquote(segment), safe=UNRESERVED_PATHSEGMENT)