Skip to content

Commit 77b94f5

Browse files
committed
String include and require methods.
1 parent 5cd6e25 commit 77b94f5

21 files changed

+148
-49
lines changed

.gitignore

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,4 @@ sftp-config.json
102102
### Cloud9 ###
103103
# Cloud9 IDE - http://c9.io
104104
.c9revisions
105-
.c9
106-
107-
108-
### TypeScript definitions ###
109-
# http://definitelytyped.org/
110-
typings
105+
.c9

gulpfile.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ gulp.task('templates-concat-commonjs', function() {
1414
'sources/commonjs-open.js',
1515

1616
'sources/index.js',
17-
'sources/el/prototype.js',
18-
'sources/el/content.js',
17+
'sources/elements/prototype.js',
18+
'sources/elements/content.js',
1919
'sources/content.js',
20-
'sources/el/tag.js',
21-
'sources/el/single.js',
22-
'sources/el/double.js',
23-
'sources/el/doctype.js',
24-
'sources/el/module.js',
20+
'sources/elements/tag.js',
21+
'sources/elements/single.js',
22+
'sources/elements/double.js',
23+
'sources/elements/doctype.js',
24+
'sources/elements/module.js',
2525
'sources/doctypes.js',
2626
'sources/singles.js',
2727
'sources/doubles.js',
@@ -38,14 +38,14 @@ gulp.task('templates-concat-amd', function() {
3838
'sources/amd-open.js',
3939

4040
'sources/index.js',
41-
'sources/el/prototype.js',
42-
'sources/el/content.js',
41+
'sources/elements/prototype.js',
42+
'sources/elements/content.js',
4343
'sources/content.js',
44-
'sources/el/tag.js',
45-
'sources/el/single.js',
46-
'sources/el/double.js',
47-
'sources/el/doctype.js',
48-
'sources/el/module.js',
44+
'sources/elements/tag.js',
45+
'sources/elements/single.js',
46+
'sources/elements/double.js',
47+
'sources/elements/doctype.js',
48+
'sources/elements/module.js',
4949
'sources/doctypes.js',
5050
'sources/singles.js',
5151
'sources/doubles.js',

index.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
var _ = require('lodash');
22
var async = require('async');
3+
var fs = require('fs');
4+
5+
var _stringRequire = exports._stringRequire = function(file, path) {
6+
var m = new module.constructor();
7+
m._compile(file, path);
8+
return m.exports;
9+
};
10+
11+
var includeString = exports.includeString = function(file, path) {
12+
return exports.Module(_stringRequire(file, path));
13+
};
14+
15+
var include = exports.include = function(path, callback) {
16+
var result = asAsync(function(callback) {
17+
fs.readFile(path, 'utf-8', function(error, file) {
18+
if (error) throw new error;
19+
else {
20+
var result = includeString(file, path);
21+
callback(result);
22+
}
23+
});
24+
});
25+
if (callback) result(callback);
26+
return result;
27+
};
28+
29+
var includeSync = exports.includeSync = function(path) {
30+
return includeString(fs.readFileSync(path, 'utf-8'), path);
31+
};
332
// (argument: any) => boolean;
433
var isSync = exports.isSync = function(argument) { return _.isFunction(argument) && !!argument.__templatesSync; };
534

@@ -104,6 +133,7 @@ var parseSelector = exports.parseSelector = function(_attributes, selector) {
104133
var _stringTemplate = exports._stringTemplate = function(string, context, callback) {
105134
callback(_.template(string, context));
106135
};
136+
107137
// new () => this;
108138
var Prototype = exports.Prototype = function() {
109139

@@ -226,6 +256,14 @@ var Content = exports.Content = (new Prototype()).extend(function() {
226256
};
227257
});
228258

259+
var content = exports.content = Content().extend(function() {
260+
var parent = this._parent;
261+
this.constructor = function() {
262+
parent.constructor.apply(this);
263+
if (arguments.length > 0) this.content.apply(this, arguments);
264+
};
265+
});
266+
229267
// [new] (...arguments: Array<TSelector|IAttributes>) => this;
230268
var Tag = exports.Tag = Content().extend(function() {
231269
var parent = this._parent;
@@ -275,6 +313,7 @@ var Tag = exports.Tag = Content().extend(function() {
275313
}
276314
};
277315
});
316+
278317
// [new] (...arguments: Array<IAttributes|TSelector>) => this;
279318
var Single = exports.Single = Tag().extend(function() {
280319
var parent = this._parent;
@@ -363,7 +402,7 @@ instance._quotesLeft + instance._name + attributes + instance._quotesRight
363402
});
364403

365404
// (data: TData) => Module
366-
var Module = exports.Module = Content().extend(function() {
405+
exports.Module = Content().extend(function() {
367406
var parent = this._parent;
368407

369408
var extending = function() {
@@ -393,14 +432,6 @@ var Module = exports.Module = Content().extend(function() {
393432
};
394433
});
395434

396-
var content = exports.content = Content().extend(function() {
397-
var parent = this._parent;
398-
this.constructor = function() {
399-
parent.constructor.apply(this);
400-
if (arguments.length > 0) this.content.apply(this, arguments);
401-
};
402-
});
403-
404435
var doctypes = exports.doctypes = {};
405436

406437
doctypes.html = Doctype('[html]').extend();
@@ -409,6 +440,7 @@ doctypes.strict = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http
409440
doctypes.frameset = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"]').extend();
410441
doctypes.basic = Doctype('[html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"]').extend();
411442
doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend();
443+
412444
var _singles = exports._singles = ['br', 'hr', 'img', 'input', 'base', 'frame', 'link', 'meta', 'style'];
413445

414446
var singles = exports.singles = {};
@@ -438,6 +470,7 @@ var mixin = exports.mixin = function(reconstructor) {
438470
};
439471
});
440472
};
473+
441474
exports.with = {};
442475

443476
exports.with.Mixin = exports.Mixin;
@@ -453,4 +486,4 @@ _.extend(exports.with, exports.singles);
453486
exports.with.Single = exports.Single;
454487

455488
_.extend(exports.with, exports.doubles);
456-
exports.with.Double = exports.Double;
489+
exports.with.Double = exports.Double;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "osws-templates",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "Tools for generating, extending and rendering HTML.",
55
"keywords": [],
66
"author": "Open Source Web Standards <opensourcewebstandards@gmail.com> (http://osws.github.io/OSWS)",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# [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)
1+
# [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)
22

33
[documentation](https://github.com/OSWS/OSWS-Templates/wiki)

sources/commonjs-open.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
var _ = require('lodash');
2-
var async = require('async');
2+
var async = require('async');
3+
var fs = require('fs');
4+
5+
var _stringRequire = exports._stringRequire = function(file, path) {
6+
var m = new module.constructor();
7+
m._compile(file, path);
8+
return m.exports;
9+
};
10+
11+
var includeString = exports.includeString = function(file, path) {
12+
return exports.Module(_stringRequire(file, path));
13+
};
14+
15+
var include = exports.include = function(path, callback) {
16+
var result = asAsync(function(callback) {
17+
fs.readFile(path, 'utf-8', function(error, file) {
18+
if (error) throw new error;
19+
else {
20+
var result = includeString(file, path);
21+
callback(result);
22+
}
23+
});
24+
});
25+
if (callback) result(callback);
26+
return result;
27+
};
28+
29+
var includeSync = exports.includeSync = function(path) {
30+
return includeString(fs.readFileSync(path, 'utf-8'), path);
31+
};

sources/doctypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ doctypes.transitional = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona
55
doctypes.strict = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"').extend();
66
doctypes.frameset = Doctype('[html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"]').extend();
77
doctypes.basic = Doctype('[html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"]').extend();
8-
doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend();
8+
doctypes.mobile = Doctype('[html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"]').extend();

0 commit comments

Comments
 (0)