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


Python parser.Parser类代码示例

本文整理汇总了Python中jinja2.parser.Parser的典型用法代码示例。如果您正苦于以下问题:Python Parser类的具体用法?Python Parser怎么用?Python Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: render_str

def render_str(src, **kw):
    from jinja2.environment import Environment
    env = Environment()
    parsed = Parser(env, src)
    gen = JsGenerator(env, '<internal>', '<internal>')
    gen.visit(parsed.parse())

    return render(source=gen.stream.getvalue(), **kw)
开发者ID:muromec,项目名称:jscrap,代码行数:8,代码来源:phrender.py

示例2: compile_expression

    def compile_expression(self, source, undefined_to_none = True):
        parser = Parser(self, source, state='variable')
        exc_info = None
        try:
            expr = parser.parse_expression()
            if not parser.stream.eos:
                raise TemplateSyntaxError('chunk after expression', parser.stream.current.lineno, None, None)
            expr.set_environment(self)
        except TemplateSyntaxError:
            exc_info = sys.exc_info()

        if exc_info is not None:
            self.handle_exception(exc_info, source_hint=source)
        body = [nodes.Assign(nodes.Name('result', 'store'), expr, lineno=1)]
        template = self.from_string(nodes.Template(body, lineno=1))
        return TemplateExpression(template, undefined_to_none)
开发者ID:Pluckyduck,项目名称:eve,代码行数:16,代码来源:environment.py

示例3: compile_expression

    def compile_expression(self, source, undefined_to_none=True):
        """A handy helper method that returns a callable that accepts keyword
        arguments that appear as variables in the expression.  If called it
        returns the result of the expression.

        This is useful if applications want to use the same rules as Jinja
        in template "configuration files" or similar situations.

        Example usage:

        >>> env = Environment()
        >>> expr = env.compile_expression('foo == 42')
        >>> expr(foo=23)
        False
        >>> expr(foo=42)
        True

        Per default the return value is converted to `None` if the
        expression returns an undefined value.  This can be changed
        by setting `undefined_to_none` to `False`.

        >>> env.compile_expression('var')() is None
        True
        >>> env.compile_expression('var', undefined_to_none=False)()
        Undefined

        .. versionadded:: 2.1
        """
        parser = Parser(self, source, state='variable')
        exc_info = None
        try:
            expr = parser.parse_expression()
            if not parser.stream.eos:
                raise TemplateSyntaxError('chunk after expression',
                                          parser.stream.current.lineno,
                                          None, None)
            expr.set_environment(self)
        except TemplateSyntaxError:
            exc_info = sys.exc_info()
        if exc_info is not None:
            self.handle_exception(exc_info, source_hint=source)
        body = [nodes.Assign(nodes.Name('result', 'store'), expr, lineno=1)]
        template = self.from_string(nodes.Template(body, lineno=1))
        return TemplateExpression(template, undefined_to_none)
开发者ID:AJH693,项目名称:jinja2,代码行数:44,代码来源:environment.py

示例4: dump_tpl

def dump_tpl(**ctx):
    """
    Here I`m compiling template into js code
    and dumping it to browser.

    Template selected by function "splash" which is 
    view function for "/".

    This handler called every time, somebody GETs /?js
    """
    from jinja2.parser import Parser
    from jscrap.generator import JsGenerator

    ret = []
    for tpl in env.loader.list_templates():
        source,_,_ = env.loader.get_source(env, tpl)
        code = Parser(env, source)
        gen = JsGenerator(env, tpl, tpl)
        gen.visit(code.parse())
        ret.append(gen.stream.getvalue())

    return ret, 'text/plain'
开发者ID:muromec,项目名称:jscrap-demo,代码行数:22,代码来源:main.py

示例5: compile_expression

    def compile_expression(self, source, undefined_to_none=True):
        """A handy helper method that returns a callable that accepts keyword
        arguments that appear as variables in the expression.  If called it
        returns the result of the expression.

        This is useful if applications want to use the same rules as Jinja
        in template "configuration files" or similar situations.

        Example usage:

        >>> env = Environment()
        >>> expr = env.compile_expression('foo == 42')
        >>> expr(foo=23)
        False
        >>> expr(foo=42)
        True

        Per default the return value is converted to `None` if the
        expression returns an undefined value.  This can be changed
        by setting `undefined_to_none` to `False`.

        >>> env.compile_expression('var')() is None
        True
        >>> env.compile_expression('var', undefined_to_none=False)()
        Undefined

        **new in Jinja 2.1**
        """
        parser = Parser(self, source, state='variable')
        try:
            expr = parser.parse_expression()
            if not parser.stream.eos:
                raise TemplateSyntaxError('chunk after expression',
                                          parser.stream.current.lineno,
                                          None, None)
        except TemplateSyntaxError, e:
            e.source = source
            raise e
开发者ID:FriedWishes,项目名称:drydrop,代码行数:38,代码来源:environment.py

示例6: Environment

from jinja2.lexer import Lexer, compile_rules
from jinja2.environment import Environment
from jinja2.parser import Parser
from jinja2 import nodes
import re


env = Environment()
le = Lexer(env)
source = r'''{% for user in users %}\n<html>\n    Hello {{ user }}\n</html>\n{% endfor %}'''
pas = Parser(env, source)
token = le.tokenize(source)

for x in token:
    print((x.lineno, x.type, x.value))
m = pas.subparse()
print(m)

result = nodes.Template(m, lineno=1)
print(result)

rex = r'(?P<raw_begin>(?:\{\%\+?)\s*raw\s*(?:\%\}\+?))'
s = '{% raw %}'
rec = re.compile(rex)
m = rec.match(s)
print(m.groupdict())

rule = compile_rules(env)
print(rule)

from jinja2 import _stringdefs
开发者ID:tyrrrr1987,项目名称:ybtest,代码行数:31,代码来源:bb.py

示例7: js_compile

 def js_compile(self, source, name):
     code = Parser(self.env, source)
     gen = JsGenerator(self.env, name, name)
     gen.visit(code.parse())
     return gen.stream.getvalue()
开发者ID:muromec,项目名称:jscrap,代码行数:5,代码来源:phrender.py

示例8: jinja_compile

 def jinja_compile(source, name, generator):
     code = Parser(env, source)
     gen = generator(env, name, name)
     gen.visit(code.parse())
     return gen.stream.getvalue()
开发者ID:muromec,项目名称:jscrap,代码行数:5,代码来源:generator.py


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