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


Python Token.collapse_nodes_of_class方法代码示例

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


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

示例1: parse

# 需要导入模块: from template_preprocessor.core.lexer import Token [as 别名]
# 或者: from template_preprocessor.core.lexer.Token import collapse_nodes_of_class [as 别名]
def parse(source_code, path, context, main_template=False):
    """
    Parse the code.
    - source_code: string
    - path: for attaching meta information to the tree.
    - context: preprocess context (holding the settings/dependecies/warnings, ...)
    - main_template: False for includes/extended templates. True for the
                     original path that was called.
    """
    # To start, create the root node of a tree.
    tree = Token(name="root", line=1, column=1, path=path)
    tree.children = [source_code]

    # Lex Django tags
    tokenize(tree, __DJANGO_STATES, Token)

    # Phase I: add parser extensions
    _add_parser_extensions(tree)

    # Phase II: process inline tags
    _process_inline_tags(tree)

    # Phase III: create recursive structure for block level tags.
    nest_block_level_elements(tree, __DJANGO_BLOCK_ELEMENTS, DjangoTag, lambda c: c.tagname)

    # === Actions ===

    if main_template:
        _find_first_level_dependencies(tree, context)

    # Extend parent template and process includes
    tree = _process_extends(tree, context)  # NOTE: this returns a new tree!
    _preprocess_includes(tree, context)
    _preprocess_decorate_tags(tree, context)

    # Following actions only need to be applied if this is the 'main' tree.
    # It does not make sense to apply it on every include, and then again
    # on the complete tree.
    if main_template:

        _update_preprocess_settings(tree, context)
        options = context.options

        # Remember translations in context (form PO-file generation)
        remember_gettext_entries(tree, context)

        # Do translations
        if options.preprocess_translations:
            _preprocess_trans_tags(tree)

        # Reverse URLS
        if options.preprocess_urls:
            _preprocess_urls(tree)

        # Do variable lookups
        if options.preprocess_variables:
            sites_enabled = "django.contrib.sites" in settings.INSTALLED_APPS

            _preprocess_variables(
                tree,
                {"MEDIA_URL": getattr(settings, "MEDIA_URL", ""), "STATIC_URL": getattr(settings, "STATIC_URL", "")},
            )
            if sites_enabled:
                from django.contrib.sites.models import Site

                try:
                    # Don't preprocess anything when we don't have a Site
                    # instance yet.
                    site = Site.objects.get_current()
                    _preprocess_variables(
                        tree,
                        {"SITE_DOMAIN": site.domain, "SITE_NAME": site.name, "SITE_URL": "http://%s" % site.domain},
                    )
                except Site.DoesNotExist, e:
                    pass

        # Don't output {% block %} tags in the compiled file.
        if options.remove_block_tags:
            tree.collapse_nodes_of_class(DjangoBlockTag)

        # Preprocess {% callmacro %} tags
        if options.preprocess_macros:
            _preprocess_macros(tree)

        # Group all {% load %} statements
        if options.merge_all_load_tags:
            _group_all_loads(tree)

        # Preprocessable tags
        if options.execute_preprocessable_tags:
            _execute_preprocessable_tags(tree)

        # HTML compiler
        if options.is_html:
            compile_html(tree, context)
开发者ID:sentido,项目名称:django-template-preprocessor,代码行数:97,代码来源:django_processor.py

示例2: parse

# 需要导入模块: from template_preprocessor.core.lexer import Token [as 别名]
# 或者: from template_preprocessor.core.lexer.Token import collapse_nodes_of_class [as 别名]
def parse(source_code, path, loader, main_template=False, options=None):
    """
    Parse the code.
    - source_code: string
    - loader: method to be called to include other templates.
    - path: for attaching meta information to the tree.
    - main_template: False for includes/extended templates. True for the
                     original path that was called.
    """
    # To start, create the root node of a tree.
    tree = Token(name='root', line=1, column=1, path=path)
    tree.children = [ source_code ]

    # Lex Django tags
    tokenize(tree, __DJANGO_STATES, [Token])

    # Phase I: add parser extensions
    _add_parser_extensions(tree)

    # Phase II: process inline tags
    _process_inline_tags(tree)

    # Phase III: create recursive structure for block level tags.
    nest_block_level_elements(tree, __DJANGO_BLOCK_ELEMENTS, [DjangoTag], lambda c: c.tagname)

    # === Actions ===

    # Extend parent template and process includes
    tree = _process_extends(tree, loader) # NOTE: this returns a new tree!
    _preprocess_includes(tree, loader)
    _preprocess_decorate_tags(tree, loader)


    # Following actions only need to be applied if this is the 'main' tree.
    # It does not make sense to apply it on every include, and then again
    # on the complete tree.
    if main_template:
        options = _get_preprocess_settings(tree, options)

        # Do translations
        if options.preprocess_translations:
            _preprocess_trans_tags(tree)

        # Reverse URLS
        if options.preprocess_urls:
            _preprocess_urls(tree)

        # Do variable lookups
        if options.preprocess_variables:
            from django.contrib.sites.models import Site
            _preprocess_variables(tree,
                        {
                            'MEDIA_URL': settings.MEDIA_URL,
                            'SITE_DOMAIN': Site.objects.get_current().domain,
                            'SITE_NAME': Site.objects.get_current().name,
                            'SITE_URL': 'http://%s' % Site.objects.get_current().domain,
                        })

        # Don't output {% block %} tags in the compiled file.
        if options.remove_block_tags:
            tree.collapse_nodes_of_class(DjangoBlockTag)

        # Preprocess {% callmacro %} tags
        if options.preprocess_macros:
            _preprocess_macros(tree)

        if options.preprocess_ifdebug:
            _preprocess_ifdebug(tree)

        # Group all {% load %} statements
        if options.merge_all_load_tags:
            _group_all_loads(tree)

        # Preprocessable tags
        if options.execute_preprocessable_tags:
            _execute_preprocessable_tags(tree)

        # HTML compiler
        if options.is_html:
            compile_html(tree, options)

    return tree
开发者ID:bryanveloso,项目名称:django-template-preprocessor,代码行数:84,代码来源:django_processor.py


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