コード例 #1
0
 def my_compile_string(self, template_string, origin, libraries=[]):
     "Compiles template_string into NodeList ready for rendering"
     lexer = template.Lexer(template_string, origin)
     parser = template.Parser(lexer.tokenize())
     for lib in libraries:
         parser.add_library(lib)
     return parser.parse()
コード例 #2
0
ファイル: features.py プロジェクト: wujcheng/nav
def compile_template_with_filters(template_string, filters):
    """Compile a Django template, using additional filters.

    This is like Template(template_string) except that additional
    filters to be made available to the template may be specified.

    Normally, one would define filters as documented in [1], but this
    requires the INSTALLED_APPS settings to be set, which is not the
    case in NAV[2]. This function is just a hack to get around that
    limitation. The code is based on
    django.template.compile_string[3].

    filters should be a dictionary mapping filter names to functions.

    [1]: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
    [2]: https://nav.uninett.no/wiki/devel:django_introduction#settings
    [3]: http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py

    """
    lib = template.Library()
    for name in filters.keys():
        lib.filter(name, filters[name])
    lexer = template.Lexer(template_string, None)
    parser = template.Parser(lexer.tokenize())
    parser.add_library(lib)
    return parser.parse()
コード例 #3
0
ファイル: sitetreeapp.py プロジェクト: Uznick/django-sitetree
    def parse_titles(self, items, context=None):
        """Walks through the list of items, and resolves any variable found
        in a title of an item.

        We deliberately strip down template tokens that are not text or variable.
        There is definitely no need nor purpose in blocks or comments in a title.
        Why to think otherwise, if only you're a villain. Joke here.

        Returns the list with resolved titles.

        """
        if context is None:
            context = self.global_context

        for item in items:
            if item.title.find(template.VARIABLE_TAG_START) != -1:
                my_lexer = template.Lexer(item.title, template.UNKNOWN_SOURCE)
                my_tokens = my_lexer.tokenize()

                for my_token in my_tokens:
                    if my_token.token_type not in (template.TOKEN_TEXT,
                                                   template.TOKEN_VAR):
                        my_tokens.remove(my_token)

                my_parser = template.Parser(my_tokens)
                item.title_resolved = my_parser.parse().render(context)

        return items
コード例 #4
0
def parse_file(filename, printable_filename=None):
    with io.open(filename, 'r', encoding='utf-8') as sample_file:
        file_content = sample_file.read()
    lexer = template.Lexer(file_content, printable_filename)
    tokens = lexer.tokenize()
    parser = MessageParser(tokens)
    return parser.parse()
コード例 #5
0
    def __unicode__(self):
        my_lexer = template.Lexer(self.title, template.UNKNOWN_SOURCE)
        my_tokens = my_lexer.tokenize()

        # Deliberately strip off template tokens that are not text or variable.
        for my_token in my_tokens:
            if my_token.token_type not in (template.TOKEN_TEXT, template.TOKEN_VAR):
                my_tokens.remove(my_token)

        my_parser = template.Parser(my_tokens)
        return my_parser.parse().render(SiteTree.get_global_context())
コード例 #6
0
def parse_template(content):
    lexer = template.Lexer(content, '')
    parser = template.Parser(lexer.tokenize())
    token = parser.next_token()
    return parser, token