Skip to content

Commit

Permalink
Moved conditional handler to index
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Jul 16, 2015
1 parent d33ff3c commit e141701
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
28 changes: 13 additions & 15 deletions lib/conditional.js
@@ -1,20 +1,16 @@
var balanced = require('balanced-match');

module.exports = function parse(content, data, opts) {
// parse @@if (something) { include('...') }
module.exports = function parse(inst, opts) {
var content = inst.content;
var regexpStart = new RegExp(opts.prefix + '[ ]*if([^{}]*)\\{');
var regexpEnd = opts.suffix ? new RegExp('^\\s*' + opts.suffix) : false;
var condition;
var replacement;
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));
Expand All @@ -24,17 +20,19 @@ module.exports = function parse(content, data, opts) {

if (matchEnd) {
matchEnd = regexpEnd ? matchEnd[0].length : 0;
replacement = inst.handler(matchStart[1], matchBody.body);

// 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) : '';
if(replacement !== undefined) {
result += content.slice(0, matchStart.index);
result += parse({
handler: inst.handler,
content: replacement.toString()
}, opts);

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

continue;
continue;
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/include.js
@@ -1,6 +1,7 @@
var balanced = require('balanced-match');

module.exports = function (content, opts) {
module.exports = function (inst, opts) {
var content = inst.content;
var result = '';
var reStart = new RegExp(opts.prefix + '[ ]*include\\(');
var reEnd = new RegExp('^[ ]*' + opts.suffix);
Expand All @@ -25,7 +26,7 @@ module.exports = function (content, opts) {

if(!opts.suffix || matchEnd) {
before = content.slice(0, matchStart.index);
replacement = opts.handler(matchArg.body, before);
replacement = inst.handler(matchArg.body, before);

if(replacement !== undefined) {
result += before + replacement.toString();
Expand Down
20 changes: 18 additions & 2 deletions lib/index.js
Expand Up @@ -67,11 +67,27 @@ module.exports = function(opts) {
var currentFilename = path.resolve(file.base, file.path);

data = extend(true, {}, opts.context, data || {});
data.content = text;

text = stripCommentedIncludes(text, opts);
text = replaceConditional(text, data, opts);
text = replaceInclude(text, extend({}, opts, { handler: includeHandler }));
text = replaceConditional({
handler: conditionalHandler,
content: text
}, opts);
text = replaceInclude({
handler: includeHandler,
content: text
}, opts);
text = replaceVariable(text, data, opts);

function conditionalHandler(args, body) {
// jshint ignore: start
var condition = new Function('var context = this; with (context) { return ' + args + '; }').call(data);
// jshint ignore: end

return condition ? body : '';
}

function includeHandler(args, before) {
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*({[\s\S]*?})){0,1}\s*/.exec(args);

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "gulp-file-include",
"version": "0.13.3",
"version": "0.13.4",
"description": "a gulp plugin for file include",
"main": "lib/index.js",
"scripts": {
Expand Down
28 changes: 24 additions & 4 deletions test/conditional.js
Expand Up @@ -10,8 +10,18 @@ describe('## conditional', function() {
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'
parse({
handler: function conditionalHandler(args, body) {
// jshint ignore: start
var condition = new Function('var context = this; with (context) { return ' + args + '; }').call({
content: index,
name: 'c'
});
// jshint ignore: end

return condition ? body : '';
},
content: index
}, {
prefix: '@@',
suffix: ''
Expand All @@ -24,8 +34,18 @@ describe('## conditional', function() {
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'
parse({
handler: function conditionalHandler(args, body) {
// jshint ignore: start
var condition = new Function('var context = this; with (context) { return ' + args + '; }').call({
content: index,
name: 'c'
});
// jshint ignore: end

return condition ? body : '';
},
content: index
}, {
prefix: '@@',
suffix: '##'
Expand Down

0 comments on commit e141701

Please sign in to comment.