Comparing version 0.0.3 to 0.0.4
92
index.js
@@ -6,38 +6,55 @@ /** | ||
var Swig = require('swig').Swig; | ||
var path = require('path'); | ||
var util = require("util"); | ||
var EventEmitter = require("events").EventEmitter; | ||
var tags = [ | ||
"script", | ||
"style", | ||
"html", | ||
"body", | ||
"require", | ||
"uri", | ||
"widget", | ||
"head" | ||
]; | ||
module.exports = function (res, options) { | ||
if (!res.fis) { | ||
throw new Error('must `yog-resource-api` is loaded.'); | ||
} | ||
if (!options || !options['viewdir']) { | ||
throw new Error('must set `options.viewdir`.'); | ||
Swig.prototype._w = Swig.prototype._widget = function(api, id, attr, options) { | ||
var self = this; | ||
var pathname = api.resolve(id); | ||
if (!api.supportBigPipe() || !attr.mode || attr.mode === 'sync') { | ||
api.load(id); | ||
return this.compileFile(pathname, options); | ||
} | ||
//add responseWriter to the context of swig. | ||
Swig.prototype._r = function () { | ||
return res; | ||
}; | ||
return function(locals) { | ||
Swig.prototype._compileFile = function (id, w_args, opt) { | ||
opt.resolveFrom = ''; | ||
var p = path.join(options['viewdir'], res.fis.load(id)); | ||
return this.compileFile(p, opt); | ||
api.addPagelet({ | ||
id: attr.id, | ||
mode: attr.mode, | ||
locals: locals, | ||
view: pathname, | ||
sourceId: id, | ||
compiled: function(locals) { | ||
var fn = self.compileFile(pathname, options); | ||
locals._yog && locals._yog.load(id); | ||
return fn.apply(this, arguments); | ||
} | ||
}); | ||
return '<div id="' + attr.id + '"></div>'; | ||
}; | ||
} | ||
var swig = new Swig(options); | ||
var SwigWrap = module.exports = function SwigWrap(options) { | ||
var tags = [ | ||
"script", | ||
"style", | ||
"html", | ||
"body", | ||
"require", | ||
"uri", | ||
"widget", | ||
"head" | ||
]; | ||
if (!(this instanceof SwigWrap)) { | ||
return new SwigWrap(options); | ||
} | ||
var self = this; | ||
var swig = this.swig = new Swig(options); | ||
tags.forEach(function (tag) { | ||
@@ -48,3 +65,22 @@ var t = require('./tags/' + tag); | ||
return swig; | ||
EventEmitter.call(this); | ||
}; | ||
util.inherits(SwigWrap, EventEmitter); | ||
SwigWrap.prototype.renderFile = function(path, data) { | ||
var self = this; | ||
this.swig.renderFile(path, data, function(err, output) { | ||
if (err) { | ||
return self.emit('error', error); | ||
} | ||
// 这里支持 chunk 输出内容。 | ||
// 可以先输出部分,如: | ||
// self.emit('data', 'chunk content'); | ||
// self.emit('flush'); | ||
self.emit('end', output); | ||
}); | ||
}; |
{ | ||
"name": "yog-swig", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "custom swig template, add some tag or filter ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,6 +6,4 @@ var exports = module.exports; | ||
content.push('</html>'); | ||
var code = 'var _res = _swig._r(); _res.fis.framework = _res.fis.getUri(' + args.pop() + ');' + | ||
'_output += _res.fis.render((function() { var _output = "";' + | ||
compiler(content, parents, options, blockName) + | ||
'return _output;})());'; | ||
var code = '_ctx._yog.setFramework(_ctx._yog.getUri(' + args.pop() + '));' + | ||
compiler(content, parents, options, blockName); | ||
return code; | ||
@@ -12,0 +10,0 @@ }; |
@@ -5,5 +5,5 @@ var exports = module.exports; | ||
* trigger the load of FIS, it means add a js/css file to the page. | ||
* | ||
* | ||
* @alias require | ||
* | ||
* | ||
* @example | ||
@@ -13,7 +13,7 @@ * // if `namespace` = `user` | ||
* {%require "user:static/mod.js"%} | ||
* | ||
* | ||
* @param {string|var} id the resource `id` of the FIS system. | ||
*/ | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
var code = 'var _res = _swig._r(); _res.fis.load(' + args.pop() + ');' | ||
var code = '_ctx._yog.load(' + args.pop() + ');' | ||
return code; | ||
@@ -20,0 +20,0 @@ }; |
var exports = module.exports; | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
var code = 'var _res = _swig._r(); _res.fis.addScript((function () { var _output = "";' + | ||
compiler(content, parents, options, blockName) + | ||
var code = '_ctx._yog.addScript((function () { var _output = "";' + | ||
compiler(content, parents, options, blockName) + | ||
' return _output; })());'; | ||
@@ -7,0 +7,0 @@ return code; |
var exports = module.exports; | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
var code = 'var _res = _swig._r(); _res.fis.addStyle((function () { var _output = "";' + | ||
var code = '_ctx._yog.addStyle((function () { var _output = "";' + | ||
compiler(content, parents, options, blockName) + | ||
@@ -6,0 +6,0 @@ ' return _output; })());'; |
var exports = module.exports; | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
var code = compiler(content, parents, options, blockName); | ||
return code; | ||
return '_output += _swig._r().fis.getUri(' + args.shift() + ');'; | ||
}; | ||
@@ -7,0 +6,0 @@ |
@@ -50,3 +50,3 @@ var ignore = 'ignore', | ||
return (ignore ? ' try {\n' : '') + | ||
'_output += _swig._compileFile(' + file + ',' + JSON.stringify(w_args) + ', {' + | ||
'_output += _swig._w(_ctx._yog, '+ file+',' + JSON.stringify(w_args) + ', {' + | ||
'resolveFrom: "' + parentFile + '"' + | ||
@@ -53,0 +53,0 @@ '})(' + |
var Swig = require('../../'); | ||
var rApi = require('../../../yog/middleware/yog-resource-api'); | ||
var fs = require('fs'); | ||
var r = { | ||
fis: new rApi.ResourceApi(__dirname + '/config') | ||
fis: new rApi.ResourceApi(__dirname + '/config'), | ||
write: function (s) { | ||
console.log(s); | ||
} | ||
} | ||
@@ -11,7 +15,16 @@ | ||
console.log(swig.renderFile(__dirname + '/tpls/test01.tpl', { | ||
console.log(swig._render(__dirname + '/tpls/test01.tpl', { | ||
obj_var: { | ||
title: 'it\'s a widget' | ||
} | ||
}, { | ||
"widget01": function (done) { | ||
fs.readFile(__dirname + '/data/pagelet01.json', function (err, json) { | ||
if (err) { | ||
throw err; | ||
} | ||
done(JSON.parse(json)); | ||
}); | ||
} | ||
})); | ||
console.log(r); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
13253
315
2
1