From cdb151af19401ab3b3347aeeadcd31de39284183 Mon Sep 17 00:00:00 2001 From: githunFree Date: Tue, 7 Nov 2017 23:04:28 +0800 Subject: [PATCH 1/2] more encoding support --- index.js | 63 ++++++++++++++++++++++++++-------------------------- package.json | 9 ++++---- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index bc328bf..729eb2c 100755 --- a/index.js +++ b/index.js @@ -22,6 +22,7 @@ var extend = require('extend'); var fs = require('fs'); var path = require('path'); var async = require('async'); +var iconv = require('iconv-lite'); var syntaxReg = //mg; @@ -42,7 +43,7 @@ var endifReg = //; * @since 0.1.0 * @version 0.1.2 */ -function resolve(tpl) { +function resolve (tpl) { //resolve set/echo/if var fnStr = 'var _r="";\nwith(__data){\n'; var matches, key, val, pat, eq; @@ -50,7 +51,7 @@ function resolve(tpl) { var start = 0, lastMatches; - var resolveLine = function(str) { + var resolveLine = function (str) { //This is stupid but works for "\r\b\f\u\v\n". //Here we assume line break is "\n" return str.replace(/\r/mg, '').replace(/\\/mg, '\\\\').replace(/"/mg, '\\"').replace(/\n/mg, '\\n\\\n'); @@ -122,7 +123,7 @@ function resolve(tpl) { * @since 0.1.0 * @version 0.1.0 */ -var SSI = function(initOptions) { +var SSI = function (initOptions) { this.options = extend({ baseDir: '.', encoding: 'utf-8', @@ -144,7 +145,7 @@ SSI.prototype = { * * @param {Object} config */ - setDefaultOptions: function(options) { + setDefaultOptions: function (options) { return extend(this.options, options || {}); }, /** @@ -165,7 +166,7 @@ SSI.prototype = { * @param {Object} options * @param {Function} callback */ - compile: function(content, options, callback) { + compile: function (content, options, callback) { var func; if (arguments.length < 3) { @@ -175,8 +176,8 @@ SSI.prototype = { options = extend({}, this.options, options || {}); - this.resolveIncludes(content, options, function(err, content) { - if(err) { + this.resolveIncludes(content, options, function (err, content) { + if (err) { return callback(err); } @@ -196,46 +197,45 @@ SSI.prototype = { * @param options * @param callback */ - resolveIncludes: function(content, options, callback) { + resolveIncludes: function (content, options, callback) { var matches, seg, isVirtual, basePath, tpath, subOptions, ssi = this; async.whilst( // https://www.npmjs.org/package/async#whilst-test-fn-callback- - function test() {return !!(matches = includeFileReg.exec(content)); }, - function insertInclude(next) { + function test () { return !!(matches = includeFileReg.exec(content)); }, + function insertInclude (next) { seg = matches[0]; isVirtual = RegExp.$1 == 'virtual'; - basePath = (isVirtual && options.dirname && RegExp.$3.charAt(0) !== '/')? options.dirname : options.baseDir; + basePath = (isVirtual && options.dirname && RegExp.$3.charAt(0) !== '/') ? options.dirname : options.baseDir; tpath = path.join(basePath, RegExp.$3); fs.lstat(tpath, - function(err, stats) { + function (err, stats) { if (err) { return next(err); } if (stats.isDirectory()) { tpath = tpath.replace(/(\/)?$/, '/index.html'); } - fs.readFile(tpath, { - encoding: options.encoding - }, function(err, innerContentRaw) { + fs.readFile(tpath, function (err, innerContentRaw) { + if (err) { + return next(err); + } + innerContentRaw = iconv.decode(innerContentRaw, options.encoding); + // ensure that included files can include other files with relative paths + subOptions = extend({}, options, { dirname: path.dirname(tpath) }); + ssi.resolveIncludes(innerContentRaw, subOptions, function (err, innerContent) { if (err) { return next(err); } - // ensure that included files can include other files with relative paths - subOptions = extend({}, options, {dirname: path.dirname(tpath)}); - ssi.resolveIncludes(innerContentRaw, subOptions, function(err, innerContent) { - if (err) { - return next(err); - } - content = content.slice(0, matches.index) + innerContent + content.slice(matches.index + seg.length); - next(null, content); - }); - } + content = content.slice(0, matches.index) + innerContent + content.slice(matches.index + seg.length); + next(null, content); + }); + } ); } ); }, - function includesComplete(err) { + function includesComplete (err) { if (err) { return callback(err); } @@ -249,7 +249,7 @@ SSI.prototype = { * @param {Object} options * @param {Function} callback */ - compileFile: function(filepath, options, callback) { + compileFile: function (filepath, options, callback) { if (arguments.length < 3) { callback = options; @@ -261,12 +261,13 @@ SSI.prototype = { var ssi = this; - return fs.readFile(filepath, { - encoding: options.encoding - }, function(err, content) { + return fs.readFile(filepath, function (err, content) { if (err) { return callback(err); - } else return ssi.compile(content, options, callback); + } else { + content = iconv.decode(content, options.encoding); + return ssi.compile(content, options, callback); + } }); } }; diff --git a/package.json b/package.json index 66f5869..208d62a 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "node-ssi", - "version": "0.3.2", + "name": "@tencent/node-ssi", + "version": "0.3.3", "description": "A SSI parser", "main": "index.js", "scripts": { @@ -27,6 +27,7 @@ }, "dependencies": { "async": "~1.4.2", - "extend": "~3.0.0" + "extend": "~3.0.0", + "iconv-lite": "^0.4.19" } -} +} \ No newline at end of file From f237f11ccb27aa8efc9689f6ac27f8b448e0e313 Mon Sep 17 00:00:00 2001 From: githunFree Date: Tue, 7 Nov 2017 23:07:14 +0800 Subject: [PATCH 2/2] remove namespace --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 208d62a..51ae70e 100755 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@tencent/node-ssi", + "name": "node-ssi", "version": "0.3.3", "description": "A SSI parser", "main": "index.js",