gulp-file-include
Advanced tools
Comparing version 0.14.0 to 1.0.0
107
lib/index.js
'use strict'; | ||
var replaceOperator = require('./replace-operator'); | ||
var replaceFunction = require('./replace-function'); | ||
var replaceVariable = require('./replace-variable'); | ||
var concat = require('concat-stream'); | ||
var setIndent = require('./indent'); | ||
var through = require('through2'); | ||
var gutil = require('gulp-util'); | ||
var extend = require('extend'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
const replaceOperator = require('./replace-operator'); | ||
const replaceFunction = require('./replace-function'); | ||
const replaceVariable = require('./replace-variable'); | ||
const concat = require('concat-stream'); | ||
const setIndent = require('./indent'); | ||
const through = require('through2'); | ||
const gutil = require('gulp-util'); | ||
const extend = require('extend'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
@@ -92,2 +92,8 @@ module.exports = function(opts) { | ||
}); | ||
text = replaceFunction(text, { | ||
prefix: opts.prefix, | ||
suffix: opts.suffix, | ||
name: 'loop', | ||
handler: loopHandler | ||
}); | ||
@@ -112,5 +118,63 @@ function conditionalHandler(inst) { | ||
function includeHandler(inst) { | ||
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args); | ||
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args); | ||
if (args) { | ||
if (args) { | ||
var includePath = path.resolve(filebase, args[1]); | ||
// for checking if we are not including the current file again | ||
if (currentFilename.toLowerCase() === includePath.toLowerCase()) { | ||
throw new Error('recursion detected in file: ' + currentFilename); | ||
} | ||
var includeContent = fs.readFileSync(includePath, 'utf-8'); | ||
if (opts.indent) { | ||
includeContent = setIndent(inst.before, inst.before.length, includeContent); | ||
} | ||
// need to double each `$` to escape it in the `replace` function | ||
// includeContent = includeContent.replace(/\$/gi, '$$$$'); | ||
// apply filters on include content | ||
if (typeof opts.filters === 'object') { | ||
includeContent = applyFilters(includeContent, args.input); | ||
} | ||
var recFile = new gutil.File({ | ||
cwd: process.cwd(), | ||
base: file.base, | ||
path: includePath, | ||
contents: new Buffer(includeContent) | ||
}); | ||
recFile = include(recFile, includeContent, args[3] ? JSON.parse(args[3]) : {}); | ||
return String(recFile.contents); | ||
} | ||
} | ||
function loopHandler(inst) { | ||
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*([\s\S]*())){0,1}\s*/.exec(inst.args); | ||
var arr = []; | ||
if (args) { | ||
// loop array in the json file | ||
if (args[3].match(/^('|")[^']|[^"]('|")$/)) { | ||
// clean filename var and define path | ||
var jsonfile = file.base + args[3].replace(/^('|")/, '').replace(/('|")$/, ''); | ||
// check if json file exists | ||
if (fs.existsSync(jsonfile)) { | ||
arr = require(jsonfile); | ||
} else { | ||
return console.error('JSON file not exists:', jsonfile); | ||
} | ||
} else { | ||
// loop array in the function | ||
try { | ||
arr = JSON.parse(args[3]); | ||
} catch (err) { | ||
return console.error(err, args[3]); | ||
} | ||
} | ||
if (arr) { | ||
var includePath = path.resolve(filebase, args[1]); | ||
@@ -128,5 +192,2 @@ // for checking if we are not including the current file again | ||
// need to double each `$` to escape it in the `replace` function | ||
// includeContent = includeContent.replace(/\$/gi, '$$$$'); | ||
// apply filters on include content | ||
@@ -144,7 +205,19 @@ if (typeof opts.filters === 'object') { | ||
recFile = include(recFile, includeContent, args[3] ? JSON.parse(args[3]) : {}); | ||
var contents = ''; | ||
return String(recFile.contents); | ||
for (var i in arr) { | ||
if (arr.hasOwnProperty(i)) { | ||
var context = arr[i]; | ||
recFile = include(recFile, includeContent, args[3] ? context : {}); | ||
// why handler dont reconize underscore? | ||
// if (typeof context == 'object' && typeof context['_key'] == 'undefined') { | ||
// context['_key'] = i; | ||
// } | ||
contents += String(recFile.contents); | ||
} | ||
} | ||
} | ||
return contents; | ||
} | ||
} | ||
@@ -151,0 +224,0 @@ file.contents = new Buffer(text); |
@@ -1,3 +0,5 @@ | ||
var balanced = require('balanced-match'); | ||
'use strict'; | ||
const balanced = require('balanced-match'); | ||
module.exports = function(content, opts) { | ||
@@ -4,0 +6,0 @@ var result = ''; |
@@ -1,3 +0,5 @@ | ||
var balanced = require('balanced-match'); | ||
'use strict'; | ||
const balanced = require('balanced-match'); | ||
module.exports = function parse(content, opts) { | ||
@@ -4,0 +6,0 @@ var regexpStart = new RegExp(opts.prefix + '[ ]*' + opts.name + '([^{}]*)\\{'); |
@@ -1,3 +0,5 @@ | ||
var flatten = require('flatnest').flatten; | ||
'use strict'; | ||
const flatten = require('flatnest').flatten; | ||
module.exports = function(content, data, opts) { | ||
@@ -4,0 +6,0 @@ var prefix = opts.prefix + '[ ]*'; |
{ | ||
"name": "gulp-file-include", | ||
"version": "0.14.0", | ||
"version": "1.0.0", | ||
"description": "a gulp plugin for file include", | ||
@@ -26,3 +26,4 @@ "main": "lib/index.js", | ||
"contributors": [ | ||
"Bogdan Chadkin <trysound@yandex.ru>" | ||
"Bogdan Chadkin <trysound@yandex.ru>", | ||
"Arthur Araújo <webarthur@gmail.com>" | ||
], | ||
@@ -34,13 +35,13 @@ "license": "MIT", | ||
"markdown": "0", | ||
"mocha": "2", | ||
"should": "7" | ||
"mocha": "3", | ||
"should": "11" | ||
}, | ||
"dependencies": { | ||
"balanced-match": "^0.2.1", | ||
"concat-stream": "^1.5.1", | ||
"balanced-match": "^0.4.2", | ||
"concat-stream": "^1.5.2", | ||
"extend": "^3.0.0", | ||
"flatnest": "^1.0.0", | ||
"gulp-util": "^3.0.7", | ||
"through2": "^2.0.0" | ||
"through2": "^2.0.1" | ||
} | ||
} |
@@ -180,3 +180,3 @@ [![NPM version][npm-img]][npm-url] | ||
``` | ||
```html | ||
<ul> | ||
@@ -189,2 +189,44 @@ @@for (var i = 0; i < arr.length; i++) { | ||
### `loop` statement | ||
* index.html | ||
```html | ||
<body> | ||
@@loop('loop-article.html', [ | ||
{ "title": "My post title", "text": "<p>lorem ipsum...</p>" }, | ||
{ "title": "Another post", "text": "<p>lorem ipsum...</p>" }, | ||
{ "title": "One more post", "text": "<p>lorem ipsum...</p>" } | ||
]) | ||
</body> | ||
``` | ||
* loop-article.html | ||
```html | ||
<article> | ||
<h1>@@title</h1> | ||
@@text | ||
</article> | ||
``` | ||
### `loop` statement + data.json | ||
data.json | ||
```js | ||
[ | ||
{ "title": "My post title", "text": "<p>lorem ipsum...</p>" }, | ||
{ "title": "Another post", "text": "<p>lorem ipsum...</p>" }, | ||
{ "title": "One more post", "text": "<p>lorem ipsum...</p>" } | ||
] | ||
``` | ||
* loop-article.html | ||
```html | ||
<body> | ||
@@loop("loop-article.html", "data.json") | ||
</body> | ||
``` | ||
### License | ||
@@ -191,0 +233,0 @@ MIT |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16833
320
1
245
3
+ Addedbalanced-match@0.4.2(transitive)
- Removedbalanced-match@0.2.1(transitive)
Updatedbalanced-match@^0.4.2
Updatedconcat-stream@^1.5.2
Updatedthrough2@^2.0.1