def test_call_macro4(self): # call macro passing parament, with extra output node = self.get_compile_from_string("""{% namespace xxx.ns1 %} {% macro testif(option) -%} {% if option %}{{ option }}{% endif %}{% endmacro %} {% macro testcall() %}Hello, {{ xxx.ns1.testif(option = true) }}!{% endmacro %}""") stream = StringIO() jscompiler.generate( node, self.env, "for.html", "for.html", stream = stream) source_code = stream.getvalue() self.assertEqual(source_code, """goog.provide('xxx.ns1'); goog.require('soy'); xxx.ns1.testif = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); if (opt_data.option) { output.append(opt_data.option); } if (!opt_sb) return output.toString(); } xxx.ns1.testcall = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); output.append('Hello, '); xxx.ns1.testif({option: true}, output); output.append('!'); if (!opt_sb) return output.toString(); }""")
def test_call_macro2(self): # call macro in same template where the namespace contains # multiple dotted names. node = self.get_compile_from_string("""{% namespace xxx.ns1 %} {% macro testif(option) -%} {% if option %}{{ option }}{% endif %}{% endmacro %} {% macro testcall() %}{{ xxx.ns1.testif() }}{% endmacro %}""") stream = StringIO() jscompiler.generate( node, self.env, "for.html", "for.html", stream = stream) source_code = stream.getvalue() self.assertEqual(source_code, """goog.provide('xxx.ns1'); goog.require('soy'); xxx.ns1.testif = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); if (opt_data.option) { output.append(opt_data.option); } if (!opt_sb) return output.toString(); } xxx.ns1.testcall = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); xxx.ns1.testif({}, output); if (!opt_sb) return output.toString(); }""")
def __call__(self, request): path = request.path[len(self.url):] try: source, filename, uptodate = self.env.loader.get_source( self.env, path) except jinja2.TemplateNotFound: return webob.Response("Not found", status = 404) node = self.env._parse(source, path, filename) stream = StringIO() jscompiler.generate(node, self.env, path, filename, stream) return webob.Response( body = stream.getvalue(), content_type = "application/javascript")
def GetSourcePath(self): jscode = jscompiler.generate( self.node, self.env, os.path.basename(self._path), self._path) self.tmp = tempfile.NamedTemporaryFile(delete = False) self.tmp.write(jscode) self.tmp.close() return jscode, self.tmp.name
def test_for8(self): # test loop.index with other variable, with attribute node = self.get_compile_from_string("{% namespace xxx %}{% macro fortest(data, param) %}{% for item in data %}{{ loop.index }} - {{ param.name }}{% endfor %}{% endmacro %}") stream = StringIO() jscompiler.generate( node, self.env, "for.html", "for.html", stream = stream) source_code = stream.getvalue() self.assertEqual(source_code, """goog.provide('xxx'); goog.require('soy'); xxx.fortest = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); var itemList = opt_data.data; var itemListLen = itemList.length; for (var itemIndex = 0; itemIndex < itemListLen; itemIndex++) { var itemData = itemList[itemIndex]; output.append(itemIndex + 1, ' - ', opt_data.param.name); } if (!opt_sb) return output.toString(); }""")
def test_filter2(self): # XXX - test filter with kwargs node = self.get_compile_from_string("""{% namespace test %} {% macro filtertest(data) %} {{ data|truncate(length=280) }} {% endmacro %} """) stream = StringIO() jscompiler.generate( node, self.env, "filter.html", "filter.html", stream = stream) source_code = stream.getvalue() self.assertEqual(source_code, """goog.provide('test'); goog.require('soy'); test.filtertest = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); output.append('\\n', jinja2_filters.filter_truncate(opt_data.data, {length: 280}), '\\n'); if (!opt_sb) return output.toString(); }""")
def main(args = None, output = None): if output is None: output = sys.stdout parser = optparse.OptionParser() # closure template options that we support parser.add_option( "--outputPathFormat", dest = "output_format", help = "A format string that specifies how to build the path to each output file. You can include literal characters as well as the following $variables: ${INPUT_FILE_NAME}, ${INPUT_FILE_NAME_NO_EXT}, and ${INPUT_DIRECTORY}.", metavar = "OUTPUT_FORMAT") # jinja2js specific options parser.add_option( "--directories", dest = "directories", default = [], action = "append", help = "List of directories to look for template files.", metavar = "DIRECTORIES") parser.add_option( "--packages", dest = "packages", default = [], action = "append", help = "List of packages to look for template files.", metavar = "PACKAGE") parser.add_option( "--codeStyle", choices = ["stringbuilder", "concat"], dest = "codeStyle", default = "concat", type = "choice", help = "The code style to use when generating JS code. Either `stringbuilder` or `concat` styles.") options, files = parser.parse_args(args) outputPathFormat = options.output_format if not outputPathFormat: parser.print_help(output) return 1 env = environment.create_environment( packages = options.packages, directories = options.directories, writer = writerclasses[options.codeStyle]) for filename in files: name = os.path.basename(filename) node = env._parse(open(filename).read(), name, filename) output = jscompiler.generate(node, env, name, filename) output_filename = get_output_filename(options.output_format, filename) open(output_filename, "w").write(output) return 0
def __call__(self, request): path = request.path_info try: source, filename, uptodate = self.env.loader.get_source( self.env, path) except jinja2.TemplateNotFound: return webob.Response("Not found", status = 404) node = self.env._parse(source, path, filename) output = jscompiler.generate(node, self.env, path, filename) return webob.Response( body = output, content_type = "application/javascript")
def test_import1(self): node = self.get_compile_from_string("""{% namespace xxx.ns1 %} {% import 'test_import.soy' as forms %} {% macro hello(name) %}{{ forms.input(name = 'test') }}{% endmacro %}""") stream = StringIO() jscompiler.generate( node, self.env, "for.html", "for.html", stream = stream) source_code = stream.getvalue() self.assertEqual(source_code, """goog.provide('xxx.ns1'); goog.require('soy'); goog.require('test.ns1'); xxx.ns1.hello = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); test.ns1.input({name: 'test'}, output); if (!opt_sb) return output.toString(); }""")
def test_call_macro8(self): # call macro with parameter sub node = self.get_compile_from_string("""{% namespace xxx.ns1 %} {% macro hello(name) -%} Hello, {% if name %}{{ name.first }}{% else %}world{% endif %}!{% endmacro %} {% macro testcall() %}{{ xxx.ns1.hello(name = {"first": "Michael"}) }}{% endmacro %}""") stream = StringIO() jscompiler.generate( node, self.env, "for.html", "for.html", stream = stream) source_code = stream.getvalue() self.assertEqual(source_code, """goog.provide('xxx.ns1'); goog.require('soy'); xxx.ns1.hello = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); output.append('Hello, '); if (opt_data.name) { output.append(opt_data.name.first); } else { output.append('world'); } output.append('!'); if (!opt_sb) return output.toString(); } xxx.ns1.testcall = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); xxx.ns1.hello({name: {'first': 'Michael'}}, output); if (!opt_sb) return output.toString(); }""")
def test_for1(self): # XXX - test recursive loop node = self.get_compile_from_string("""{% namespace test %} {% macro fortest(data) %} {% for item in data %} Item {{ item }}. {% else %} No items. {% endfor %} {% endmacro %} """) stream = StringIO() jscompiler.generate( node, self.env, "for.html", "for.html", stream = stream) source_code = stream.getvalue() self.assertEqual(source_code, """goog.provide('test'); goog.require('soy'); test.fortest = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder(); output.append('\\n'); var itemList = opt_data.data; var itemListLen = itemList.length; if (itemListLen > 0) { for (var itemIndex = 0; itemIndex < itemListLen; itemIndex++) { var itemData = itemList[itemIndex]; output.append('\\n Item ', itemData, '.\\n'); } } else { output.append('\\n No items.\\n'); } output.append('\\n'); if (!opt_sb) return output.toString(); }""")
def load_and_compare(source_file, expected_file): src = jscompiler.generate(env, expected_file, source_file) with open(expected_file) as f: expected = f.read() compare(src, expected)
def compiler(self, node, env, path, filename): return jscompiler.generate(node, env, path, filename)