Permalink
Browse files

Push to 3.0.0beta5

Updated documentation (closes #121)
Removed support for chunks
Added support for escaped and unescaped text interpolation
Client-side templates no longer expose base path information (closes #122)
Fixed interpolation for :javascript filter (fixes #120). Interpolated text is first wrapped in double quotes before UglifyJS parses it. Then, it is unwrapped.
Added interpolateQuote and interpolateUnquote routines to allow interpolated text to be wrapped and unwrapped in double quotes
  • Loading branch information...
bminer committed Jan 10, 2013
1 parent b9fb7b1 commit 189d16b2e6e68617e67e723c700074f8b153e562
View
315 README.md

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -95,7 +95,8 @@ function middleware(sourcePath, options) {
options.compileOptions = {
"cache": process.env.NODE_ENV == "production",
"minify": process.env.NODE_ENV == "production",
- "includeSource": process.env.NODE_ENV == "development"
+ "includeSource": process.env.NODE_ENV == "development",
+ "middleware": true
};
sourcePath = path.resolve(sourcePath);
options.compileOptions.basedir = sourcePath;
View
@@ -41,6 +41,12 @@ function Compiler(string, opts) {
this.options.basedir = path.resolve(this.options.basedir) || process.cwd();
this.options.reldir = path.dirname(path.relative(
this.options.basedir, this.options.filename) );
+ if(this.options.middleware)
+ {
+ this.options.filename = path.relative(this.options.basedir,
+ this.options.filename);
+ delete this.options.basedir;
+ }
this.includes = 0;
}
else
@@ -121,8 +127,9 @@ Compiler.prototype.compile = function(cb) {
{
var tempBuf = this.buf.substr(baseRelStart);
this.buf = this.buf.substr(0, baseRelStart);
- this._pushOff(ns + ".base=" + JSON.stringify(this.options.basedir) + ";" +
- ns + ".rel=" + JSON.stringify(this.options.reldir) + ";");
+ if(this.options.basedir)
+ this._pushOff(ns + ".base=" + JSON.stringify(this.options.basedir) + ";");
+ this._pushOff(ns + ".rel=" + JSON.stringify(this.options.reldir) + ";");
this.buf += tempBuf;
}
if(this.options.debug)
View
@@ -27,8 +27,10 @@ exports['coffeescript'] = exports['cs'] = function(text) {
};
exports['javascript'] = exports['js'] = function(text, opts) {
opts = opts || {};
- return '<script type="text/javascript">\n' +
- bladeutil.uglify(text) + '\n</script>';
+ text = bladeutil.interpolatePreparse(text);
+ text = bladeutil.uglify(text, opts.minify);
+ text = bladeutil.interpolatePostparse(text);
+ return '<script type="text/javascript">\n' + text + '\n</script>';
};
exports['stylus'] = cssTemplate('stylus');
exports['less'] = cssTemplate('less');
@@ -120,8 +120,6 @@ parent_node_types =
block
/
function_definition
- /
- chunk
/
isolate
/
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -13,12 +13,41 @@ double quoted string with double quotes escaped.
Usage: interpolate(JSON.stringify(some_string) )
*/
exports.interpolate = function interpolate(str, ns) {
- return str.replace(/(\\)?#{([^}]*)}/g, function(match, escaped, code) {
- return escaped ? "#{" + code + "}" : "\"+((" + ns + ".z=" + code.replace(/\\"/g, '"') +
+ return str.replace(/(\\)?([#!]){([^}]*)}/g, function(match, escaped, escFlag, code) {
+ return escaped ? match.substr(1) :
+ "\"+" + (escFlag == "#" ? ns + ".r.escape" : "") + "((" +
+ ns + ".z=" + code.replace(/\\"/g, '"') +
") == null ? '' : " + ns + ".z)+\"";
});
};
+/* This function is used before parsing `str` with UglifyJS to allow
+the resulting JavaScript code to be parsable by UglifyJS.
+
+`#{foo}` and `\#{foo}` are not valid JavaScript code. What's worse is
+that `var x = "\#{foo}"` results in JavaScript code that is semantically
+the same as `var x = "#{foo}"`, which UglifyJS may or may not detect.
+This means that the necessary backslash could be removed.
+
+For escaped interpolation strings, we simply remove the backslash
+For normal interpolation strings, `#{foo}` is replaced with
+`{__interpolate__esc__:foo}`, which is valid JavaScript. */
+exports.interpolatePreparse = function(str, ns) {
+ return str.replace(/(\\)?([#!]){([^}]*)}/g, function(match, escaped, escFlag, code) {
+ return escaped ? match.substr(1) : "{__interpolate__" +
+ (escFlag == "#" ? "esc__" : "") + ":" + code + "}";
+ });
+};
+
+// Inverse of interpolatePreparse
+exports.interpolatePostparse = function(str, ns) {
+ return str.replace(/([#!]){([^}]*)}/g, function(match) {
+ return "\\" + match;
+ }).replace(/{\s*__interpolate__(esc__)?\s*\:\s*([^}]*)}/g, function(match, escFlag, code) {
+ return (escFlag ? "#" : "!") + "{" + code.trim() + "}";
+ });
+};
+
/* Escapes single quotes in the string */
exports.quote = function quote(str) {
return str.replace(/'/g, "\\'");
View
@@ -15,7 +15,7 @@
"live binding",
"meteor"
],
- "version": "3.0.0beta4",
+ "version": "3.0.0beta5",
"homepage": "https://github.com/bminer/node-blade",
"repository": {
"type": "git",
View
@@ -3,7 +3,7 @@
"author": "Blake Miner <miner.blake@gmail.com> (http://www.blakeminer.com/)",
"description": "Blade - HTML Template Compiler, inspired by Jade & Haml",
"homepage": "https://github.com/bminer/node-blade",
- "version": "3.0.0beta4",
+ "version": "3.0.0beta5",
"git": "https://github.com/bminer/node-blade.git",
"packages": {
}
View
@@ -1 +0,0 @@
-<div><h1>Hello!</h1></div><div><h1>1</h1><h1>2</h1><h1>3</h1></div><h1>Hello</h1>
@@ -0,0 +1,20 @@
+<script type="text/javascript">
+var obj = {"foo":"bar"};
+
+alert(obj.foo);
+</script>
+This is some text that<br/>should have some &lt;br/&gt;<br/>crap in it.<br/>What do you think of that?<br/><br/>This &quot;text&quot; 'might' &lt;need&gt; some &quot;escaping&quot; &amp; stuff<br/>This "text" 'might' <need> some "escaping" & stuff<br/>#{escape} is escaped<br/>!{escape} is escaped<script type="text/javascript">
+var str = "This is a cool test";
+
+var madness = "This is a bit " + "weird";
+
+window.alert(madness);
+
+var x = "#{foo}";
+
+//escaped interpolation
+var y = "!{foo}";
+
+//escaped interpolation
+var z = "\\#{foo}";
+</script>
@@ -1,14 +0,0 @@
-chunk
- h1 Hello!
-div
- != __.chunk.last()
-- var i = 0
-chunk
- h1= ++i
-div
- != __.chunk.last()
- != __.chunk.last()
- != __.chunk.last()
-chunk header(text)
- h1= text
-!= __.chunk.header("Hello")
@@ -0,0 +1,22 @@
+- var obj = JSON.stringify({"foo":"bar"});
+:javascript
+ var obj = !{obj};
+ alert(obj.foo);
+:nl2br
+ This is some text that
+ should have some <br/>
+ crap in it.
+ What do you think of that?
+
+ #{escape}
+ !{escape}
+ \#{escape} is escaped
+ \!{escape} is escaped
+- var str = "cool"
+:javascript
+ var str = "This is a !{str} test";
+ var madness = "This is a bit " + (!{str == "cool" ? "\"weird\"" : "\"uncool\""});
+ window.alert(madness);
+ var x = "\#{foo}"; //escaped interpolation
+ var y = "\!{foo}"; //escaped interpolation
+ var z = "\\\#{foo}"; //escaped interpolation

0 comments on commit 189d16b

Please sign in to comment.