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)
def parse(self, parser): """Execute parsing of md token and return nodes.""" kwargs = None expected_heading_level = None count = 0 while parser.stream.current.type != lexer.TOKEN_BLOCK_END: count = count + 1 if count > self.max_tag_parse: raise err.TrestleError( 'Unexpected Jinja tag structure provided, please review docs.' ) token = parser.stream.current if token.test('name:mdsection_include'): parser.stream.expect(lexer.TOKEN_NAME) markdown_source = parser.stream.expect(lexer.TOKEN_STRING) section_title = parser.stream.expect(lexer.TOKEN_STRING) elif kwargs is not None: arg = token.value next(parser.stream) parser.stream.expect(lexer.TOKEN_ASSIGN) token = parser.stream.current exp = self.parse_expression(parser) kwargs[arg] = exp.value else: if parser.stream.look().type == lexer.TOKEN_ASSIGN: kwargs = {} continue # Use the established environment to source the file md_content, _, _ = self.environment.loader.get_source( self.environment, markdown_source.value) fm = frontmatter.loads(md_content) if not fm.metadata == {}: logger.warning( 'Non zero metadata on MD section include - ignoring') full_md = markdown_node.MarkdownNode.build_tree_from_markdown( fm.content.split('\n')) md_section = full_md.get_node_for_key(section_title.value, strict_matching=True) # adjust if kwargs is not None: expected_heading_level = kwargs.get('heading_level') if expected_heading_level is not None: level = md_section.get_node_header_lvl() delta = int(expected_heading_level) - level if not delta == 0: md_section.change_header_level_by(delta) if not md_section: raise err.TrestleError( f'Unable to retrieve section "{section_title.value}"" from {markdown_source.value} jinja template.' ) local_parser = Parser(self.environment, md_section.content.raw_text) top_level_output = local_parser.parse() return top_level_output.body
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)
def include_relative(token: Token, parser: Parser) -> nodes.Node: """The {% include_relative ... %} tag""" node = nodes.Include(lineno=token.lineno) path = parser.parse_expression() if parser.stream.filename: node.template = nodes.Add( nodes.Add( nodes.Const(os.path.dirname(parser.stream.filename)), nodes.Const(os.path.sep), ), path, ) else: node.template = path node.ignore_missing = False return parser.parse_import_context(node, True)
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)
def parse(self, parser): """Execute parsing of md token and return nodes.""" kwargs = None count = 0 while parser.stream.current.type != lexer.TOKEN_BLOCK_END: count = count + 1 token = parser.stream.current if count > self.max_tag_parse: raise err.TrestleError( f'Unexpected Jinja tag structure provided at token {token.value}' ) if token.test('name:md_datestamp'): parser.stream.expect(lexer.TOKEN_NAME) elif kwargs is not None: arg = token.value next(parser.stream) parser.stream.expect(lexer.TOKEN_ASSIGN) token = parser.stream.current exp = self.parse_expression(parser) kwargs[arg] = exp.value else: if parser.stream.look( ).type == lexer.TOKEN_ASSIGN or parser.stream.look( ).type == lexer.TOKEN_STRING: kwargs = {} continue if kwargs is not None: if 'format' in kwargs and type(kwargs['format'] is str): date_string = date.today().strftime(kwargs['format']) else: date_string = date.today().strftime( markdown_const.JINJA_DATESTAMP_FORMAT) if 'newline' in kwargs and kwargs['newline'] is False: pass else: date_string += '\n\n' else: date_string = date.today().strftime( markdown_const.JINJA_DATESTAMP_FORMAT) + '\n\n' local_parser = Parser(self.environment, date_string) datestamp_output = local_parser.parse() return datestamp_output.body
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)
def parse(self, parser: Parser) -> CallBlock: token = next(parser.stream) tag = token.value lineno = token.lineno # now we parse body of the block body = parser.parse_statements( ["name:enddeferJS", "name:enddeferredJS"], drop_needle=True) method = "defer_nodes" if tag == "deferJS" else "collect_deferred" return nodes.CallBlock(self.call_method(method, []), [], [], body).set_lineno(lineno)
def parse(self, parser: Parser) -> nodes.Output: lineno = next(parser.stream).lineno arg = parser.parse_expression() call_method = self.call_method( "_time", [arg], lineno=lineno, ) return nodes.Output([call_method], lineno=lineno)
def parse(self, parser): """Execute parsing of md token and return nodes.""" kwargs = None expected_heading_level = None count = 0 while parser.stream.current.type != lexer.TOKEN_BLOCK_END: count = count + 1 if count > self.max_tag_parse: raise err.TrestleError( 'Unexpected Jinja tag structure provided, please review docs.' ) token = parser.stream.current if token.test('name:md_clean_include'): parser.stream.expect(lexer.TOKEN_NAME) markdown_source = parser.stream.expect(lexer.TOKEN_STRING) elif kwargs is not None: arg = token.value next(parser.stream) parser.stream.expect(lexer.TOKEN_ASSIGN) token = parser.stream.current exp = self.parse_expression(parser) kwargs[arg] = exp.value else: if parser.stream.look().type == lexer.TOKEN_ASSIGN: kwargs = {} continue md_content, _, _ = self.environment.loader.get_source( self.environment, markdown_source.value) fm = frontmatter.loads(md_content) content = fm.content content += '\n\n' if kwargs is not None: expected_heading_level = kwargs.get('heading_level') if expected_heading_level is not None: content = adjust_heading_level(content, expected_heading_level) local_parser = Parser(self.environment, content) top_level_output = local_parser.parse() return top_level_output.body
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'
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
def parse(self, source, name=None, filename=None): """Parse the sourcecode and return the abstract syntax tree. This tree of nodes is used by the compiler to convert the template into executable source- or bytecode. This is useful for debugging or to extract information from templates. If you are :ref:`developing Jinja2 extensions <writing-extensions>` this gives you a good overview of the node tree generated. """ if isinstance(filename, unicode): filename = filename.encode('utf-8') try: return Parser(self, source, name, filename).parse() except TemplateSyntaxError: self.handle_exception(sys.exc_info(), source_hint=source)
def parse(self, source, name=None, filename=None): """Parse the sourcecode and return the abstract syntax tree. This tree of nodes is used by the compiler to convert the template into executable source- or bytecode. This is useful for debugging or to extract information from templates. If you are :ref:`developing Jinja2 extensions <writing-extensions>` this gives you a good overview of the node tree generated. """ if isinstance(filename, unicode): filename = filename.encode('utf-8') try: return Parser(self, source, name, filename).parse() except TemplateSyntaxError, e: from jinja2.debug import translate_syntax_error exc_type, exc_value, tb = translate_syntax_error(e) raise exc_type, exc_value, tb
def _parse(self, source, name, filename): return Parser(self, source, name, _encode_filename(filename)).parse()
def _parse(self, source, name, filename): """Internal parsing function used by `parse` and `compile`.""" return Parser(self, source, name, _encode_filename(filename)).parse()
def _parse(self, source, name, filename): """Internal parsing function used by `parse` and `compile`.""" if isinstance(filename, unicode): filename = filename.encode('utf-8') return Parser(self, source, name, filename).parse()
def jinja_compile(source, name, generator): code = Parser(env, source) gen = generator(env, name, name) gen.visit(code.parse()) return gen.stream.getvalue()
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
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()