Skip to content

Commit

Permalink
Merge pull request #9 from jonschlinkert/dev
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
jonschlinkert committed Oct 9, 2015
2 parents 89488d4 + beb86e5 commit 5dc2083
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 80 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ var item = new Item({
});
```

### [.clone](lib/item.js#L88)
### [.clone](lib/item.js#L85)

Re-decorate Item methods after calling vinyl's `.clone()` method.

Expand Down Expand Up @@ -1194,7 +1194,7 @@ $ npm i -d && npm test

## Code coverage

As of October 08, 2015, code coverage is 100%.
As of October 09, 2015, code coverage is 100%.

```sh
Statements : 100% (1162/1162)
Expand Down Expand Up @@ -1223,4 +1223,4 @@ Released under the MIT license.

***

_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 08, 2015._
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 09, 2015._
17 changes: 16 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
var path = require('path');
var templates = require('./');
var app = templates();
var green = require('ansi-green');
var yellow = require('ansi-yellow');
var red = require('ansi-red');


app.use(function (app) {
app.on('error', function (err) {
console.log('app:', red(err));
Expand Down Expand Up @@ -75,7 +77,6 @@ app.section('articles')
console.log(res.content)
})

// console.log(app.views.articles)

/**
* Events
Expand All @@ -87,4 +88,18 @@ posts.engine('html', require('engine-base'));
posts.addView('home.html', {content: 'The <%= title %> page'})
.render({title: 'Home'}, function (err, res) {
console.log(res.content);
});



/**
* Loading templates
*/

app.create('docs', {cwd: 'test/fixtures'})
.option('renameKey', function (key) {
return path.basename(key);
})
.loadViews('templates/*.tmpl')

console.log(app.views.docs)
8 changes: 3 additions & 5 deletions lib/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ function Item(item) {
this.locals = item.locals || {};
this.data = item.data || {};

this.define('_name', item._name || 'Item');
this.define('_contents', null);
this.define('_content', null);
this.define('contents', {
configurable: true,
enumerable: false,
Expand Down Expand Up @@ -89,7 +86,7 @@ Item.prototype.clone = function (opts) {
opts = opts || {};

if (typeof opts === 'boolean') {
opts = {deep: true};
opts = { deep: true };
}

opts.deep = opts.deep === true;
Expand Down Expand Up @@ -126,6 +123,7 @@ Item.prototype.clone = function (opts) {
*/

Item.prototype.inspect = function () {
var name = this._name || 'Item';
var inspect = [];

// use relative path if possible
Expand All @@ -142,7 +140,7 @@ Item.prototype.inspect = function () {
if (this.isStream()) {
inspect.push(inspectStream(this.contents));
}
return '<' + this._name + ' ' + inspect.join(' ') + '>';
return '<' + name + ' ' + inspect.join(' ') + '>';
};

/**
Expand Down
12 changes: 0 additions & 12 deletions lib/utils/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var fs = require('fs');
var keys = require('./keys');

/**
Expand Down Expand Up @@ -58,17 +57,6 @@ utils.isObject = function(val) {
|| typeof val === 'object';
};

/**
* Try to read a file, fail gracefully
*/

utils.tryRead = function(fp) {
try {
return fs.readFileSync(fp);
} catch(err) {}
return null;
};

/**
* Return true if the given value is a stream.
*/
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* Lazily required module dependencies
*/

var lazy = require('lazy-cache')(require);
var utils = require('lazy-cache')(require);
var fn = require;

/**
* Common utils
*/

require = lazy;
require = utils;
require('clone');
require('is-buffer');
require('paginationator');
Expand All @@ -33,16 +33,17 @@ require('en-route', 'router');
* Engines, templates and helpers utils
*/

require('load-templates', 'load');
require('load-helpers', 'loader');
require('engine-base', 'engine');
require('engine-cache', 'Engines');
require('template-error', 'rethrow');
require('inflection', 'inflect');
require('layouts');
require = fn;

/**
* Expose utils
*/

require = fn;
module.exports = lazy;
module.exports = utils;
5 changes: 5 additions & 0 deletions lib/utils/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ utils.isItem = utils.isView = function(val) {
*/

utils.syncContents = function(view, contents) {
if (typeof view._contents === 'undefined') {
view.define('_contents', null);
view.define('_content', null);
}

if (contents === null) {
view._contents = null;
view._content = null;
Expand Down
4 changes: 2 additions & 2 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = View;

function View(view) {
Item.call(this, view);
this.is('View');
this.define('_name', 'View');
this.define('isView', true);
this._name = 'View';
delete this.isItem;
}

Expand Down
33 changes: 2 additions & 31 deletions lib/views.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var path = require('path');
var plugin = require('./plugins');
var utils = require('./utils');
var Base = require('./base');
Expand Down Expand Up @@ -52,7 +51,8 @@ plugin.errors(Views.prototype, 'Views');
*/

Views.prototype.init = function(opts) {
this.is('Collection');
this.define('isCollection', true);
this.define('isViews', true);

// decorate the instance
this.use(plugin.init);
Expand Down Expand Up @@ -196,35 +196,6 @@ Views.prototype.addList = function(list, fn) {
return this;
};

/**
* Loads and create a new `View` from the file system.
*
* @param {String} `filename`
* @param {Object} `options`
* @return {Object} Returns view object
* @api public
*/

Views.prototype.loadView = function(filename, options) {
var opts = utils.merge({cwd: ''}, this.options, options);
var View = this.get('View');

var extname = path.extname(filename);
var name = path.basename(filename, extname);
var fp = path.resolve(opts.cwd, name);
var ext = opts.ext || extname || '';
var str = utils.tryRead(fp + ext) || utils.tryRead(fp);

var view = new View({
path: fp + ext,
name: name,
ext: ext,
content: str
});

return this.addView(view);
};

/**
* Get a view from the collection.
*
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "templates",
"description": "System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.",
"version": "0.2.8",
"version": "0.3.0",
"homepage": "https://github.com/jonschlinkert/templates",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"repository": "jonschlinkert/templates",
Expand Down Expand Up @@ -41,6 +41,7 @@
"layouts": "^0.10.6",
"lazy-cache": "^0.2.3",
"load-helpers": "^0.2.8",
"load-templates": "^0.9.1",
"mixin-deep": "^1.1.3",
"paginationator": "^0.1.2",
"resolve-glob": "^0.1.2",
Expand All @@ -57,7 +58,6 @@
"engine-handlebars": "^0.8.0",
"event-stream": "^3.3.1",
"get-value": "^1.2.1",
"globby": "^2.0.0",
"gulp": "^3.9.0",
"gulp-istanbul": "^0.10.0",
"gulp-jshint": "^1.11.2",
Expand Down
1 change: 1 addition & 0 deletions test/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* deps: coveralls istanbul */
require('mocha');
require('should');
var assert = require('assert');
Expand Down
20 changes: 0 additions & 20 deletions test/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,6 @@ describe('views', function () {
});
});

describe('loadView', function() {
beforeEach(function() {
collection = new Views({
renameKey: function (key) {
return path.basename(key);
}
});
});

it('should load a file and add it to `views`:', function () {
collection.loadView('test/fixtures/templates/a.tmpl');
collection.views.should.have.property('a.tmpl');
});

it('should handle files with no extension:', function () {
collection.loadView('test/fixtures/noext/license');
collection.views.should.have.property('license');
});
});

describe('addViews', function() {
beforeEach(function() {
collection = new Views();
Expand Down

0 comments on commit 5dc2083

Please sign in to comment.