Skip to content

Commit

Permalink
include_once: Added new include_once option (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
t4valedeltorre authored Dec 20, 2020
1 parent 584a689 commit 191570d
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 8 deletions.
72 changes: 72 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,78 @@ result:
</html>
```

### @@include_once options - type: `JSON`

index.html
```html
<!DOCTYPE html>
<html>
<body>
@@include_once('./view.html')
@@include_once('./var.html', {
"name": "haoxin",
"age": 12345,
"socials": {
"fb": "facebook.com/include",
"tw": "twitter.com/include"
}
})
@@include_once('./var.html', {
"name": "haoxin",
"age": 12345,
"socials": {
"fb": "facebook.com/include",
"tw": "twitter.com/include"
}
})
</body>
</html>
```

view.html
```html
<h1>view</h1>
```

var.html
```html
<label>@@name</label>
<label>@@age</label>
<strong>@@socials.fb</strong>
<strong>@@socials.tw</strong>
```

gulpfile.js
```js
const fileinclude = require('gulp-file-include');
const gulp = require('gulp');

gulp.task('fileinclude', function() {
gulp.src(['index.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest('./'));
});
```

result:
```html
<!DOCTYPE html>
<html>
<body>
<h1>view</h1>
<label>haoxin</label>
<label>12345</label>
<strong>facebook.com/include</strong>
<strong>twitter.com/include</strong>

</body>
</html>
```


### filters

index.html
Expand Down
41 changes: 34 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = function(opts) {
}

var customWebRoot = !!opts.context.webRoot
var includeOnceFiles = {};

function fileInclude(file, enc, cb) {
if (!customWebRoot) {
Expand Down Expand Up @@ -72,7 +73,7 @@ module.exports = function(opts) {
return content.replace(regex, '')
}

function include(file, text, data) {
function include(file, text, data, sourceFile = '') {
var filebase = opts.basepath === '@file' ? path.dirname(file.path) : opts.basepath
var currentFilename = path.resolve(file.base, file.path)

Expand All @@ -84,26 +85,37 @@ module.exports = function(opts) {
prefix: opts.prefix,
suffix: opts.suffix,
name: 'if',
handler: conditionalHandler
handler: conditionalHandler,
sourceFile: sourceFile
})
text = replaceOperator(text, {
prefix: opts.prefix,
suffix: opts.suffix,
name: 'for',
handler: forHandler
handler: forHandler,
sourceFile: sourceFile
})
text = replaceVariable(text, data, opts)
text = replaceFunction(text, {
prefix: opts.prefix,
suffix: opts.suffix,
name: 'include_once',
handler: includeOnceHandler,
sourceFile: sourceFile
})
text = replaceFunction(text, {
prefix: opts.prefix,
suffix: opts.suffix,
name: 'include',
handler: includeHandler
handler: includeHandler,
sourceFile: sourceFile
})
text = replaceFunction(text, {
prefix: opts.prefix,
suffix: opts.suffix,
name: 'loop',
handler: loopHandler
handler: loopHandler,
sourceFile: sourceFile
})

function conditionalHandler(inst) {
Expand All @@ -128,6 +140,21 @@ module.exports = function(opts) {
return result
}

function includeOnceHandler(inst) {
var args = /[^)"']*["']([^"']*)["'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args)
if (args) {
if (typeof includeOnceFiles[inst.sourceFile] === 'undefined') {
includeOnceFiles[inst.sourceFile] = [];
}
if (includeOnceFiles[inst.sourceFile].indexOf(args[1]) === -1) {
includeOnceFiles[inst.sourceFile].push(args[1]);
return includeHandler(inst)
} else {
return '';
}
}
}

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

Expand Down Expand Up @@ -159,7 +186,7 @@ module.exports = function(opts) {
contents: Buffer.from(includeContent)
})

recFile = include(recFile, includeContent, args[3] ? JSON5.parse(args[3]) : {})
recFile = include(recFile, includeContent, args[3] ? JSON5.parse(args[3]) : {}, inst.sourceFile != '' ? inst.sourceFile : currentFilename)

return String(recFile.contents)
}
Expand Down Expand Up @@ -222,7 +249,7 @@ module.exports = function(opts) {
for (var i in arr) {
if (arr.hasOwnProperty(i)) {
var context = arr[i]
recFile = include(recFile, includeContent, args[3] ? context : {})
recFile = include(recFile, includeContent, args[3] ? context : {}, inst.sourceFile != '' ? inst.sourceFile : currentFilename)
// why handler dont reconize underscore?
// if (typeof context == 'object' && typeof context['_key'] == 'undefined') {
// context['_key'] = i;
Expand Down
3 changes: 2 additions & 1 deletion lib/replace-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = function(content, opts) {
before = content.slice(0, matchStart.index)
replacement = opts.handler({
before: before,
args: matchArg.body
args: matchArg.body,
sourceFile: opts.sourceFile
})

if (replacement !== undefined) {
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures-nested-once/index-twice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<body>
@@include_once('var.html', {
"name": "haoxin",
"age": 12345,
"socials": {
"fb": "facebook.com/include",
"tw": "twitter.com/include"
}
})
@@include_once('var.html', {
"name": "haoxin",
"age": 12345,
"socials": {
"fb": "facebook.com/include",
"tw": "twitter.com/include"
}
})
</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures-nested-once/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<body>
@@include_once('var.html', {
"name": "haoxin",
"age": 12345,
"socials": {
"fb": "facebook.com/include",
"tw": "twitter.com/include"
}
})
</body>
</html>
10 changes: 10 additions & 0 deletions test/fixtures-nested-once/result-twice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<body>
<label>haoxin</label>
<label>12345</label>
<strong>facebook.com/include</strong>
<strong>twitter.com/include</strong>

</body>
</html>
9 changes: 9 additions & 0 deletions test/fixtures-nested-once/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<body>
<label>haoxin</label>
<label>12345</label>
<strong>facebook.com/include</strong>
<strong>twitter.com/include</strong>
</body>
</html>
4 changes: 4 additions & 0 deletions test/fixtures-nested-once/var.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<label>@@name</label>
<label>@@age</label>
<strong>@@socials.fb</strong>
<strong>@@socials.tw</strong>
91 changes: 91 additions & 0 deletions test/nested-once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict'

const fileIncludePlugin = require('../lib')
const Vinyl = require('vinyl')
const should = require('should')
const fs = require('fs')

describe('## gulp-file-include', () => {
var result = fs.readFileSync('test/fixtures-nested-once/result.html', 'utf8')
var resultTwice = fs.readFileSync('test/fixtures-nested-once/result-twice.html', 'utf8')

describe('# nested once arguments', () => {
it('file', done => {
var file = new Vinyl({
path: 'test/fixtures-nested-once/index.html',
contents: fs.readFileSync('test/fixtures-nested-once/index.html')
})

var stream = fileIncludePlugin()
stream.on('data', newFile => {
should.exist(newFile)
should.exist(newFile.contents)

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

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

it('stream', done => {
var file = new Vinyl({
path: 'test/fixtures-nested-once/index.html',
contents: fs.createReadStream('test/fixtures-nested-once/index.html')
})

var stream = fileIncludePlugin()
stream.on('data', newFile => {
should.exist(newFile)
should.exist(newFile.contents)

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

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

describe('# nested once arguments twice', () => {
it('file', done => {
var file = new Vinyl({
path: 'test/fixtures-nested-once/index-twice.html',
contents: fs.readFileSync('test/fixtures-nested-once/index-twice.html')
})

var stream = fileIncludePlugin()
stream.on('data', newFile => {
should.exist(newFile)
should.exist(newFile.contents)

String(newFile.contents).should.equal(resultTwice)
done()
})

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

it('stream', done => {
var file = new Vinyl({
path: 'test/fixtures-nested-once/index-twice.html',
contents: fs.createReadStream('test/fixtures-nested-once/index-twice.html')
})

var stream = fileIncludePlugin()
stream.on('data', newFile => {
should.exist(newFile)
should.exist(newFile.contents)

String(newFile.contents).should.equal(resultTwice)
done()
})

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

0 comments on commit 191570d

Please sign in to comment.