From 191570de6f2aa798bb4246e61441b553d97a8264 Mon Sep 17 00:00:00 2001 From: t4valedeltorre <61047937+t4valedeltorre@users.noreply.github.com> Date: Sun, 20 Dec 2020 14:58:54 +0100 Subject: [PATCH] include_once: Added new include_once option (#181) --- Readme.md | 72 ++++++++++++++++ lib/index.js | 41 ++++++++-- lib/replace-function.js | 3 +- test/fixtures-nested-once/index-twice.html | 21 +++++ test/fixtures-nested-once/index.html | 13 +++ test/fixtures-nested-once/result-twice.html | 10 +++ test/fixtures-nested-once/result.html | 9 ++ test/fixtures-nested-once/var.html | 4 + test/nested-once.js | 91 +++++++++++++++++++++ 9 files changed, 256 insertions(+), 8 deletions(-) create mode 100644 test/fixtures-nested-once/index-twice.html create mode 100644 test/fixtures-nested-once/index.html create mode 100644 test/fixtures-nested-once/result-twice.html create mode 100644 test/fixtures-nested-once/result.html create mode 100644 test/fixtures-nested-once/var.html create mode 100644 test/nested-once.js diff --git a/Readme.md b/Readme.md index d5a986c..06dad9a 100644 --- a/Readme.md +++ b/Readme.md @@ -136,6 +136,78 @@ result: ``` +### @@include_once options - type: `JSON` + +index.html +```html + + + + @@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" + } + }) + + +``` + +view.html +```html +

view

+``` + +var.html +```html + + +@@socials.fb +@@socials.tw +``` + +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 + + + +

view

+ + +facebook.com/include +twitter.com/include + + + +``` + + ### filters index.html diff --git a/lib/index.js b/lib/index.js index d4849cd..89168d8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -32,6 +32,7 @@ module.exports = function(opts) { } var customWebRoot = !!opts.context.webRoot + var includeOnceFiles = {}; function fileInclude(file, enc, cb) { if (!customWebRoot) { @@ -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) @@ -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) { @@ -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) @@ -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) } @@ -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; diff --git a/lib/replace-function.js b/lib/replace-function.js index d5a5ed5..6b24b9c 100644 --- a/lib/replace-function.js +++ b/lib/replace-function.js @@ -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) { diff --git a/test/fixtures-nested-once/index-twice.html b/test/fixtures-nested-once/index-twice.html new file mode 100644 index 0000000..41a6664 --- /dev/null +++ b/test/fixtures-nested-once/index-twice.html @@ -0,0 +1,21 @@ + + + + @@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" + } + }) + + \ No newline at end of file diff --git a/test/fixtures-nested-once/index.html b/test/fixtures-nested-once/index.html new file mode 100644 index 0000000..4cc8f4d --- /dev/null +++ b/test/fixtures-nested-once/index.html @@ -0,0 +1,13 @@ + + + + @@include_once('var.html', { + "name": "haoxin", + "age": 12345, + "socials": { + "fb": "facebook.com/include", + "tw": "twitter.com/include" + } + }) + + \ No newline at end of file diff --git a/test/fixtures-nested-once/result-twice.html b/test/fixtures-nested-once/result-twice.html new file mode 100644 index 0000000..2aa039f --- /dev/null +++ b/test/fixtures-nested-once/result-twice.html @@ -0,0 +1,10 @@ + + + + + +facebook.com/include +twitter.com/include + + + \ No newline at end of file diff --git a/test/fixtures-nested-once/result.html b/test/fixtures-nested-once/result.html new file mode 100644 index 0000000..6de001f --- /dev/null +++ b/test/fixtures-nested-once/result.html @@ -0,0 +1,9 @@ + + + + + +facebook.com/include +twitter.com/include + + \ No newline at end of file diff --git a/test/fixtures-nested-once/var.html b/test/fixtures-nested-once/var.html new file mode 100644 index 0000000..e955ae2 --- /dev/null +++ b/test/fixtures-nested-once/var.html @@ -0,0 +1,4 @@ + + +@@socials.fb +@@socials.tw \ No newline at end of file diff --git a/test/nested-once.js b/test/nested-once.js new file mode 100644 index 0000000..4795e02 --- /dev/null +++ b/test/nested-once.js @@ -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() + }) + }) +})