diff --git a/.gitignore b/.gitignore index d50f8a1..710bead 100755 --- a/.gitignore +++ b/.gitignore @@ -102,9 +102,4 @@ sftp-config.json ### Cloud9 ### # Cloud9 IDE - http://c9.io .c9revisions -.c9 - - -### TypeScript definitions ### -# http://definitelytyped.org/ -typings \ No newline at end of file +.c9 \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index dd1c29e..aa8dc2b 100755 --- a/gulpfile.js +++ b/gulpfile.js @@ -14,14 +14,14 @@ gulp.task('templates-concat-commonjs', function() { 'sources/commonjs-open.js', 'sources/index.js', - 'sources/el/prototype.js', - 'sources/el/content.js', + 'sources/elements/prototype.js', + 'sources/elements/content.js', 'sources/content.js', - 'sources/el/tag.js', - 'sources/el/single.js', - 'sources/el/double.js', - 'sources/el/doctype.js', - 'sources/el/module.js', + 'sources/elements/tag.js', + 'sources/elements/single.js', + 'sources/elements/double.js', + 'sources/elements/doctype.js', + 'sources/elements/module.js', 'sources/doctypes.js', 'sources/singles.js', 'sources/doubles.js', @@ -38,14 +38,14 @@ gulp.task('templates-concat-amd', function() { 'sources/amd-open.js', 'sources/index.js', - 'sources/el/prototype.js', - 'sources/el/content.js', + 'sources/elements/prototype.js', + 'sources/elements/content.js', 'sources/content.js', - 'sources/el/tag.js', - 'sources/el/single.js', - 'sources/el/double.js', - 'sources/el/doctype.js', - 'sources/el/module.js', + 'sources/elements/tag.js', + 'sources/elements/single.js', + 'sources/elements/double.js', + 'sources/elements/doctype.js', + 'sources/elements/module.js', 'sources/doctypes.js', 'sources/singles.js', 'sources/doubles.js', diff --git a/index.js b/index.js index 71bce23..7b9f5e3 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,34 @@ var _ = require('lodash'); var async = require('async'); +var fs = require('fs'); + +var _stringRequire = exports._stringRequire = function(file, path) { + var m = new module.constructor(); + m._compile(file, path); + return m.exports; +}; + +var includeString = exports.includeString = function(file, path) { + return exports.Module(_stringRequire(file, path)); +}; + +var include = exports.include = function(path, callback) { + var result = asAsync(function(callback) { + fs.readFile(path, 'utf-8', function(error, file) { + if (error) throw new error; + else { + var result = includeString(file, path); + callback(result); + } + }); + }); + if (callback) result(callback); + return result; +}; + +var includeSync = exports.includeSync = function(path) { + return includeString(fs.readFileSync(path, 'utf-8'), path); +}; // (argument: any) => boolean; var isSync = exports.isSync = function(argument) { return _.isFunction(argument) && !!argument.__templatesSync; }; @@ -104,6 +133,7 @@ var parseSelector = exports.parseSelector = function(_attributes, selector) { var _stringTemplate = exports._stringTemplate = function(string, context, callback) { callback(_.template(string, context)); }; + // new () => this; var Prototype = exports.Prototype = function() { @@ -226,6 +256,14 @@ var Content = exports.Content = (new Prototype()).extend(function() { }; }); +var content = exports.content = Content().extend(function() { + var parent = this._parent; + this.constructor = function() { + parent.constructor.apply(this); + if (arguments.length > 0) this.content.apply(this, arguments); + }; +}); + // [new] (...arguments: Array) => this; var Tag = exports.Tag = Content().extend(function() { var parent = this._parent; @@ -275,6 +313,7 @@ var Tag = exports.Tag = Content().extend(function() { } }; }); + // [new] (...arguments: Array) => this; var Single = exports.Single = Tag().extend(function() { var parent = this._parent; @@ -363,7 +402,7 @@ instance._quotesLeft + instance._name + attributes + instance._quotesRight }); // (data: TData) => Module -var Module = exports.Module = Content().extend(function() { +exports.Module = Content().extend(function() { var parent = this._parent; var extending = function() { @@ -393,14 +432,6 @@ var Module = exports.Module = Content().extend(function() { }; }); -var content = exports.content = Content().extend(function() { - var parent = this._parent; - this.constructor = function() { - parent.constructor.apply(this); - if (arguments.length > 0) this.content.apply(this, arguments); - }; -}); - var doctypes = exports.doctypes = {}; doctypes.html = Doctype('[html]').extend(); @@ -409,6 +440,7 @@ doctypes.strict = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http doctypes.frameset = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"]').extend(); doctypes.basic = Doctype('[html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"]').extend(); doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend(); + var _singles = exports._singles = ['br', 'hr', 'img', 'input', 'base', 'frame', 'link', 'meta', 'style']; var singles = exports.singles = {}; @@ -438,6 +470,7 @@ var mixin = exports.mixin = function(reconstructor) { }; }); }; + exports.with = {}; exports.with.Mixin = exports.Mixin; @@ -453,4 +486,4 @@ _.extend(exports.with, exports.singles); exports.with.Single = exports.Single; _.extend(exports.with, exports.doubles); -exports.with.Double = exports.Double; \ No newline at end of file +exports.with.Double = exports.Double; diff --git a/package.json b/package.json index f75803b..9b35998 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "osws-templates", - "version": "0.2.4", + "version": "0.2.5", "description": "Tools for generating, extending and rendering HTML.", "keywords": [], "author": "Open Source Web Standards (http://osws.github.io/OSWS)", diff --git a/readme.md b/readme.md index f5a57c4..ca347e2 100755 --- a/readme.md +++ b/readme.md @@ -1,3 +1,3 @@ -# [OSWS](https://github.com/OSWS) [Templates](https://github.com/OSWS/OSWS-Templates) [0.2.4](https://github.com/OSWS/OSWS-Templates/wiki/0.2.4) +# [OSWS](https://github.com/OSWS) [Templates](https://github.com/OSWS/OSWS-Templates) [0.2.5](https://github.com/OSWS/OSWS-Templates/wiki/0.2.5) [documentation](https://github.com/OSWS/OSWS-Templates/wiki) \ No newline at end of file diff --git a/sources/commonjs-open.js b/sources/commonjs-open.js index 670ebcd..1fdc9a4 100644 --- a/sources/commonjs-open.js +++ b/sources/commonjs-open.js @@ -1,2 +1,31 @@ var _ = require('lodash'); -var async = require('async'); \ No newline at end of file +var async = require('async'); +var fs = require('fs'); + +var _stringRequire = exports._stringRequire = function(file, path) { + var m = new module.constructor(); + m._compile(file, path); + return m.exports; +}; + +var includeString = exports.includeString = function(file, path) { + return exports.Module(_stringRequire(file, path)); +}; + +var include = exports.include = function(path, callback) { + var result = asAsync(function(callback) { + fs.readFile(path, 'utf-8', function(error, file) { + if (error) throw new error; + else { + var result = includeString(file, path); + callback(result); + } + }); + }); + if (callback) result(callback); + return result; +}; + +var includeSync = exports.includeSync = function(path) { + return includeString(fs.readFileSync(path, 'utf-8'), path); +}; \ No newline at end of file diff --git a/sources/doctypes.js b/sources/doctypes.js index f3d7454..b6f8f75 100755 --- a/sources/doctypes.js +++ b/sources/doctypes.js @@ -5,4 +5,4 @@ doctypes.transitional = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona doctypes.strict = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"').extend(); doctypes.frameset = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"]').extend(); doctypes.basic = Doctype('[html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"]').extend(); -doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend(); \ No newline at end of file +doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend(); diff --git a/sources/el/content.js b/sources/elements/content.js similarity index 100% rename from sources/el/content.js rename to sources/elements/content.js diff --git a/sources/el/doctype.js b/sources/elements/doctype.js similarity index 100% rename from sources/el/doctype.js rename to sources/elements/doctype.js diff --git a/sources/el/double.js b/sources/elements/double.js similarity index 100% rename from sources/el/double.js rename to sources/elements/double.js diff --git a/sources/el/module.js b/sources/elements/module.js similarity index 93% rename from sources/el/module.js rename to sources/elements/module.js index 7b7110e..2d4312a 100644 --- a/sources/el/module.js +++ b/sources/elements/module.js @@ -1,5 +1,5 @@ // (data: TData) => Module -var Module = exports.Module = Content().extend(function() { +exports.Module = Content().extend(function() { var parent = this._parent; var extending = function() { diff --git a/sources/el/prototype.js b/sources/elements/prototype.js similarity index 100% rename from sources/el/prototype.js rename to sources/elements/prototype.js diff --git a/sources/el/single.js b/sources/elements/single.js similarity index 100% rename from sources/el/single.js rename to sources/elements/single.js diff --git a/sources/el/tag.js b/sources/elements/tag.js similarity index 99% rename from sources/el/tag.js rename to sources/elements/tag.js index 97f39df..0e44010 100755 --- a/sources/el/tag.js +++ b/sources/elements/tag.js @@ -46,4 +46,4 @@ var Tag = exports.Tag = Content().extend(function() { else this.attributes(arguments[a]); } }; -}); \ No newline at end of file +}); diff --git a/sources/index.js b/sources/index.js index 243af39..613bf45 100644 --- a/sources/index.js +++ b/sources/index.js @@ -101,4 +101,4 @@ var parseSelector = exports.parseSelector = function(_attributes, selector) { // (string: string, context: Object, callback: TCallback) => void; var _stringTemplate = exports._stringTemplate = function(string, context, callback) { callback(_.template(string, context)); -}; \ No newline at end of file +}; diff --git a/sources/mixin.js b/sources/mixin.js index 0fcf5fa..055cab2 100644 --- a/sources/mixin.js +++ b/sources/mixin.js @@ -12,4 +12,4 @@ var mixin = exports.mixin = function(reconstructor) { this.content(asSync(reconstructor.apply(this, arguments))); }; }); -}; \ No newline at end of file +}; diff --git a/sources/with.js b/sources/with.js index 8553e59..4b744a7 100644 --- a/sources/with.js +++ b/sources/with.js @@ -13,4 +13,4 @@ _.extend(exports.with, exports.singles); exports.with.Single = exports.Single; _.extend(exports.with, exports.doubles); -exports.with.Double = exports.Double; \ No newline at end of file +exports.with.Double = exports.Double; diff --git a/templates.js b/templates.js index 6fb11b7..ffdae98 100644 --- a/templates.js +++ b/templates.js @@ -104,6 +104,7 @@ var parseSelector = exports.parseSelector = function(_attributes, selector) { var _stringTemplate = exports._stringTemplate = function(string, context, callback) { callback(_.template(string, context)); }; + // new () => this; var Prototype = exports.Prototype = function() { @@ -226,6 +227,14 @@ var Content = exports.Content = (new Prototype()).extend(function() { }; }); +var content = exports.content = Content().extend(function() { + var parent = this._parent; + this.constructor = function() { + parent.constructor.apply(this); + if (arguments.length > 0) this.content.apply(this, arguments); + }; +}); + // [new] (...arguments: Array) => this; var Tag = exports.Tag = Content().extend(function() { var parent = this._parent; @@ -275,6 +284,7 @@ var Tag = exports.Tag = Content().extend(function() { } }; }); + // [new] (...arguments: Array) => this; var Single = exports.Single = Tag().extend(function() { var parent = this._parent; @@ -363,7 +373,7 @@ instance._quotesLeft + instance._name + attributes + instance._quotesRight }); // (data: TData) => Module -var Module = exports.Module = Content().extend(function() { +exports.Module = Content().extend(function() { var parent = this._parent; var extending = function() { @@ -393,14 +403,6 @@ var Module = exports.Module = Content().extend(function() { }; }); -var content = exports.content = Content().extend(function() { - var parent = this._parent; - this.constructor = function() { - parent.constructor.apply(this); - if (arguments.length > 0) this.content.apply(this, arguments); - }; -}); - var doctypes = exports.doctypes = {}; doctypes.html = Doctype('[html]').extend(); @@ -409,6 +411,7 @@ doctypes.strict = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http doctypes.frameset = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"]').extend(); doctypes.basic = Doctype('[html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"]').extend(); doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend(); + var _singles = exports._singles = ['br', 'hr', 'img', 'input', 'base', 'frame', 'link', 'meta', 'style']; var singles = exports.singles = {}; @@ -438,6 +441,7 @@ var mixin = exports.mixin = function(reconstructor) { }; }); }; + exports.with = {}; exports.with.Mixin = exports.Mixin; @@ -454,4 +458,5 @@ exports.with.Single = exports.Single; _.extend(exports.with, exports.doubles); exports.with.Double = exports.Double; + }); \ No newline at end of file diff --git a/templates.min.js b/templates.min.js index 6f47df8..3200659 100644 --- a/templates.min.js +++ b/templates.min.js @@ -1 +1 @@ -define(["exports","lodash","async"],function(t,n,e){var i=(t.isSync=function(t){return n.isFunction(t)&&!!t.__templatesSync},t.isAsync=function(t){return n.isFunction(t)&&!!t.__templatesAsync},t.asSync=function(t){return n.isFunction(t)&&(t.__templatesSync=!0),t}),s=t.asAsync=function(t){return n.isFunction(t)&&(t.__templatesAsync=!0),t},r=function(t,n,e){t._render(n,e)},o=t.dataRender=function(t,i,s){if(n.isFunction(t))t.prototype instanceof f?t.prototype instanceof m?r(t()(),i,s):r(t(),i,s):t.__templatesInstance instanceof f?r(t.__templatesInstance,i,s):t.__templatesAsync?t(function(t){o(t,i,s)}):t.__templatesSync?o(t(),i,s):i(t);else if(n.isObject(t))if(t instanceof f)r(t,i,s);else{var u;u=n.isArray(t)?[]:{};var a=n.keys(t);e.each(a,function(n,e){o(t[n],function(t){u[n]=t,e()},s)},function(){i(u)})}else i(t)},u=t.wrapMethod=function(t,n){return n.__templatesInstance=t,n},a=t.regExpSearch=function(t,n){for(var e=[],i=null;null!=(i=n.exec(t));)i.index===n.lastIndex&&n.lastIndex++,e.push(i);return e},c=t._selectorRegExp=/(\[)|(\])|#([-\w\d]+)|\.([-\w\d]+)|([\w\d-]+)="(['\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]*)"|([\w\d-]+)='(["\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]*)'|([\w\d-]+)=([\w\d-:\\\/\.={}<>%@#$%^&*~`]*)|("['\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]+")|('["\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]+')|([_\w-:\\\/]+)/g,h=t.parseSelector=function(t,e){var i=a(e,c),s=!1;n.each(i,function(n){if(n[1])return void(s=!0);if(n[2])return void(s=!1);if(s){if(n[9])return void(t[n[9]]=n[10]);if(n[7])return void(t[n[7]]=n[8]);if(n[5])return void(t[n[5]]=n[6]);if(n[13])return void(t[n[13]]=null);if(n[12])return void(t[n[12]]=null);if(n[11])return void(t[n[11]]=null)}else{if(n[3])return void(t.id=n[3]);if(n[4])return void(t["class"]?t["class"]+=" "+n[4]:t["class"]=n[4])}})},l=t._stringTemplate=function(t,e,i){i(n.template(t,e))},d=t.Prototype=function(){this._parent=void 0,this._arguments=void 0,this.returner=function(){return this},this.constructor=function(){},this.extend=function(t){function e(){if(!(this instanceof d)){s=arguments;var r=new e;return r.returner(r)}if(n.isArguments(s)){var o=s;s=void 0}else var o=arguments;this._parent=i,this._arguments=o,n.isFunction(t)&&t.call(this),n.isFunction(this.constructor)&&this.constructor.apply(this,o)}var i=this,s=void 0;return e.prototype=i,e}},f=t.Content=(new d).extend(function(){var t=this._parent;this._content=void 0,this.prepend=function(){return this._content.unshift.apply(this._content,arguments),this},this.content=function(){return this._content=Array.prototype.slice.call(arguments),this},this.append=function(){return this._content.push.apply(this._content,arguments),this},this._context={},this.context=function(){for(var t in arguments)n.extend(this._context,arguments[t]);return this},this.render=function(){var t=!1,e={};for(var i in arguments)n.isFunction(arguments[i])?t=arguments[i]:n.isObject(arguments[i])&&n.extend(e,arguments[i]);t&&this._render(t,e);var r=this;return s(function(t){r._render(t,e)})},this._render=function(t,e){var i=n.extend({},this._context);n.extend(i,e),o(this._content,function(n){o(i,function(e){l(n.join(""),e,t)},i)},i)},this.constructor=function(){t.constructor.apply(this),this._content=[],n.isArray(this._parent._content)&&this._content.push.apply(this._content,this._parent._content),this._context={}}}),p=t.Tag=f().extend(function(){var t=this._parent;this._name=null,this.name=function(t){return this._name=t,this},this._attributes=null,this.attributes=function(t){return n.extend(this._attributes,t),this},this.renderAttributes=function(t,e){o(this._attributes,function(e){var i="";for(var s in e)i+=n.isNull(e[s])?" "+s:" "+s+'="'+e[s]+'"';t(i)},e)},this.selector=function(t){return h(this._attributes,t),this},this.constructor=function(){t.constructor.call(this),this._attributes=n.isObject(this._parent._attributes)?n.extend({},this._parent._attributes):{};for(var e in arguments)n.isString(arguments[e])?this.selector(arguments[e]):this.attributes(arguments[e])}}),_=t.Single=p().extend(function(){var t=this._parent;this._quotesLeft="<",this._quotesRight="/>",this._render=function(n,e){var i=this;t._render(function(){i.renderAttributes(function(t){n(i._quotesLeft+i._name+t+i._quotesRight)},e)},e)}}),m=t.Double=p().extend(function(){var t=this._parent;this._quotesOpenLeft="<",this._quotesOpenRight=">",this._quotesCloseLeft="",this.returner=function(){var t=this;return u(t,function(){return arguments.length>0?t.content.apply(t,arguments):t})},this._render=function(n,e){var i=this;t._render.call(i,function(t){i.renderAttributes(function(e){n(i._quotesOpenLeft+i._name+e+i._quotesOpenRight+t+i._quotesCloseLeft+i._name+i._quotesCloseRight)},e)},e)}}),g=t.Doctype=p().extend(function(){var t=this._parent;this._name="DOCTYPE",this._quotesLeft="",this._render=function(n,e){var i=this;t._render(function(){i.renderAttributes(function(t){n(i._quotesLeft+i._name+t+i._quotesRight)},e)},e)}}),x=(t.Module=f().extend(function(){var t=this._parent,e=function(){this.constructor=function(){t.constructor.apply(this),this._result=x(n.isFunction(this._data)&&this._data.prototype instanceof q?this._data.apply(this._data,arguments):this._data)},this.returner=function(){return this}};this.constructor=function(t){this._data=t},this.returner=function(){return this.extend(e)},this.render=function(){this._result.render.apply(this._result,arguments)}}),t.content=f().extend(function(){var t=this._parent;this.constructor=function(){t.constructor.apply(this),arguments.length>0&&this.content.apply(this,arguments)}})),v=t.doctypes={};v.html=g("[html]").extend(),v.transitional=g('[html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"]').extend(),v.strict=g('[html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"').extend(),v.frameset=g('[html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"]').extend(),v.basic=g('[html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"]').extend(),v.mobile=g('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend();var b=t._singles=["br","hr","img","input","base","frame","link","meta","style"],w=t.singles={};for(var y in b)w[b[y]]=_().name(b[y]).extend();var D=t._doubles=["html","body","head","h1","h2","h3","h4","h5","h6","hgroup","div","p","address","blockquote","pre","ul","ol","li","dl","dt","dd","fieldset","legend","form","noscript","object","table","thead","tbody","tfoot","tr","td","th","col","colgroup","caption","span","b","big","strong","i","var","cite","em","q","del","s","strike","tt","code","kbd","samp","small","sub","sup","dfn","bdo","abbr","acronym","a","button","textarea","select","option","article","aside","figcaption","figure","footer","header","section","main","nav","menu","audio","video","embed","canvas","output","details","summary","mark","meter","progress","template","comment","title","script"],T=t.doubles={};for(var y in D)T[D[y]]=m()().name(D[y]).extend();{var q=t.Mixin=f().extend();t.mixin=function(t){if(!n.isFunction(t))throw new Error("reconstructor must be a function");return q().extend(function(){var n=this._parent;this.constructor=function(){n.constructor.apply(this),this.content(i(t.apply(this,arguments)))}})}}t["with"]={},t["with"].Mixin=t.Mixin,t["with"].mixin=t.mixin,t["with"].content=t.content,t["with"].Content=t.Content,t["with"].doctype=t.doctypes,t["with"].Doctype=t.Doctype,n.extend(t["with"],t.singles),t["with"].Single=t.Single,n.extend(t["with"],t.doubles),t["with"].Double=t.Double}); \ No newline at end of file +define(["exports","lodash","async"],function(t,n,e){var i=(t.isSync=function(t){return n.isFunction(t)&&!!t.__templatesSync},t.isAsync=function(t){return n.isFunction(t)&&!!t.__templatesAsync},t.asSync=function(t){return n.isFunction(t)&&(t.__templatesSync=!0),t}),s=t.asAsync=function(t){return n.isFunction(t)&&(t.__templatesAsync=!0),t},r=function(t,n,e){t._render(n,e)},o=t.dataRender=function(t,i,s){if(n.isFunction(t))t.prototype instanceof f?t.prototype instanceof g?r(t()(),i,s):r(t(),i,s):t.__templatesInstance instanceof f?r(t.__templatesInstance,i,s):t.__templatesAsync?t(function(t){o(t,i,s)}):t.__templatesSync?o(t(),i,s):i(t);else if(n.isObject(t))if(t instanceof f)r(t,i,s);else{var u;u=n.isArray(t)?[]:{};var a=n.keys(t);e.each(a,function(n,e){o(t[n],function(t){u[n]=t,e()},s)},function(){i(u)})}else i(t)},u=t.wrapMethod=function(t,n){return n.__templatesInstance=t,n},a=t.regExpSearch=function(t,n){for(var e=[],i=null;null!=(i=n.exec(t));)i.index===n.lastIndex&&n.lastIndex++,e.push(i);return e},c=t._selectorRegExp=/(\[)|(\])|#([-\w\d]+)|\.([-\w\d]+)|([\w\d-]+)="(['\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]*)"|([\w\d-]+)='(["\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]*)'|([\w\d-]+)=([\w\d-:\\\/\.={}<>%@#$%^&*~`]*)|("['\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]+")|('["\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]+')|([_\w-:\\\/]+)/g,h=t.parseSelector=function(t,e){var i=a(e,c),s=!1;n.each(i,function(n){if(n[1])return void(s=!0);if(n[2])return void(s=!1);if(s){if(n[9])return void(t[n[9]]=n[10]);if(n[7])return void(t[n[7]]=n[8]);if(n[5])return void(t[n[5]]=n[6]);if(n[13])return void(t[n[13]]=null);if(n[12])return void(t[n[12]]=null);if(n[11])return void(t[n[11]]=null)}else{if(n[3])return void(t.id=n[3]);if(n[4])return void(t["class"]?t["class"]+=" "+n[4]:t["class"]=n[4])}})},l=t._stringTemplate=function(t,e,i){i(n.template(t,e))},d=t.Prototype=function(){this._parent=void 0,this._arguments=void 0,this.returner=function(){return this},this.constructor=function(){},this.extend=function(t){function e(){if(!(this instanceof d)){s=arguments;var r=new e;return r.returner(r)}if(n.isArguments(s)){var o=s;s=void 0}else var o=arguments;this._parent=i,this._arguments=o,n.isFunction(t)&&t.call(this),n.isFunction(this.constructor)&&this.constructor.apply(this,o)}var i=this,s=void 0;return e.prototype=i,e}},f=t.Content=(new d).extend(function(){var t=this._parent;this._content=void 0,this.prepend=function(){return this._content.unshift.apply(this._content,arguments),this},this.content=function(){return this._content=Array.prototype.slice.call(arguments),this},this.append=function(){return this._content.push.apply(this._content,arguments),this},this._context={},this.context=function(){for(var t in arguments)n.extend(this._context,arguments[t]);return this},this.render=function(){var t=!1,e={};for(var i in arguments)n.isFunction(arguments[i])?t=arguments[i]:n.isObject(arguments[i])&&n.extend(e,arguments[i]);t&&this._render(t,e);var r=this;return s(function(t){r._render(t,e)})},this._render=function(t,e){var i=n.extend({},this._context);n.extend(i,e),o(this._content,function(n){o(i,function(e){l(n.join(""),e,t)},i)},i)},this.constructor=function(){t.constructor.apply(this),this._content=[],n.isArray(this._parent._content)&&this._content.push.apply(this._content,this._parent._content),this._context={}}}),p=t.content=f().extend(function(){var t=this._parent;this.constructor=function(){t.constructor.apply(this),arguments.length>0&&this.content.apply(this,arguments)}}),_=t.Tag=f().extend(function(){var t=this._parent;this._name=null,this.name=function(t){return this._name=t,this},this._attributes=null,this.attributes=function(t){return n.extend(this._attributes,t),this},this.renderAttributes=function(t,e){o(this._attributes,function(e){var i="";for(var s in e)i+=n.isNull(e[s])?" "+s:" "+s+'="'+e[s]+'"';t(i)},e)},this.selector=function(t){return h(this._attributes,t),this},this.constructor=function(){t.constructor.call(this),this._attributes=n.isObject(this._parent._attributes)?n.extend({},this._parent._attributes):{};for(var e in arguments)n.isString(arguments[e])?this.selector(arguments[e]):this.attributes(arguments[e])}}),m=t.Single=_().extend(function(){var t=this._parent;this._quotesLeft="<",this._quotesRight="/>",this._render=function(n,e){var i=this;t._render(function(){i.renderAttributes(function(t){n(i._quotesLeft+i._name+t+i._quotesRight)},e)},e)}}),g=t.Double=_().extend(function(){var t=this._parent;this._quotesOpenLeft="<",this._quotesOpenRight=">",this._quotesCloseLeft="",this.returner=function(){var t=this;return u(t,function(){return arguments.length>0?t.content.apply(t,arguments):t})},this._render=function(n,e){var i=this;t._render.call(i,function(t){i.renderAttributes(function(e){n(i._quotesOpenLeft+i._name+e+i._quotesOpenRight+t+i._quotesCloseLeft+i._name+i._quotesCloseRight)},e)},e)}}),v=t.Doctype=_().extend(function(){var t=this._parent;this._name="DOCTYPE",this._quotesLeft="",this._render=function(n,e){var i=this;t._render(function(){i.renderAttributes(function(t){n(i._quotesLeft+i._name+t+i._quotesRight)},e)},e)}});t.Module=f().extend(function(){var t=this._parent,e=function(){this.constructor=function(){t.constructor.apply(this),this._result=p(n.isFunction(this._data)&&this._data.prototype instanceof q?this._data.apply(this._data,arguments):this._data)},this.returner=function(){return this}};this.constructor=function(t){this._data=t},this.returner=function(){return this.extend(e)},this.render=function(){this._result.render.apply(this._result,arguments)}});var x=t.doctypes={};x.html=v("[html]").extend(),x.transitional=v('[html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"]').extend(),x.strict=v('[html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"').extend(),x.frameset=v('[html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"]').extend(),x.basic=v('[html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"]').extend(),x.mobile=v('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend();var b=t._singles=["br","hr","img","input","base","frame","link","meta","style"],w=t.singles={};for(var y in b)w[b[y]]=m().name(b[y]).extend();var D=t._doubles=["html","body","head","h1","h2","h3","h4","h5","h6","hgroup","div","p","address","blockquote","pre","ul","ol","li","dl","dt","dd","fieldset","legend","form","noscript","object","table","thead","tbody","tfoot","tr","td","th","col","colgroup","caption","span","b","big","strong","i","var","cite","em","q","del","s","strike","tt","code","kbd","samp","small","sub","sup","dfn","bdo","abbr","acronym","a","button","textarea","select","option","article","aside","figcaption","figure","footer","header","section","main","nav","menu","audio","video","embed","canvas","output","details","summary","mark","meter","progress","template","comment","title","script"],T=t.doubles={};for(var y in D)T[D[y]]=g()().name(D[y]).extend();{var q=t.Mixin=f().extend();t.mixin=function(t){if(!n.isFunction(t))throw new Error("reconstructor must be a function");return q().extend(function(){var n=this._parent;this.constructor=function(){n.constructor.apply(this),this.content(i(t.apply(this,arguments)))}})}}t["with"]={},t["with"].Mixin=t.Mixin,t["with"].mixin=t.mixin,t["with"].content=t.content,t["with"].Content=t.Content,t["with"].doctype=t.doctypes,t["with"].Doctype=t.Doctype,n.extend(t["with"],t.singles),t["with"].Single=t.Single,n.extend(t["with"],t.doubles),t["with"].Double=t.Double}); \ No newline at end of file diff --git a/tests/module.js b/tests/module.js new file mode 100644 index 0000000..7a7ec7c --- /dev/null +++ b/tests/module.js @@ -0,0 +1,4 @@ +var T = require('../index.js'); +var div = T.with.div; + +module.exports = div()('content'); \ No newline at end of file diff --git a/tests/test.js b/tests/test.js index 61a8dcd..5ef84fe 100755 --- a/tests/test.js +++ b/tests/test.js @@ -1,6 +1,7 @@ var Templates = require('../index.js'); var _ = require('lodash'); var assert = require('chai').assert; +var fs = require('fs'); var asSync = Templates.asSync; var asAsync = Templates.asAsync; @@ -276,4 +277,36 @@ describe('OSWS-Templates', function() { }) } }); + it('_stringRequire', function() { + assert.equal(Templates._stringRequire('module.exports = __dirname;', __dirname + '/module.js'), __dirname); + }); + it('includeString', function(done) { + Templates.includeString(fs.readFileSync(__dirname + '/module.js', 'utf-8'), __dirname + '/module.js')().render(function(result) { + assert.equal(result, '
content
'); + done(); + }); + }); + it('includeSync', function(done) { + Templates.includeSync(__dirname + '/module.js')().render(function(result) { + assert.equal(result, '
content
'); + done(); + }); + }); + it('include', function(done) { + Templates.include(__dirname + '/module.js', function(result) { + result().render(function(result) { + assert.equal(result, '
content
'); + done(); + }); + }); + }); + it('include asAsync', function(done) { + var async = Templates.include(__dirname + '/module.js'); + async(function(result) { + result().render(function(result) { + assert.equal(result, '
content
'); + done(); + }); + }); + }); }); \ No newline at end of file