From da2768d56bc90f09eb32c1c337d0f85d255a5f3e Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 21 May 2015 09:33:25 -0400 Subject: [PATCH] feature(bower) Util.render -> rendy --- bower.json | 3 +- lib/client/cloudcmd.js | 23 ++++++++------- lib/client/config.js | 4 ++- lib/client/dom.js | 7 +++-- lib/client/load.js | 4 ++- lib/client/view.js | 6 ++-- lib/cloudfunc.js | 3 +- lib/util.js | 29 ++----------------- modules/rendy/.bower.json | 35 ++++++++++++++++++++++ modules/rendy/ChangeLog | 59 ++++++++++++++++++++++++++++++++++++++ modules/rendy/LICENSE | 21 ++++++++++++++ modules/rendy/README.md | 52 +++++++++++++++++++++++++++++++++ modules/rendy/bower.json | 25 ++++++++++++++++ modules/rendy/gulpfile.js | 18 ++++++++++++ modules/rendy/lib/rendy.js | 43 +++++++++++++++++++++++++++ modules/rendy/package.json | 28 ++++++++++++++++++ test/lib/util.js | 12 -------- 17 files changed, 315 insertions(+), 57 deletions(-) create mode 100644 modules/rendy/.bower.json create mode 100644 modules/rendy/ChangeLog create mode 100644 modules/rendy/LICENSE create mode 100644 modules/rendy/README.md create mode 100644 modules/rendy/bower.json create mode 100644 modules/rendy/gulpfile.js create mode 100644 modules/rendy/lib/rendy.js create mode 100644 modules/rendy/package.json diff --git a/bower.json b/bower.json index 3b908b39..1ac200f3 100644 --- a/bower.json +++ b/bower.json @@ -25,6 +25,7 @@ "jquery": "~2.1.4", "menu": "~0.7.8", "promise-polyfill": "~2.0.0", - "format-io": "~0.9.6" + "format-io": "~0.9.6", + "rendy": "~1.1.0" } } diff --git a/lib/client/cloudcmd.js b/lib/client/cloudcmd.js index f4b6ee45..c22a1205 100644 --- a/lib/client/cloudcmd.js +++ b/lib/client/cloudcmd.js @@ -27,18 +27,21 @@ var CloudCmd; client + 'key' ].map(function(name) { return lib + name; + }), + + moduleFiles = [ + 'promise-polyfill/Promise', + 'format-io/lib/format', + 'rendy/lib/rendy', + ].map(function(name) { + return modules + name; }); - files.concat([ - 'promise-polyfill/Promise', - 'format-io/lib/format' - ].map(function(name) { - return modules + name; - })); - - files = files.map(function(name) { - return name + '.js'; - }); + files = moduleFiles + .concat(files) + .map(function(name) { + return name + '.js'; + }); urlFiles = getJoinURL(files); diff --git a/lib/client/config.js b/lib/client/config.js index 7fa5fd53..22c07e54 100644 --- a/lib/client/config.js +++ b/lib/client/config.js @@ -3,6 +3,8 @@ var CloudCmd, Util, DOM, io; (function(CloudCmd, Util, DOM) { 'use strict'; + /* global rendy */ + CloudCmd.Config = ConfigProto; function ConfigProto() { @@ -126,7 +128,7 @@ var CloudCmd, Util, DOM, io; obj[obj.editor + '-selected'] = 'selected'; delete obj.editor; - data = Util.render(Template, obj); + data = rendy(Template, obj); div = DOM.load({ name : 'div', className : 'config', diff --git a/lib/client/dom.js b/lib/client/dom.js index f8781d58..306c950b 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -3,6 +3,8 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; (function(Util, window) { 'use strict'; + /* global rendy */ + var DOMFunc = function() {}, DOMProto, @@ -387,7 +389,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; }); remote = remoteTmpls.map(function(tmpl) { - return Util.render(tmpl, { + return rendy(tmpl, { version: version }); }); @@ -1581,6 +1583,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; shouldAsk, sameName, ok, + tmpl = '"{{ name }}" already exist. Overwrite?', RESTful = DOM.RESTful, from = '', @@ -1627,7 +1630,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; ok = from !== to && to; if (ok && shouldAsk && sameName) - ok = Dialog.confirm(Util.render('"{{ name }}" already exist. Overwrite?', { + ok = Dialog.confirm(rendy(tmpl, { name: name })); diff --git a/lib/client/load.js b/lib/client/load.js index f9e2c432..3a56a737 100644 --- a/lib/client/load.js +++ b/lib/client/load.js @@ -3,6 +3,8 @@ var Util, DOM; (function (Util, DOM) { 'use strict'; + /* global rendy */ + var DOMProto = Object.getPrototypeOf(DOM); DOMProto.load = new LoaderProto(Util, DOM.Images, DOM.Events); @@ -51,7 +53,7 @@ var Util, DOM; var callback, template = 'file {{ src }} could not be loaded', isQuit = true, - msg = Util.render(template, { + msg = rendy(template, { src: p.src }), diff --git a/lib/client/view.js b/lib/client/view.js index 2b8870bf..1e28d8f3 100644 --- a/lib/client/view.js +++ b/lib/client/view.js @@ -3,8 +3,10 @@ var CloudCmd, Util, DOM, CloudFunc, $; (function(CloudCmd, Util, DOM, CloudFunc) { 'use strict'; + /* global rendy */ + CloudCmd.View = ViewProto; - + function ViewProto(CallBack) { var Name = 'View', Loading = false, @@ -239,7 +241,7 @@ var CloudCmd, Util, DOM, CloudFunc, $; is = isAudio(name); type = is ? 'audio' : 'video'; - rendered = Util.render(TemplateAudio, { + rendered = rendy(TemplateAudio, { src : src, type: type, name: Info.name diff --git a/lib/cloudfunc.js b/lib/cloudfunc.js index 47537f15..0c0b500a 100644 --- a/lib/cloudfunc.js +++ b/lib/cloudfunc.js @@ -7,10 +7,9 @@ var Util; if (typeof module === 'object' && module.exports) { rendy = require('rendy'); - module.exports = new CloudFuncProto(Util); } else { - rendy = Util.render; + rendy = window.rendy; scope.CloudFunc = new CloudFuncProto(Util); } diff --git a/lib/util.js b/lib/util.js index 0a24383a..7dd058a6 100644 --- a/lib/util.js +++ b/lib/util.js @@ -3,6 +3,8 @@ var Scope = scope.window ? window : global; + /* global rendy */ + if (typeof module === 'object' && module.exports) module.exports = new UtilProto(); else if (!Scope.Util) @@ -40,7 +42,7 @@ else name = names[emptyIndex]; - msg = Util.render(template, { + msg = rendy(template, { name: name }); @@ -202,31 +204,6 @@ return regExp; }; - /** - * function render template with view and own symbols - * - * @param templ - * @param data - */ - this.render = function(templ, data) { - var result = templ; - - Object - .keys(data) - .forEach(function(param) { - var name = '{{ ' + param + ' }}', - str = data[param]; - - while(~result.indexOf(name)) - result = result.replace(name, str); - }); - - if (~result.indexOf('{{')) - result = result.replace(/{{.*?}}/g, ''); - - return result; - }; - this.type = new TypeProto(); function TypeProto() { diff --git a/modules/rendy/.bower.json b/modules/rendy/.bower.json new file mode 100644 index 00000000..bad121e9 --- /dev/null +++ b/modules/rendy/.bower.json @@ -0,0 +1,35 @@ +{ + "name": "rendy", + "version": "1.1.0", + "homepage": "https://github.com/coderaiser/rendy", + "authors": [ + "coderaiser " + ], + "description": "simple template engine", + "main": "lib/rendy.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "template", + "engine", + "rendy" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "test" + ], + "_release": "1.1.0", + "_resolution": { + "type": "version", + "tag": "v1.1.0", + "commit": "4ad810d131db34863df83977afea92b6fb2f1127" + }, + "_source": "git://github.com/coderaiser/rendy.git", + "_target": "~1.1.0", + "_originalSource": "rendy", + "_direct": true +} \ No newline at end of file diff --git a/modules/rendy/ChangeLog b/modules/rendy/ChangeLog new file mode 100644 index 00000000..7fe73aaa --- /dev/null +++ b/modules/rendy/ChangeLog @@ -0,0 +1,59 @@ +2015.05.21, v1.1.0 + +feature: +- (rendy) 5 times spead up +- (package) should v6.0.1 +- (travis) add iojs, rm global gulp +- (package) scripts: test +- (test) greedy +- (package) should v5.2 +- (package) scripts: gulp + + +2015.03.13, v1.0.6 + +fix: +- (rendy) clean up regexp: lazy -> greedy + + +2015.03.13, v1.0.5 + +feature: +- (rendy) put vars in forEach +- (package) should v5.0.0 +- (bower) add + + +2015.01.26, v1.0.4 + +feature: +- (rendy) improve speed + + +2015.01.26, v1.0.3 + +feature: +- (rendy) speed up in 10 times: rm greedy quantifier +- (package) should v4.6 +- (package) should v4.4.1 + + +2014.12.12, v1.0.2 + +fix: +- (util) render: do not remove empty blocks "{{", "}}" +- (rendy) indexOf -> ~indexOf + +feature: +- (package) v1.0.1 +- (rendy) add expr + + +2014.12.12, v1.0.1 + +fix: +- (rendy) indexOf -> ~indexOf + +feature: +- (rendy) add expr + diff --git a/modules/rendy/LICENSE b/modules/rendy/LICENSE new file mode 100644 index 00000000..78aa4e7f --- /dev/null +++ b/modules/rendy/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014-2015 coderaiser + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/modules/rendy/README.md b/modules/rendy/README.md new file mode 100644 index 00000000..33addac5 --- /dev/null +++ b/modules/rendy/README.md @@ -0,0 +1,52 @@ +# Rendy [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] +Simple template engine compatible with [handlebars](http://handlebarsjs.com "Handlebars") and [mustache](https://mustache.github.io "Mustache"). + +## Install +![NPM_INFO][NPM_INFO_IMG] + +`npm i rendy --save` + +## How to use? +Rendy could be used in browser or node. + +Browser version: + +```html + + +``` + +Node version: + +```js +var rendy = require('rendy'), + Tmpl = 'hello {{ who }}'; + result = rendy(Tmpl, { + who: 'world' + }); + // returns + 'hello world' + +``` + +## License + +MIT + +[NPM_INFO_IMG]: https://nodei.co/npm/rendy.png?downloads&&stars&&downloadRank "npm install rendy" +[NPMIMGURL]: https://img.shields.io/npm/v/rendy.svg?style=flat +[BuildStatusIMGURL]: https://img.shields.io/travis/coderaiser/rendy/master.svg?style=flat +[DependencyStatusIMGURL]: https://img.shields.io/gemnasium/coderaiser/rendy.svg?style=flat +[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat +[NPMURL]: https://npmjs.org/package/rendy "npm" +[BuildStatusURL]: https://travis-ci.org/coderaiser/rendy "Build Status" +[DependencyStatusURL]: https://gemnasium.com/coderaiser/rendy "Dependency Status" +[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License" + diff --git a/modules/rendy/bower.json b/modules/rendy/bower.json new file mode 100644 index 00000000..f192df47 --- /dev/null +++ b/modules/rendy/bower.json @@ -0,0 +1,25 @@ +{ + "name": "rendy", + "version": "1.1.0", + "homepage": "https://github.com/coderaiser/rendy", + "authors": [ + "coderaiser " + ], + "description": "simple template engine", + "main": "lib/rendy.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "template", + "engine", + "rendy" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "test" + ] +} diff --git a/modules/rendy/gulpfile.js b/modules/rendy/gulpfile.js new file mode 100644 index 00000000..35b48186 --- /dev/null +++ b/modules/rendy/gulpfile.js @@ -0,0 +1,18 @@ +(function() { + 'use strict'; + + var gulp = require('gulp'), + mocha = require('gulp-mocha'); + + gulp.task('test', function() { + gulp.src('test/test.js') + .pipe(mocha({reporter: 'min'})) + .on('error', onError); + }); + + gulp.task('default', ['test']); + + function onError(error) { + console.log(error.message); + } +})(); diff --git a/modules/rendy/lib/rendy.js b/modules/rendy/lib/rendy.js new file mode 100644 index 00000000..526527c0 --- /dev/null +++ b/modules/rendy/lib/rendy.js @@ -0,0 +1,43 @@ +(function(global) { + 'use strict'; + + if (typeof module === 'object' && module.exports) + module.exports = rendy; + else + global.rendy = rendy; + + /** + * render template with data + * + * @param templ + * @param data + */ + function rendy(templ, data) { + var result = templ; + + check(templ, data); + + Object + .keys(data) + .forEach(function(param) { + var name = '{{ ' + param + ' }}', + str = data[param]; + + while(~result.indexOf(name)) + result = result.replace(name, str); + }); + + if (~result.indexOf('{{')) + result = result.replace(/{{.*?}}/g, ''); + + return result; + } + + function check(templ, data) { + if (typeof templ !== 'string') + throw(Error('template should be string!')); + + if (typeof data !== 'object') + throw(Error('data should be object!')); + } +})(this); diff --git a/modules/rendy/package.json b/modules/rendy/package.json new file mode 100644 index 00000000..fb95af5c --- /dev/null +++ b/modules/rendy/package.json @@ -0,0 +1,28 @@ +{ + "name": "rendy", + "version": "1.1.0", + "description": "simplest template engine", + "main": "lib/rendy.js", + "scripts": { + "test": "gulp test" + }, + "repository": { + "type": "git", + "url": "git://github.com/coderaiser/rendy.git" + }, + "keywords": [ + "template", + "engine" + ], + "author": "coderaiser (http://coderaiser.github.io/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/coderaiser/rendy/issues" + }, + "homepage": "https://github.com/coderaiser/rendy", + "devDependencies": { + "gulp": "~3.8.10", + "gulp-mocha": "~2.0.0", + "should": "~6.0.1" + } +} diff --git a/test/lib/util.js b/test/lib/util.js index b07ddadd..ae5635d0 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -81,18 +81,6 @@ }); }); - - describe('render', function() { - it('should render template with object of params', function() { - var WORD = 'hello world', - word = Util.render('hello {{ name }}', { - name: 'world' - }); - - WORD.should.equal(word); - }); - - }); }); })();