Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.
Closed
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
50 changes: 12 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

var concat = require('concat-stream'),
flatten = require('flatnest').flatten,
merge = require('merge').recursive,
through = require('through2'),
gutil = require('gulp-util'),
path = require('path'),
fs = require('fs');
var concat = require('concat-stream');
var flatten = require('flatnest').flatten;
var merge = require('merge').recursive;
var through = require('through2');
var gutil = require('gulp-util');
var path = require('path');
var fs = require('fs');
var parseConditional = require('./lib/conditional');

module.exports = function(options) {
var prefix, suffix, basepath, filters, context;
Expand Down Expand Up @@ -59,38 +60,13 @@ module.exports = function(options) {
return content.replace(regex, '');
}

function parseConditionalIncludes(content, data) {
// parse @@if (something) { include('...') }
var regexp = new RegExp(prefix + '[ ]*if.*\\{[^{}]*\\}\\s*' + suffix),
matches = regexp.exec(content),
included = false;

if (!data.content) data.content = content;

while (matches) {
var match = matches[0],
includeContent = /\{([^{}]*)\}/.exec(match)[1];

// jshint ignore: start
var exp = /if(.*)\{/.exec(match)[1];
included = new Function('var context = this; with (context) { return ' + exp + '; }').call(data);
// jshint ignore: end

if (included) {
content = content.replace(match, includeContent);
} else {
content = content.replace(match, '');
}

matches = regexp.exec(content);
}

return content;
}

function include(file, text, data) {
data = merge(true, context, data || {});
text = stripCommentedIncludes(text);
text = parseConditional(text, data, {
prefix: prefix,
suffix: suffix
});

// grab keys & sort by longest keys 1st to iterate in that order
var variables = flatten(data);
Expand Down Expand Up @@ -141,8 +117,6 @@ module.exports = function(options) {
matches = includeRegExp.exec(text);
}

text = parseConditionalIncludes(text, data);

file.contents = new Buffer(text);
return file;
}
Expand Down
48 changes: 48 additions & 0 deletions lib/conditional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var balanced = require('balanced-match');

module.exports = function parse(content, data, opts) {
// parse @@if (something) { include('...') }
var regexpStart = new RegExp(opts.prefix + '[ ]*if([^{}]*)\\{');
var regexpEnd = opts.suffix ? new RegExp('^\\s*' + opts.suffix) : false;
var condition;
var result = '';
var matchStart;
var matchBody;
var matchEnd;
var startEnd;

if (!data.content) {
data.content = content;
}

while (matchStart = regexpStart.exec(content)) {
startEnd = matchStart.index + matchStart[0].length;
matchBody = balanced('{', '}', content.slice(startEnd - 1));

if (matchBody && matchBody.start === 0) {
matchEnd = regexpEnd ? regexpEnd.exec(matchBody.post) : true;

if (matchEnd) {
matchEnd = regexpEnd ? matchEnd[0].length : 0;

// jshint ignore: start
condition = new Function('var context = this; with (context) { return ' + matchStart[1] + '; }').call(data);
// jshint ignore: end

result += content.slice(0, matchStart.index);
result += condition ? parse(matchBody.body, data, opts) : '';

content = content.slice(startEnd + matchBody.end + matchEnd);

continue;
}
}

result += content.slice(0, startEnd);
content = content.slice(startEnd);
}

result += content;

return result;
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
"should": "*"
},
"dependencies": {
"balanced-match": "^0.2.0",
"concat-stream": "^1.4.7",
"flatnest": "^1.0.0",
"gulp-util": "^3.0.4",
"merge": "^1.2.0",
"through2": "^2.0.0"
},
"files": [
"lib",
"index.js",
"Readme.md"
]
Expand Down
86 changes: 32 additions & 54 deletions test/conditional.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,36 @@
'use strict';

var fileIncludePlugin = require('..'),
gutil = require('gulp-util'),
should = require('should'),
fs = require('fs');

describe('## conditional include', function() {
var result = fs.readFileSync('test/fixtures-conditional/result.html', 'utf8');

describe('# basic usage', function() {
it('file', function(done) {
var file = new gutil.File({
path: 'test/fixtures-conditional/index.html',
contents: fs.readFileSync('test/fixtures-conditional/index.html')
});

var stream = fileIncludePlugin({
context: {
name: 'c'
}
});
stream.on('data', function(newFile) {
should.exist(newFile);
should.exist(newFile.contents);

String(newFile.contents).should.equal(result);
done();
});

stream.write(file);
stream.end();
});

it('stream', function(done) {
var file = new gutil.File({
path: 'test/fixtures-conditional/index.html',
contents: fs.createReadStream('test/fixtures-conditional/index.html')
});

var stream = fileIncludePlugin({
context: {
name: 'c'
}
});
stream.on('data', function(newFile) {
should.exist(newFile);
should.exist(newFile.contents);

String(newFile.contents).should.equal(result);
done();
});

stream.write(file);
stream.end();
});
var parse = require('../lib/conditional');
var should = require('should');
var fs = require('fs');

describe('## conditional', function() {

it('# basic usage', function(done) {
var result = fs.readFileSync('test/fixtures-conditional/result-index.html', 'utf-8');
var index = fs.readFileSync('test/fixtures-conditional/index.html', 'utf-8');

parse(index, {
name: 'c'
}, {
prefix: '@@',
suffix: ''
}).should.equal(result);

done();
});

it('# with suffix', function(done) {
var result = fs.readFileSync('test/fixtures-conditional/result-suffix.html', 'utf-8');
var index = fs.readFileSync('test/fixtures-conditional/suffix.html', 'utf-8');

parse(index, {
name: 'c'
}, {
prefix: '@@',
suffix: '##'
}).should.equal(result);

done();
});
});
1 change: 0 additions & 1 deletion test/fixtures-conditional/a.html

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures-conditional/b.html

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures-conditional/c.html

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures-conditional/d.html

This file was deleted.

29 changes: 15 additions & 14 deletions test/fixtures-conditional/index.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
@@if (content.indexOf('a.html')) {
@@include('a.html')

@@if (content.indexOf('>a') > -1) {
<h1>a</h1>
}

@@if(content.indexOf('a.html')){
@@include('a.html')
@@if(content.indexOf('>a') > -1){
{<h1>a</h1>}
}

@@if content.indexOf('a.html') {
@@include('a.html')
@@if content.indexOf('>a') > -1 {
<h1>a</h1>
}

@@if (content.length > 888) {
@@include('b.html')
<h2>b</h2>
}

@@if (name === 'c') {
@@include('c.html')
@@if (name === 'c') {
<h3>c</h3>
}
}

@@include('d.html', {
"title": "haoxin"
})

@@ if (name === 'c' && context.title === 'haoxin') {
@@include('c.html')
<h3>c</h3>
}

@@ if (name === 'c' && context.title !== 'haoxin') {
@@include('c.html')
<h3>c</h3>
}

@@if(name === 'c') {
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@


<h1>a</h1>


<h1>a</h1>

{<h1>a</h1>}



<h1>a</h1>






<h3>c</h3>

<h4>haoxin</h4>





<h3>c</h3>


@@if(name === 'c') {
32 changes: 32 additions & 0 deletions test/fixtures-conditional/result-suffix.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


<h1>a</h1>



{<h1>a</h1>}



<h1>a</h1>






<h3>c</h3>






<h3>c</h3>


@@if(name === 'c') {

}

@@if(name === 'c') {
40 changes: 40 additions & 0 deletions test/fixtures-conditional/suffix.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

@@if (content.indexOf('>a') > -1) {
<h1>a</h1>
}##

@@if(content.indexOf('>a') > -1){
{<h1>a</h1>}
} ##

@@if content.indexOf('>a') > -1 {
<h1>a</h1>
}
##

@@if (content.length > 888) {
<h2>b</h2>
}

##

@@if (name === 'c') {
@@if (name === 'c') {
<h3>c</h3>
}##
}
##

@@ if (name === 'c' && context.title === 'haoxin') {
<h3>c</h3>
}##

@@ if (name === 'c' && context.title !== 'haoxin') {
<h3>c</h3>
}##

@@if(name === 'c') {

}

@@if(name === 'c') {
Loading