def test_python_expression(self): content = repr(expr.handle_text_node('${a\n+\n"}"}')) self.assertEqual(content, 'PythonExpression(a\n+\n"}")') content = repr(expr.handle_text_node('foo$a.baz["bar]"].xyz("ham)"))...')) expected = 'DynamicText([Text(foo), PythonExpression(a.baz["bar]"].xyz("ham)")), Text()...)])' self.assertEqual(content, expected)
def test_javascript_expression(self): # a divide operator content = repr(expr.handle_text_node('a ${1 / 0} /} }', expr_parser=javascript.parse_expression)) self.assertEqual(content, 'ComplexExpression([Text(a ), JavascriptExpression(1 / 0), Text( /} })])') # a regexp literal content = repr(expr.handle_text_node('a ${1 + / 0} /} }', expr_parser=javascript.parse_expression)) self.assertEqual(content, 'ComplexExpression([Text(a ), JavascriptExpression(1 + / 0} /), Text( })])')
def generate_attributes_for_node(self, dom_node, ir_node): attrs_node = self.grab_and_remove_control_attr(dom_node, 'attrs') attrs = [(k, handle_text_node(v)) for (k, v) in dom_node.attributes.items()] self.generate_attributes(ir_node, attrs=attrs, dynamic_attrs=attrs_node)
def generate_attributes_for_node(self, dom_node, ir_node): attrs_node = self.grab_and_remove_control_attr(dom_node, 'attrs') attrs = [ (k, handle_text_node(v, translatable=self.is_attr_translatable(k))) for (k, v) in dom_node.attributes.items() ] self.generate_attributes(ir_node, attrs=attrs, dynamic_attrs=attrs_node)
def test_javascript_expression(self): # a divide operator content = repr(expr.handle_text_node('a ${1 / 0} /} }', expr_parser=javascript.parse_expression)) self.assertEqual(content, 'DynamicText([Text(a ), JavascriptExpression(1 / 0), Text( /} })])') # a regexp literal content = repr(expr.handle_text_node('a ${1 + / 0} /} }', expr_parser=javascript.parse_expression)) self.assertEqual(content, 'DynamicText([Text(a ), JavascriptExpression(1 + / 0} /), Text( })])') content = repr(expr.handle_text_node('foo$a.baz["bar]"].xyz("ham)"))...', expr_parser=javascript.parse_expression)) expected = 'DynamicText([Text(foo), JavascriptExpression(a.baz["bar]"].xyz("ham)")), Text()...)])' self.assertEqual(content, expected)
def generate_ir_node(self, dom_node): node_t = dom_node.nodeType if node_t == Node.ELEMENT_NODE: return self.generate_element_node(dom_node) if node_t == Node.TEXT_NODE: ir_node = handle_text_node(dom_node.nodeValue, is_cdata=self.is_cdata) return ir_node if node_t == Node.COMMENT_NODE: ir_node = EscapedText(u'<!--' + dom_node.nodeValue + u'-->') return ir_node raise ValueError("Unhandled node type %d" % node_t)
def generate_ir_node(self, dom_node): node_t = dom_node.nodeType if node_t == Node.ELEMENT_NODE: return self.generate_element_node(dom_node) if node_t == Node.TEXT_NODE: ir_node = handle_text_node(dom_node.nodeValue, is_cdata=self.state['is_cdata']) return ir_node if node_t == Node.COMMENT_NODE: ir_node = EscapedText(u'<!--' + dom_node.nodeValue + u'-->') return ir_node raise ValueError("Unhandled node type %d" % node_t)
def test_python_expression(self): content = repr(expr.handle_text_node('${a\n+\n"}"}')) self.assertEqual(content, 'PythonExpression(a\n+\n"}")')
from __future__ import absolute_import, division, print_function, unicode_literals from tonnikala import expr from tonnikala.languages import javascript from tonnikala import parser from os import path from tonnikala.ir.generate import IRGenerator from tonnikala.languages.python.generator import Generator as PythonGenerator from tonnikala.languages.javascript.generator import Generator as JavascriptGenerator from tonnikala.loader import Loader print(repr(expr.handle_text_node('${a\n+\n"}"}'))) print(repr(expr.handle_text_node('asdfasdf${1 / 0} /} }', expr_parser=javascript.parse_expression))) print(repr(expr.handle_text_node('asdfasdf${1 + / 0} /} }', expr_parser=javascript.parse_expression))) template = """<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/" xmlns:fb="http://www.facebook.com/2008/fbml" py:foo="barf"> <py:import href="includes.html" alias="includes"/> <body> <py:def function="the_body"><ul><li py:for="i in ['1', '2', '3']">$i ${str(int(i))}</li></ul></py:def> <py:def function="output_body(body_func)"><div>${body_func()}</div></py:def> ${output_body(the_body)} <div id="foo">$foo</div> <div id="bar">$bar</div> </body> </html>""" compiled = Loader(debug=True).load_string(template) print("Rendered python template >>>")