Skip to content

Commit

Permalink
String include and require methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansglazunov committed Jan 30, 2015
1 parent 5cd6e25 commit 77b94f5
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 49 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,4 @@ sftp-config.json
### Cloud9 ###
# Cloud9 IDE - http://c9.io
.c9revisions
.c9


### TypeScript definitions ###
# http://definitelytyped.org/
typings
.c9
28 changes: 14 additions & 14 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
53 changes: 43 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -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; };

Expand Down Expand Up @@ -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() {

Expand Down Expand Up @@ -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<TSelector|IAttributes>) => this;
var Tag = exports.Tag = Content().extend(function() {
var parent = this._parent;
Expand Down Expand Up @@ -275,6 +313,7 @@ var Tag = exports.Tag = Content().extend(function() {
}
};
});

// [new] (...arguments: Array<IAttributes|TSelector>) => this;
var Single = exports.Single = Tag().extend(function() {
var parent = this._parent;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand All @@ -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 = {};
Expand Down Expand Up @@ -438,6 +470,7 @@ var mixin = exports.mixin = function(reconstructor) {
};
});
};

exports.with = {};

exports.with.Mixin = exports.Mixin;
Expand All @@ -453,4 +486,4 @@ _.extend(exports.with, exports.singles);
exports.with.Single = exports.Single;

_.extend(exports.with, exports.doubles);
exports.with.Double = exports.Double;
exports.with.Double = exports.Double;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <opensourcewebstandards@gmail.com> (http://osws.github.io/OSWS)",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 30 additions & 1 deletion sources/commonjs-open.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
var _ = require('lodash');
var async = require('async');
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);
};
2 changes: 1 addition & 1 deletion sources/doctypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend();
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion sources/el/module.js → sources/elements/module.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion sources/el/tag.js → sources/elements/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ var Tag = exports.Tag = Content().extend(function() {
else this.attributes(arguments[a]);
}
};
});
});
2 changes: 1 addition & 1 deletion sources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
};
2 changes: 1 addition & 1 deletion sources/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ var mixin = exports.mixin = function(reconstructor) {
this.content(asSync(reconstructor.apply(this, arguments)));
};
});
};
};
2 changes: 1 addition & 1 deletion sources/with.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ _.extend(exports.with, exports.singles);
exports.with.Single = exports.Single;

_.extend(exports.with, exports.doubles);
exports.with.Double = exports.Double;
exports.with.Double = exports.Double;
23 changes: 14 additions & 9 deletions templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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<TSelector|IAttributes>) => this;
var Tag = exports.Tag = Content().extend(function() {
var parent = this._parent;
Expand Down Expand Up @@ -275,6 +284,7 @@ var Tag = exports.Tag = Content().extend(function() {
}
};
});

// [new] (...arguments: Array<IAttributes|TSelector>) => this;
var Single = exports.Single = Tag().extend(function() {
var parent = this._parent;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand All @@ -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 = {};
Expand Down Expand Up @@ -438,6 +441,7 @@ var mixin = exports.mixin = function(reconstructor) {
};
});
};

exports.with = {};

exports.with.Mixin = exports.Mixin;
Expand All @@ -454,4 +458,5 @@ exports.with.Single = exports.Single;

_.extend(exports.with, exports.doubles);
exports.with.Double = exports.Double;

});
Loading

0 comments on commit 77b94f5

Please # to comment.