当前位置: 首页>>代码示例>>Python>>正文


Python Document.locale_and_slug_from_path方法代码示例

本文整理汇总了Python中wiki.models.Document.locale_and_slug_from_path方法的典型用法代码示例。如果您正苦于以下问题:Python Document.locale_and_slug_from_path方法的具体用法?Python Document.locale_and_slug_from_path怎么用?Python Document.locale_and_slug_from_path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在wiki.models.Document的用法示例。


在下文中一共展示了Document.locale_and_slug_from_path方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __iter__

# 需要导入模块: from wiki.models import Document [as 别名]
# 或者: from wiki.models.Document import locale_and_slug_from_path [as 别名]
    def __iter__(self):
        from wiki.models import Document

        input = html5lib_Filter.__iter__(self)

        # Pass #1: Gather all the link URLs and prepare annotations
        links = dict()
        buffer = []
        for token in input:
            buffer.append(token)
            if "StartTag" == token["type"] and "a" == token["name"]:
                attrs = dict(token["data"])
                if not "href" in attrs:
                    continue

                href = attrs["href"]
                if href.startswith(self.base_url):
                    # Squash site-absolute URLs to site-relative paths.
                    href = "/%s" % href[len(self.base_url) :]

                # Prepare annotations record for this path.
                links[href] = dict(classes=[])

        # Run through all the links and check for annotatable conditions.
        for href in links.keys():

            # Is this an external URL?
            is_external = False
            for prefix in self.EXTERNAL_PREFIXES:
                if href.startswith(prefix):
                    is_external = True
                    break
            if is_external:
                links[href]["classes"].append("external")
                continue

            # TODO: Should this also check for old-school mindtouch URLs? Or
            # should we encourage editors to convert to new-style URLs to take
            # advantage of link annotation? (I'd say the latter)

            # Is this a kuma doc URL?
            if "/docs/" in href:

                # Check if this is a special docs path that's exempt from "new"
                skip = False
                for path in DOC_SPECIAL_PATHS:
                    if "/docs/%s" % path in href:
                        skip = True
                if skip:
                    continue

                href_locale, href_path = href.split(u"/docs/", 1)
                if href_locale.startswith(u"/"):
                    href_locale = href_locale[1:]

                if "#" in href_path:
                    # If present, discard the hash anchor
                    href_path, _, _ = href_path.partition("#")

                # Handle any URL-encoded UTF-8 characters in the path
                href_path = href_path.encode("utf-8", "ignore")
                href_path = urllib.unquote(href_path)
                href_path = href_path.decode("utf-8", "ignore")

                # Try to sort out the locale and slug through some of our
                # redirection logic.
                locale, slug, needs_redirect = Document.locale_and_slug_from_path(href_path, path_locale=href_locale)

                # Does this locale and slug correspond to an existing document?
                # If not, mark it as a "new" link.
                #
                # TODO: Should these DB queries be batched up into one big
                # query? A page with hundreds of links will fire off hundreds
                # of queries
                ct = Document.objects.filter(locale=locale, slug=slug).count()
                if ct == 0:
                    links[href]["classes"].append("new")

        # Pass #2: Filter the content, annotating links
        for token in buffer:
            if "StartTag" == token["type"] and "a" == token["name"]:
                attrs = dict(token["data"])

                if "href" in attrs:

                    href = attrs["href"]
                    if href.startswith(self.base_url):
                        # Squash site-absolute URLs to site-relative paths.
                        href = "/%s" % href[len(self.base_url) :]

                    if href in links:
                        # Update class names on this link element.
                        if "class" in attrs:
                            classes = set(attrs["class"].split(u" "))
                        else:
                            classes = set()
                        classes.update(links[href]["classes"])
                        if classes:
                            attrs["class"] = u" ".join(classes)

#.........这里部分代码省略.........
开发者ID:KKDeep,项目名称:kuma,代码行数:103,代码来源:content.py


注:本文中的wiki.models.Document.locale_and_slug_from_path方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。