Skip to content

Commit

Permalink
Merge branch 'pp'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 13, 2011
2 parents 7dbca5f + 674b82b commit 9590130
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
45 changes: 44 additions & 1 deletion jade.js
Expand Up @@ -62,6 +62,7 @@ var nodes = require('./nodes')
, filters = require('./filters')
, doctypes = require('./doctypes')
, selfClosing = require('./self-closing')
, inlineTags = require('./inline-tags')
, utils = require('./utils');


Expand Down Expand Up @@ -96,9 +97,13 @@ var nodes = require('./nodes')
var Compiler = module.exports = function Compiler(node, options) {
this.options = options = options || {};
this.node = node;

this.hasCompiledDoctype = false;
this.hasCompiledTag = false;
if (options.doctype) this.setDoctype(options.doctype);

this.pp = options.prettyprint || false;
this.indentDepth = 0;
};

/**
Expand Down Expand Up @@ -227,6 +232,7 @@ Compiler.prototype = {
*/

visitTag: function(tag){
this.indentDepth++;
var name = tag.name;

if (!this.hasCompiledTag) {
Expand All @@ -236,6 +242,9 @@ Compiler.prototype = {
this.hasCompiledTag = true;
}

if(this.pp && inlineTags.indexOf(name) == -1)
this.buffer('\\n' + new Array(this.indentDepth).join(' '));

if (~selfClosing.indexOf(name) && !this.xml) {
this.buffer('<' + name);
this.visitAttributes(tag.attrs);
Expand All @@ -255,8 +264,10 @@ Compiler.prototype = {
if (tag.text) this.buffer(utils.text(tag.text.nodes[0].trimLeft()));
this.escape = 'pre' == tag.name;
this.visit(tag.block);
if (this.pp && inlineTags.indexOf(name) == -1 && tag.textOnly == 0) this.buffer('\\n' + new Array(this.indentDepth).join(' '));
this.buffer('</' + name + '>');
}
this.indentDepth--;
},

/**
Expand Down Expand Up @@ -308,6 +319,7 @@ Compiler.prototype = {

visitComment: function(comment){
if (!comment.buffer) return;
if (this.pp) this.buffer('\\n' + new Array(this.indentDepth + 1).join(' '));
this.buffer('<!--' + utils.escape(comment.val) + '-->');
},

Expand Down Expand Up @@ -562,6 +574,37 @@ module.exports = {
};
}); // module: filters.js

require.register("inline-tags.js", function(module, exports, require){

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

module.exports = [
'a',
'abbr',
'acronym',
'b',
'br',
'code',
'em',
'font',
'i',
'img',
'ins',
'kbd',
'map',
'samp',
'small',
'span',
'strong',
'sub',
'sup'
];
}); // module: inline-tags.js

require.register("jade.js", function(module, exports, require){

/*!
Expand Down Expand Up @@ -1912,7 +1955,7 @@ var Parser = exports = module.exports = function Parser(str, filename){
* Tags that may not contain tags.
*/

var textOnly = exports.textOnly = ['code', 'script', 'textarea', 'style'];
var textOnly = exports.textOnly = ['code', 'script', 'textarea', 'style', 'title'];

/**
* Parser prototype.
Expand Down
19 changes: 19 additions & 0 deletions lib/compiler.js
Expand Up @@ -13,6 +13,7 @@ var nodes = require('./nodes')
, filters = require('./filters')
, doctypes = require('./doctypes')
, selfClosing = require('./self-closing')
, inlineTags = require('./inline-tags')
, utils = require('./utils');

// if browser
Expand Down Expand Up @@ -49,9 +50,13 @@ var nodes = require('./nodes')
var Compiler = module.exports = function Compiler(node, options) {
this.options = options = options || {};
this.node = node;

this.hasCompiledDoctype = false;
this.hasCompiledTag = false;
if (options.doctype) this.setDoctype(options.doctype);

this.pp = options.pretty || false;
this.indentDepth = 0;
};

/**
Expand Down Expand Up @@ -201,6 +206,7 @@ Compiler.prototype = {
*/

visitTag: function(tag){
this.indentDepth++;
var name = tag.name;

if (!this.hasCompiledTag) {
Expand All @@ -210,6 +216,11 @@ Compiler.prototype = {
this.hasCompiledTag = true;
}

// pretty print
if (this.pp && inlineTags.indexOf(name) == -1) {
this.buffer('\\n' + Array(this.indentDepth).join(' '));
}

if (~selfClosing.indexOf(name) && !this.xml) {
this.buffer('<' + name);
this.visitAttributes(tag.attrs);
Expand All @@ -229,8 +240,15 @@ Compiler.prototype = {
if (tag.text) this.buffer(utils.text(tag.text.nodes[0].trimLeft()));
this.escape = 'pre' == tag.name;
this.visit(tag.block);

// pretty print
if (this.pp && !~inlineTags.indexOf(name) && !tag.textOnly) {
this.buffer('\\n' + Array(this.indentDepth).join(' '));
}

this.buffer('</' + name + '>');
}
this.indentDepth--;
},

/**
Expand Down Expand Up @@ -282,6 +300,7 @@ Compiler.prototype = {

visitComment: function(comment){
if (!comment.buffer) return;
if (this.pp) this.buffer('\\n' + Array(this.indentDepth + 1).join(' '));
this.buffer('<!--' + utils.escape(comment.val) + '-->');
},

Expand Down
28 changes: 28 additions & 0 deletions lib/inline-tags.js
@@ -0,0 +1,28 @@

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

module.exports = [
'a'
, 'abbr'
, 'acronym'
, 'b'
, 'br'
, 'code'
, 'em'
, 'font'
, 'i'
, 'img'
, 'ins'
, 'kbd'
, 'map'
, 'samp'
, 'small'
, 'span'
, 'strong'
, 'sub'
, 'sup'
];
4 changes: 2 additions & 2 deletions lib/parser.js
Expand Up @@ -34,7 +34,7 @@ var Parser = exports = module.exports = function Parser(str, filename){
* Tags that may not contain tags.
*/

var textOnly = exports.textOnly = ['code', 'script', 'textarea', 'style'];
var textOnly = exports.textOnly = ['code', 'script', 'textarea', 'style', 'title'];

/**
* Parser prototype.
Expand Down Expand Up @@ -475,4 +475,4 @@ Parser.prototype = {

return tag;
}
};
};

0 comments on commit 9590130

Please sign in to comment.