def testTokenRegexIgnoresCode(self): # Special case: s = jsontemplate.expand('{foo}', {'foo': 'bar'}) self.verify.Equal('bar', s) s = jsontemplate.expand('{.repeated section foo}{@}{.end}', {'foo': ['a', 'b', 'c']}) self.verify.Equal('abc', s) # This didn't used to work because the variable would be " foo" and it would # be undefined. Now we just let it pass through as literal text. s = jsontemplate.expand('{ foo}', {'foo': 'bar'}) self.verify.Equal('{ foo}', s) s = jsontemplate.expand('function() { return {@}; }', 1) self.verify.Equal('function() { return 1; }', s) s = jsontemplate.expand('function() { return {@};', 1) self.verify.Equal('function() { return 1;', s) # If you have an {.end} you'll get a different error because it'll be # unmatched s = jsontemplate.expand('{ .repeated section foo}', {'foo': ['a', 'b', 'c']}) self.verify.Equal('{ .repeated section foo}', s) # ignored
def run_static(args): "writes static files to the specified output directory" PATTERN = re.compile("([\"'])") formatters = {"string-data": lambda x: PATTERN.sub(r"\\\1", x)} R = lambda x: Path(x, pkg_resource=True) # partial templates (client side rendering) partials = R("data/templates/partials/") with open(path.join(args.output, "templates.json"), "wb") as out: out.write( json.dumps( dict( (name, (partials + name).read(encoding="utf-8")) for name in partials.listdir() if name.endswith(".jst") ) ).encode("utf-8") ) # message.js.jst expansion with open(path.join(args.output, "messages.js"), "wb") as out: template = R("data/scripts/messages.js.jst").read(encoding="utf-8") data = jsontemplate.expand(template, args.__dict__, meta="[[]]", more_formatters=formatters).encode("utf-8") out.write(data) # history.js + adapter with open(path.join(args.output, "history.js"), "wb") as out: out.write(R("data/scripts/history.js").read()) out.write(";\n") out.write(R("data/scripts/history.adapter.jquery.js").read()) # everything else is a straight copy to output directory for type, files in (("scripts", ["jquery-1.7.2.min.js", "json-template.js"]), ("styles", ["messages.css"])): for filename in files: with open(path.join(args.output, filename), "wb") as out: out.write(R("data/%s/%s" % (type, filename)).read())
def testExpand(self): """Test the free function expand.""" self.verify.Equal( jsontemplate.expand('Hello {name}', {'name': 'World'}), 'Hello World')