grunt-processhtml
Advanced tools
Comparing version 0.2.7 to 0.2.8
{ | ||
"name": "grunt-processhtml", | ||
"description": "Process html files at build time to modify them depending on the release environment", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"homepage": "https://github.com/dciccale/grunt-processhtml", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -40,2 +40,4 @@ # 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) | ||
You can pass multiple comma-separated targets, e.g. `<!-- build:remove:dist,dev,prod -->` and block will be parsed for each. | ||
##### value | ||
@@ -50,7 +52,7 @@ Required for types: `js`, `css`, `include` and `[attr]`. | ||
##### `build:js[:target] <value>` | ||
##### `build:js[:targets] <value>` | ||
Replace many script tags into one. | ||
`[:target]` Optional build target. | ||
`[:targets]` Optional build targets. | ||
@@ -69,7 +71,7 @@ `<value>` Required value: A file path. | ||
##### `build:css[:target] <value>` | ||
##### `build:css[:targets] <value>` | ||
Replace many stylesheet link tags into one. | ||
`[:target]` Optional build target. | ||
`[:targets]` Optional build targets. | ||
@@ -88,3 +90,3 @@ `<value>` Required value: A file path. | ||
##### `build:<[attr]>[:target] <value>` | ||
##### `build:<[attr]>[:targets] <value>` | ||
@@ -95,3 +97,3 @@ Change the value of an attribute. In most cases using `[src]` and `[href]` will be enough but it works with any html attribute. | ||
`[:target]` Optional build target. | ||
`[:targets]` Optional build targets. | ||
@@ -130,7 +132,7 @@ `<value>` Required value: A path or a file path. | ||
##### `build:include[:target] <value>` | ||
##### `build:include[:targets] <value>` | ||
Include an external file. | ||
`[:target]` Optional build target. | ||
`[:targets]` Optional build targets. | ||
@@ -153,7 +155,7 @@ `<value>` Required value: A file path. | ||
##### `build:template[:target]` | ||
##### `build:template[:targets]` | ||
Process a template block with a data object inside [options.data](#optionsdata). | ||
`[:target]` Optional build target. | ||
`[:targets]` Optional build targets. | ||
@@ -172,7 +174,7 @@ | ||
##### `build:remove[:target]` | ||
##### `build:remove[:targets]` | ||
Remove a block. | ||
`[:target]` Optional build target | ||
`[:targets]` Optional build targets | ||
@@ -259,3 +261,3 @@ ```html | ||
```html | ||
<!-- process:<type>[:target] [value] --> | ||
<!-- process:<type>[:targets] [value] --> | ||
... | ||
@@ -265,2 +267,12 @@ <!-- /process --> | ||
#### options.strip | ||
Type: `Boolean` | ||
Default value: `null` | ||
Specifying `true` (boolean, no quotes) will strip comments which do not match the current target: | ||
```javascript | ||
strip: true | ||
``` | ||
### Usage Examples | ||
@@ -442,2 +454,3 @@ | ||
## Release History | ||
- 0.2.7 Added `commentMarker` option | ||
- 0.2.6 Fix #14 and added grunt-release | ||
@@ -444,0 +457,0 @@ - 0.2.5 Create first tag using grunt-release |
@@ -23,6 +23,8 @@ /* | ||
*/ | ||
var regbuild = new RegExp('<!--\\s*' + marker + ':(\\[?[\\w-]+\\]?)(?::(\\w+))?(?:\\s*([^\\s]+)\\s*-->)*'); | ||
var regbuild = new RegExp('<!--\\s*' + marker + ':(\\[?[\\w-]+\\]?)(?::([\\w,]+))?(?:\\s*([^\\s]+)\\s*-->)*'); | ||
// <!-- /build --> | ||
var regend = new RegExp('(?:<!--\\s*)*\\/' + marker + '\\s*-->'); | ||
// normalize line endings and split in lines | ||
// Normalize line endings and split in lines | ||
var lines = content.replace(/\r\n/g, '\n').split(/\n/); | ||
@@ -40,7 +42,7 @@ var inside = false; | ||
inside = true; | ||
attr = build[1].match(/(?:\[([\w-]+)\])*/)[1]; | ||
attr = build[1].match(/(?:\[([\w\-]+)\])*/)[1]; | ||
block = { | ||
type: attr ? 'attr': build[1], | ||
attr: attr, | ||
target: build[2], | ||
targets: !!build[2] ? build[2].split(',') : null, | ||
asset: build[3], | ||
@@ -80,9 +82,13 @@ indent: /^\s*/.exec(line)[0], | ||
attr: function (content, block, blockLine, blockContent) { | ||
// only run attr replacer for the block content | ||
// 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) { | ||
// check if only the path was provided to leave the original asset name intact | ||
// 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; | ||
return start + asset + end; | ||
}); | ||
return content.replace(blockLine, replacedBlock); | ||
@@ -93,2 +99,3 @@ }, | ||
var blockRegExp = utils.blockToRegExp(blockLine); | ||
return content.replace(blockRegExp, ''); | ||
@@ -99,4 +106,6 @@ }, | ||
var compiledTmpl = utils.template(blockContent, options); | ||
// clean template output and fix indent | ||
// Clean template output and fix indent | ||
compiledTmpl = block.indent + grunt.util._.trim(compiledTmpl).replace(/([\r\n])\s*/g, '$1' + block.indent); | ||
return content.replace(blockLine, compiledTmpl); | ||
@@ -108,7 +117,12 @@ }, | ||
var filepath = path.join(base, block.asset); | ||
var fileContent; | ||
var l = blockLine.length; | ||
var fileContent, i; | ||
if (grunt.file.exists(filepath)) { | ||
fileContent = block.indent + grunt.file.read(filepath); | ||
content = content.replace(blockLine, fileContent); | ||
while ((i = content.indexOf(blockLine)) !== -1) { | ||
content = content.substring(0, i) + fileContent + content.substring(i + l); | ||
} | ||
} | ||
return content; | ||
@@ -125,2 +139,3 @@ } | ||
this.blockTypes = getBlockTypes(options, filePath); | ||
this.strip = options.strip === true; | ||
}; | ||
@@ -132,5 +147,15 @@ | ||
var result = this.blockTypes[block.type](content, block, blockLine, blockContent); | ||
return result; | ||
}; | ||
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 result; | ||
}; | ||
HTMLProcessor.prototype.process = function () { | ||
@@ -140,5 +165,8 @@ var result = this.content; | ||
grunt.util._.each(this.blocks, function (block) { | ||
// parse through correct block type also checking the build target | ||
if (this.blockTypes[block.type] && (!block.target || block.target === this.target)) { | ||
// 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)) { | ||
result = this._replace(block, result); | ||
} else if (this.strip) { | ||
result = this._strip(block, result); | ||
} | ||
@@ -145,0 +173,0 @@ }, this); |
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
21851
204
453