Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made it possible to customise the split regex. #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,27 @@ gulp.task('foo', function() {

### HTML file

The plugin will look for comments of the form:
By default plugin will look for lines in the HTML like this:

`<!-- split filename.ext -->`
`<!-- split myFilename.ext -->`

Everything following one of these comments will be piped to a file named `filename.ext`,
This divider be customised by setting the `splitStr` option to a valid regex value,
where group 1 represents the desired filename. By default the following regex is used:

```js
var htmlsplit = require('gulp-htmlsplit');

gulp.task('foo', function() {
gulp.src('./*.html')
.pipe(htmlsplit(
// Where (\S+) represents the group with the file name
splitStr: /\s*<!--\s*split\s+(\S+)\s*-->\s*/g
))
.pipe(gulp.dest('build'));
})
```

Everything following one of these dividers will be piped to a file named `myFilename.ext`,
until another `split` comment is encountered, or the file ends.

If the HTML file does not begin with a `split` comment, the contents will be discarded
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function split(opts) {
var self = this;
var options = opts || {};
var stop = options.stop || 'stop';
var splitStr = options.splitStr || /\s*<!--\s*split\s+(\S+)\s*-->\s*/g;
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
Expand All @@ -23,9 +24,8 @@ function split(opts) {
var contents = file.contents.toString(enc);

// detect split comments and build a list of splits
var regex = /\s*<!--\s*split\s+(\S+)\s*-->\s*/g;
var result, splits = [];
while (result = regex.exec(contents)) {
while (result = splitStr.exec(contents)) {
splits.push({ name: result[1], start: result.index + result[0].length });
if (splits.length > 1) {
splits[splits.length - 2].end = result.index;
Expand Down