grunt-processhtml
Advanced tools
Comparing version 0.2.8 to 0.2.9
{ | ||
"name": "grunt-processhtml", | ||
"description": "Process html files at build time to modify them depending on the release environment", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"homepage": "https://github.com/dciccale/grunt-processhtml", | ||
@@ -29,8 +29,8 @@ "author": { | ||
"scripts": { | ||
"test": "grunt test" | ||
"test": "grunt --verbose" | ||
}, | ||
"devDependencies": { | ||
"grunt": "0.4.1", | ||
"grunt-contrib-jshint": "0.6.4", | ||
"grunt-contrib-nodeunit": "0.2.1", | ||
"grunt-contrib-jshint": "0.8.0", | ||
"grunt-contrib-nodeunit": "0.3.0", | ||
"grunt-release": "0.6.0" | ||
@@ -37,0 +37,0 @@ }, |
@@ -262,3 +262,3 @@ # grunt-processhtml [![Build Status](https://travis-ci.org/dciccale/grunt-processhtml.png?branch=master)](https://travis-ci.org/dciccale/grunt-processhtml) [![NPM version](https://badge.fury.io/js/grunt-processhtml.png)](http://badge.fury.io/js/grunt-processhtml) | ||
Specifying `true` (boolean, no quotes) will strip comments which do not match the current target: | ||
Specifying `true` will strip comments which do not match the current target: | ||
@@ -269,2 +269,12 @@ ```javascript | ||
#### options.recursive | ||
Type: `Boolean` | ||
Default value: `false` | ||
Recursively process files that are being included using `build:include`. | ||
```javascript | ||
recursive: true | ||
``` | ||
### Usage Examples | ||
@@ -446,2 +456,4 @@ | ||
## Release History | ||
- 0.2.9 Added `recursive` option | ||
- 0.2.8 Changed `include` to not use `replace()` | ||
- 0.2.7 Added `commentMarker` option | ||
@@ -448,0 +460,0 @@ - 0.2.6 Fix #14 and added grunt-release |
@@ -5,3 +5,3 @@ /* | ||
* | ||
* Copyright (c) 2013 Denis Ciccale (@tdecs) | ||
* Copyright (c) 2013-2014 Denis Ciccale (@tdecs) | ||
* Licensed under the MIT license. | ||
@@ -66,99 +66,117 @@ * https://github.com/dciccale/grunt-processhtml/blob/master/LICENSE-MIT | ||
var getBlockTypes = function (options, filePath) { | ||
return { | ||
replaceAsset: function (content, block, blockLine, asset) { | ||
return content.replace(blockLine, block.indent + asset); | ||
}, | ||
// The processor | ||
var HTMLProcessor = function (content, options, filePath) { | ||
this.content = content; | ||
this.options = options; | ||
this.filePath = filePath; | ||
this.target = options.data.environment; | ||
this.linefeed = /\r\n/g.test(content) ? '\r\n' : '\n'; | ||
}; | ||
css: function (content, block, blockLine, blockContent) { | ||
return this.replaceAsset(content, block, blockLine, '<link rel="stylesheet" href="' + block.asset + '">'); | ||
}, | ||
// Returns a single line of the current block comment | ||
HTMLProcessor.prototype._getBlockLine = function (block) { | ||
return block.raw.join(this.linefeed); | ||
}; | ||
js: function (content, block, blockLine, blockContent) { | ||
return this.replaceAsset(content, block, blockLine, '<script src="' + block.asset + '"><\/script>'); | ||
}, | ||
// Returns the block content (not including the build comments) | ||
HTMLProcessor.prototype._getBlockContent = function (block) { | ||
return block.raw.slice(1, -1).join(this.linefeed); | ||
}; | ||
attr: function (content, block, blockLine, blockContent) { | ||
// Replace passed block with the processed content | ||
HTMLProcessor.prototype._replace = function (block, content) { | ||
var blockLine = this._getBlockLine(block); | ||
var blockContent = this._getBlockContent(block); | ||
var result = this._blockTypes[block.type].call(this, content, block, blockLine, blockContent); | ||
// Only run attr replacer for the block content | ||
var re = new RegExp('(\\s*(?:' + block.attr + ')=[\'"])(.*)?(".*)', 'gi'); | ||
var replacedBlock = blockContent.replace(re, function (wholeMatch, start, asset, end) { | ||
return result; | ||
}; | ||
// Check if only the path was provided to leave the original asset name intact | ||
asset = (!path.extname(block.asset) && /\//.test(block.asset))? block.asset + path.basename(asset) : block.asset; | ||
// Strips blocks not matched for the current target | ||
HTMLProcessor.prototype._strip = function (block, content) { | ||
var blockLine = this._getBlockLine(block); | ||
var blockContent = this._getBlockContent(block); | ||
var blockRegExp = utils.blockToRegExp(blockLine); | ||
var result = content.replace(blockRegExp, '\n\n' + blockContent); | ||
return start + asset + end; | ||
}); | ||
return result; | ||
}; | ||
return content.replace(blockLine, replacedBlock); | ||
}, | ||
// Define default block types | ||
HTMLProcessor.prototype._blockTypes = { | ||
remove: function (content, block, blockLine, blockContent) { | ||
var blockRegExp = utils.blockToRegExp(blockLine); | ||
css: function (content, block, blockLine, blockContent) { | ||
return content.replace(blockLine, block.indent + '<link rel="stylesheet" href="' + block.asset + '">'); | ||
}, | ||
return content.replace(blockRegExp, ''); | ||
}, | ||
js: function (content, block, blockLine, blockContent) { | ||
return content.replace(blockLine, block.indent + '<script src="' + block.asset + '"><\/script>'); | ||
}, | ||
template: function (content, block, blockLine, blockContent) { | ||
var compiledTmpl = utils.template(blockContent, options); | ||
attr: function (content, block, blockLine, blockContent) { | ||
var re = new RegExp('(\\s*(?:' + block.attr + ')=[\'"])(.*)?(".*)', 'gi'); | ||
// Clean template output and fix indent | ||
compiledTmpl = block.indent + grunt.util._.trim(compiledTmpl).replace(/([\r\n])\s*/g, '$1' + block.indent); | ||
// Only run attr replacer for the block content | ||
var replacedBlock = blockContent.replace(re, function (wholeMatch, start, asset, end) { | ||
return content.replace(blockLine, compiledTmpl); | ||
}, | ||
// Check if only the path was provided to leave the original asset name intact | ||
asset = (!path.extname(block.asset) && /\//.test(block.asset))? block.asset + path.basename(asset) : block.asset; | ||
include: function (content, block, blockLine, blockContent) { | ||
var base = options.includeBase || path.dirname(filePath); | ||
var filepath = path.join(base, block.asset); | ||
var l = blockLine.length; | ||
var fileContent, i; | ||
return start + asset + end; | ||
}); | ||
if (grunt.file.exists(filepath)) { | ||
fileContent = block.indent + grunt.file.read(filepath); | ||
while ((i = content.indexOf(blockLine)) !== -1) { | ||
content = content.substring(0, i) + fileContent + content.substring(i + l); | ||
} | ||
} | ||
return content.replace(blockLine, replacedBlock); | ||
}, | ||
return content; | ||
} | ||
}; | ||
}; | ||
remove: function (content, block, blockLine, blockContent) { | ||
var blockRegExp = utils.blockToRegExp(blockLine); | ||
var HTMLProcessor = module.exports = function (content, options, filePath) { | ||
this.content = content; | ||
this.target = options.data.environment; | ||
this.linefeed = /\r\n/g.test(content) ? '\r\n' : '\n'; | ||
this.blocks = getBlocks(content, options.commentMarker); | ||
this.blockTypes = getBlockTypes(options, filePath); | ||
this.strip = options.strip === true; | ||
}; | ||
return content.replace(blockRegExp, ''); | ||
}, | ||
HTMLProcessor.prototype._replace = function (block, content) { | ||
var blockLine = block.raw.join(this.linefeed); | ||
var blockContent = block.raw.slice(1, -1).join(this.linefeed); | ||
var result = this.blockTypes[block.type](content, block, blockLine, blockContent); | ||
template: function (content, block, blockLine, blockContent) { | ||
var compiledTmpl = utils.template(blockContent, this.options); | ||
return result; | ||
}; | ||
// Clean template output and fix indent | ||
compiledTmpl = block.indent + grunt.util._.trim(compiledTmpl).replace(/([\r\n])\s*/g, '$1' + block.indent); | ||
HTMLProcessor.prototype._strip = function (block, content) { | ||
var blockLine = block.raw.join(this.linefeed); | ||
var blockRegExp = utils.blockToRegExp(blockLine); | ||
var blockContent = block.raw.slice(1, -1).join(this.linefeed); | ||
var result = content.replace(blockRegExp, '\n\n' + blockContent); | ||
return content.replace(blockLine, compiledTmpl); | ||
}, | ||
return result; | ||
include: function (content, block, blockLine, blockContent) { | ||
var base = this.options.includeBase || path.dirname(this.filePath); | ||
var filepath = path.join(base, block.asset); | ||
var l = blockLine.length; | ||
var fileContent, i; | ||
if (grunt.file.exists(filepath)) { | ||
fileContent = block.indent + grunt.file.read(filepath); | ||
// Remove any last new line | ||
fileContent = fileContent.replace(/\n$/, ''); | ||
// Recursively process included files | ||
if (this.options.recursive) { | ||
fileContent = new HTMLProcessor(fileContent, this.options, filepath).process(); | ||
} | ||
while ((i = content.indexOf(blockLine)) !== -1) { | ||
content = content.substring(0, i) + fileContent + content.substring(i + l); | ||
} | ||
} | ||
return content; | ||
} | ||
}; | ||
HTMLProcessor.prototype.process = function () { | ||
HTMLProcessor.prototype._replaceBlocks = function (blocks) { | ||
var result = this.content; | ||
grunt.util._.each(this.blocks, function (block) { | ||
// Replace found blocks | ||
grunt.util._.each(blocks, function (block) { | ||
// Parse through correct block type also checking the build target | ||
if (this.blockTypes[block.type] && (!block.targets || grunt.util._.indexOf(block.targets, this.target) >= 0)) { | ||
// Parse through correct block type checking the build target | ||
if (this._blockTypes[block.type] && (!block.targets || grunt.util._.indexOf(block.targets, this.target) >= 0)) { | ||
result = this._replace(block, result); | ||
} else if (this.strip) { | ||
} else if (this.options.strip) { | ||
result = this._strip(block, result); | ||
@@ -170,1 +188,16 @@ } | ||
}; | ||
// Process the file content | ||
HTMLProcessor.prototype.process = function () { | ||
// Parse the file content to look for build comment blocks | ||
var blocks = getBlocks(this.content, this.options.commentMarker); | ||
// Replace found blocks | ||
var content = this._replaceBlocks(blocks); | ||
return content; | ||
}; | ||
// Export the processor | ||
module.exports = HTMLProcessor; |
@@ -5,3 +5,3 @@ /* | ||
* | ||
* Copyright (c) 2013 Denis Ciccale (@tdecs) | ||
* Copyright (c) 2013-2014 Denis Ciccale (@tdecs) | ||
* Licensed under the MIT license. | ||
@@ -8,0 +8,0 @@ * https://github.com/dciccale/grunt-processhtml/blob/master/LICENSE-MIT |
@@ -5,3 +5,3 @@ /* | ||
* | ||
* Copyright (c) 2013 Denis Ciccale (@tdecs) | ||
* Copyright (c) 2013-2014 Denis Ciccale (@tdecs) | ||
* Licensed under the MIT license. | ||
@@ -23,6 +23,9 @@ * https://github.com/dciccale/grunt-processhtml/blob/master/LICENSE-MIT | ||
templateSettings: null, | ||
commentMarker: 'build' | ||
includeBase: null, | ||
commentMarker: 'build', | ||
strip: false, | ||
recursive: false | ||
}); | ||
// extend template data | ||
// Extend template data | ||
grunt.util._.extend(options.data, { | ||
@@ -32,3 +35,3 @@ environment: this.target | ||
// set custom delimiters | ||
// Set custom delimiters | ||
var templateSettings = options.templateSettings; | ||
@@ -35,0 +38,0 @@ if (templateSettings && templateSettings.opener && templateSettings.closer) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22956
230
465