Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ Block.prototype.compile = function () {
return this.config.keepUnassigned ? this.originals : [];
}

// get the replacement strings and do replacements for extensions
if (this.uniqueExts) {
var basename = path.basename(this.file.path);
var extname = path.extname(basename);
basename = basename.slice(0, basename.lastIndexOf(extname))
if (this.uniqueExts['%f'])
this.uniqueExts['%f'].value = basename;
if (this.uniqueExts['%e'])
this.uniqueExts['%e'].value = extname;

Object.keys(this.uniqueExts).forEach(function (key) {
var unique = this.uniqueExts[key];
this.template = this.template.replace(unique.regex, unique.value);
}.bind(this));
}

if (this.srcIsNull) {
return this.indent + this.template;
}

return this.replacements.map(function (replacement) {
if (this.template) {
if (Array.isArray(replacement)) {
Expand Down Expand Up @@ -51,10 +71,13 @@ Block.prototype.compile = function () {
}.bind(this));
};


Block.prototype.reset = function () {
this.originals = [];
this.replacements = [];
this.template = null;
this.uniqueExts = null;
this.srcIsNull = false;
};

Block.prototype.setTask = function (task) {
Expand All @@ -64,6 +87,8 @@ Block.prototype.setTask = function (task) {
if (task) {
this.replacements = task.src;
this.template = task.tpl;
this.uniqueExts = task.uni;
this.srcIsNull = task.srcIsNull;
}
};

Expand Down
28 changes: 23 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ module.exports = function (options, userConfig) {

if (typeof userConfig === 'boolean') {
config.keepUnassigned = userConfig;
}

if (typeof userConfig === 'object') {
} else if (typeof userConfig === 'object') {
config = extend(config, userConfig);
}

Expand Down Expand Up @@ -58,24 +56,44 @@ module.exports = function (options, userConfig) {
* ....
* }
*/
var utilExtensions = /%f|%e/g;

function getTasks(options) {
var tasks = {};

Object.keys(options).forEach(function (key) {
var item = options[key];
var src = [];
var tpl = null;
var uniqueExtensions = {};
var result;
var srcIsNull;

if (item.src) {
if (typeof item.src !== 'undefined') {
srcIsNull = item.src === null;
src = src.concat(item.src);
tpl = item.tpl;
} else {
src = src.concat(item);
}

while (result = utilExtensions.exec(tpl)) {
var type = result[0];
var unique = {};

if (uniqueExtensions[type])
continue;

unique.regex = new RegExp(result[0], "g");
unique.value = null;
uniqueExtensions[type] = unique;
}

tasks[key] = {
src: src,
tpl: tpl
tpl: tpl,
uni: uniqueExtensions,
srcIsNull: srcIsNull
};
});

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-html-replace",
"version": "1.3.0",
"version": "1.4.0",
"description": "Replace build blocks in HTML. Like useref but done right.",
"keywords": [
"gulpplugin",
Expand All @@ -15,6 +15,10 @@
"name": "Vladimir Kucherenko",
"email": "kvsoftware@gmail.com"
},
"contributors": [
{"name": "Bruce MacNaughton",
"email": "bmacnaughton@gmail.com"}
],
"main": "./lib/index.js",
"scripts": {
"test": "mocha --reporter spec",
Expand Down
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ htmlreplace({

> In the second example `data-main="%s"` and `src="%s"` will be replaced with `data-main.js` and `require-src.js` accordingly, producing `<script data-main="data-main.js" src="require-src.js"></script>` as the result

###### Extended replacements:
```javascript
// Replacement based on the file being processed
htmlreplace({
js: {
src: null,
tpl: '<script src="%f".js></script>'
}
})
// Extended replacement combined with standard replacement
htmlreplace({
js: {
src: 'dir',
tpl: '<script src="%s/%f".js"></script>'
}
})

```
* **src** - `null|String|Array` Same as examples above but null if there are no standard replacements in the template.
* **tpl** - `String` Template string. Extended replacements do not use `util.format()` and are performed before standard replacements.

> In the first example `src` is null because there are no standard replacements. `%f` is replaced with the name (without extension) of the file currently being processed. If the file being processed is `xyzzy.html` the result is `<script src="xyzzy.js"></script>`.

> In the second example `src` has been set to the string `'dir'`. Extended replacements are processed first, replacing `%f` with `xyzzy`, then `%s` will be replaced with `dir` resulting in `<script src="dir/xyzzy.js"></script>`.

Valid extended replacements are:

* **%f** - this will be replaced with the filename, without an extension.
* **%e** - this will be replaced with the extension including the `.` character.

#### options
Type: `object`

Expand Down
20 changes: 20 additions & 0 deletions test/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ describe('Buffer mode', function () {
src: [['js/with_tpl_2vars1.js', 'js/with_tpl_2vars2.js'], ['js/with_tpl_2vars1_2.js', 'js/with_tpl_2vars2_2.js']],
tpl: '<script data-main="%s" src="%s"></script>'
},
js_files_x_tpl: {
src: null,
tpl: '<script src="js/%f.min.js"></script>'
},
js_files_x_tpl_src: {
src: 'js',
tpl: '<script src="%s/%f.min.js"></script>'
},
js_files_x_tpl_multiple: {
src: ['js/with_tpl.js', 'js/with_tpl_2.js'],
tpl: '<script data-src="%f.data" src="%s"></script>'
},
js_files_x_tpl_2vars: {
src: [['js/with_tpl_2vars1.js', 'js/with_tpl_2vars2.js']],
tpl: '<script data-src="%f%e" data-main="%s" src="%s"></script>'
},
js_files_x_tpl_2vars_multiple: {
src: [['js/with_tpl_2vars1.js', 'js/with_tpl_2vars2.js'], ['js/with_tpl_2vars1_2.js', 'js/with_tpl_2vars2_2.js']],
tpl: '<script data-src="%f.data" data-main="%s" src="%s"></script>'
},
'lorem-ipsum': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
});

Expand Down
12 changes: 12 additions & 0 deletions test/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
<script data-main="js/with_tpl_2vars1.js" src="js/with_tpl_2vars2.js"></script>
<script data-main="js/with_tpl_2vars1_2.js" src="js/with_tpl_2vars2_2.js"></script>

<script src="js/index.min.js"></script>

<script src="js/index.min.js"></script>

<script data-src="index.data" src="js/with_tpl.js"></script>
<script data-src="index.data" src="js/with_tpl_2.js"></script>

<script data-src="index.html" data-main="js/with_tpl_2vars1.js" src="js/with_tpl_2vars2.js"></script>

<script data-src="index.data" data-main="js/with_tpl_2vars1.js" src="js/with_tpl_2vars2.js"></script>
<script data-src="index.data" data-main="js/with_tpl_2vars1_2.js" src="js/with_tpl_2vars2_2.js"></script>

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</body>
</html>
20 changes: 20 additions & 0 deletions test/fixture.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@
<script data-main="main.js" src="uncompressed.js"></script>
<!-- endbuild -->

<!-- build:js_files_x_tpl -->
<script src="uncompressed.js"></script>
<!-- endbuild -->

<!-- build:js_files_x_tpl_src -->
<script src="uncompressed.js"></script>
<!-- endbuild -->

<!-- build:js_files_x_tpl_multiple -->
<script src="uncompressed.js"></script>
<!-- endbuild -->

<!-- build:js_files_x_tpl_2vars -->
<script src="uncompressed.js"></script>
<!-- endbuild -->

<!-- build:js_files_x_tpl_2vars_multiple -->
<script src="uncompressed.js"></script>
<!-- endbuild -->

<!-- build:lorem-ipsum -->
<!-- endbuild -->
</body>
Expand Down
20 changes: 20 additions & 0 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ describe('Stream mode', function () {
src: [['js/with_tpl_2vars1.js', 'js/with_tpl_2vars2.js'], ['js/with_tpl_2vars1_2.js', 'js/with_tpl_2vars2_2.js']],
tpl: '<script data-main="%s" src="%s"></script>'
},
js_files_x_tpl: {
src: null,
tpl: '<script src="js/%f.min.js"></script>'
},
js_files_x_tpl_src: {
src: 'js',
tpl: '<script src="%s/%f.min.js"></script>'
},
js_files_x_tpl_multiple: {
src: ['js/with_tpl.js', 'js/with_tpl_2.js'],
tpl: '<script data-src="%f.data" src="%s"></script>'
},
js_files_x_tpl_2vars: {
src: [['js/with_tpl_2vars1.js', 'js/with_tpl_2vars2.js']],
tpl: '<script data-src="%f%e" data-main="%s" src="%s"></script>'
},
js_files_x_tpl_2vars_multiple: {
src: [['js/with_tpl_2vars1.js', 'js/with_tpl_2vars2.js'], ['js/with_tpl_2vars1_2.js', 'js/with_tpl_2vars2_2.js']],
tpl: '<script data-src="%f.data" data-main="%s" src="%s"></script>'
},
'lorem-ipsum': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
});

Expand Down