Skip to content

Commit

Permalink
Added utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 11, 2010
1 parent f87a9af commit 359438c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
43 changes: 10 additions & 33 deletions lib/jade/compiler.js
Expand Up @@ -11,7 +11,8 @@

var filters = require('./filters'),
doctypes = require('./doctypes'),
selfClosing = require('./self-closing');
selfClosing = require('./self-closing'),
utils = require('./utils');

/**
* Initialize `Compiler` with the given `node`.
Expand Down Expand Up @@ -52,7 +53,7 @@ Compiler.prototype = {
*/

buffer: function(str, esc){
if (esc) str = escape(str);
if (esc) str = utils.escape(str);
this.buf.push("buf.push('" + str + "');");
},

Expand Down Expand Up @@ -159,18 +160,18 @@ Compiler.prototype = {
visitFilter: function(filter){
var fn = filters[filter.name];
if (!fn) throw new Error('unknown filter ":' + filter.name + '"');
this.buffer(fn(text(filter.text)));
this.buffer(fn(utils.text(filter.text)));
},

/**
* Visit a text `node`.
* Visit `text` node.
*
* @param {Text} node
* @param {Text} text
* @api private
*/

visitText: function(node){
this.buffer(text(node));
visitText: function(text){
this.buffer(utils.text(text));
},

/**
Expand All @@ -182,7 +183,7 @@ Compiler.prototype = {

visitComment: function(comment){
if (!comment.buffer) return;
this.buffer('<!--' + escape(comment.val) + '-->');
this.buffer('<!--' + utils.escape(comment.val) + '-->');
},

/**
Expand Down Expand Up @@ -251,28 +252,4 @@ Compiler.prototype = {
}
this.buf.push("buf.push(attrs({ " + buf.join(', ') + " }));");
}
};

/**
* Convert interpolation in the given string to JavaScript.
*
* @param {String} str
* @return {String}
* @api private
*/

function interpolate(str){
return str.replace(/(\\)?[#$]{(.*?)}/g, function(str, escape, code){
return escape
? str
: "' + (" + code.replace(/\\'/g, "'") + ") + '";
});
}

function text(node){
return interpolate(escape(node.lines.join('\\n').trimLeft()));
}

function escape(str) {
return str.replace(/'/g, "\\'");
}
};
46 changes: 46 additions & 0 deletions lib/jade/utils.js
@@ -0,0 +1,46 @@

/*!
* Jade - utils
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/

/**
* Convert interpolation in the given string to JavaScript.
*
* @param {String} str
* @return {String}
* @api private
*/

var interpolate = exports.interpolate = function(str){
return str.replace(/(\\)?[#$]{(.*?)}/g, function(str, escape, code){
return escape
? str
: "' + (" + code.replace(/\\'/g, "'") + ") + '";
});
};

/**
* Escape single quotes in `str`.
*
* @param {String} str
* @return {String}
* @api private
*/

var escape = exports.escape = function(str) {
return str.replace(/'/g, "\\'");
};

/**
* Interpolate, escape, and join lines in the given text `node`.
*
* @param {Text} node
* @return {String}
* @api private
*/

exports.text = function(node){
return interpolate(escape(node.lines.join('\\n').trimLeft()));
};

0 comments on commit 359438c

Please sign in to comment.