Skip to content

Commit

Permalink
lazy caching, cwd and renameKey fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Jul 26, 2015
1 parent 9c5fe4f commit 72d0ae3
Show file tree
Hide file tree
Showing 21 changed files with 392 additions and 257 deletions.
9 changes: 9 additions & 0 deletions examples/collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var App = require('..');
var app = new App();

app.collection('file', {type: 'files'});
// app.file('foo', {content: 'bar'});

// var foo = app.files.get('foo');

// console.log(app)
33 changes: 33 additions & 0 deletions examples/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var green = require('ansi-green');
var success = require('success-symbol');
var App = require('..');
var app = new App();

app.engine('*', require('engine-lodash'));

/**
* Create
*/
app.create('page');


/**
* Load
*/
app.pages('a', {path: 'a', content: 'aaa...'});
app.pages('b', {path: 'b', content: 'bbb...'});
app.pages('c', {path: 'c', content: 'ccc...'});
app.pages('d', {path: 'd', content: 'ddd...'})


var page = app.pages.get('a');

page.on('write', function (dest) {
console.log(green(success), 'file written to', dest);
});

page.on('render', function (dest) {
console.log(green(success), 'file written to', dest);
});

page.writeSync('test/actual/a.md');
22 changes: 22 additions & 0 deletions examples/mixins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var green = require('ansi-green');
var red = require('ansi-red');
var success = require('success-symbol');
var App = require('..');
var app = new App();

app.option({
mixins: {
shout: function (str) {
console.log(red(str.toUpperCase() + '!'));
}
}
});

app.shout('hello');
//=> 'HELLO!'

/**
* Create
*/
app.create('page');

92 changes: 92 additions & 0 deletions examples/paginate-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

var path = require('path');
var matter = require('parser-front-matter');
var App = require('..');
var app = new App();


/**
* Need to get the frontmatter from the contents.
*/

app.onLoad(/\.hbs$/, function (view, next) {
matter.parse(view, next);
});


/**
* Define a template engine for rendering templates
* in `.hbs` files
*/

app.engine('hbs', require('engine-handlebars'));
app.engine('md', require('engine-handlebars'));



app.helper('pager', function(locals, options, cb) {
locals = extend({modifier: ''}, locals, options.hash);

var template = [
'<ul class="pager {{modifier}}">',
' {{#is pagination.currentPage 1}}',
' <li class="pager-heading">POPULAR</li>',
' {{/is}}',
' {{#isnt pagination.currentPage 1}}',
' <li class="previous"><a href="{{relative page.dest prev.dest}}">&larr; Previous</a></li>',
' {{/isnt}}',
' {{#isnt pagination.currentPage pagination.totalPages}}',
' <li class="next"><a href="{{relative page.dest next.dest}}">Next &rarr;</a></li>',
' {{/isnt}}',
' {{#is pagination.currentPage pagination.totalPages}}',
' <li class="next disabled"><a href="{{relative page.dest next.dest}}">Next &rarr;</a></li>',
' {{/is}}',
'</ul>'
].join('\n');

this.app.render(template, locals, function (err, res) {
if (err) return cb(err);

return cb(null, res.content);
});
});

/**
* Create custom template types
*/

app.create('page');
app.create('post', { permalinks: { structure: ':year/:month/:day/:key.html'} });
app.create('include', { viewType: 'partial' });
app.create('layout', { viewType: 'layout' });

/**
* Create additional custom template type for index list pages.
* Use custom loaders to generate index pages.
* These are used just like pages, but provide the layout for a list of pages.
*/

app.create('list', {
renameKey: function (fp) {
return path.basename(fp);
}
});

/**
* Views
*/

app.pages('blog/src/*.hbs');
// app.posts('blog/src/_posts/*.md');
// app.layouts('blog/src/_layouts/*.hbs');
// app.includes('blog/src/_includes/*.hbs');

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

// var list = app.posts.list('foo')
// .pagination(function (err, post) {
// // console.log(post)
// })

console.log(app.views.posts)
43 changes: 43 additions & 0 deletions examples/view-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var App = require('..');
var app = new App();

/**
* Create a view collection: "pages"
*/
app.create('pages');

/**
* Add pages
*/

app.page('a', {path: 'pages/a.md', content: 'aaa...', data: {foo: {bar: 'Nested!'}}});
app.page('b', {path: 'pages/b.md', content: 'bbb...'});
app.page('c', {path: 'pages/c.md', content: 'ccc...'});

/**
* Get a page
*/

var pageA = app.pages.get('a');

/**
* Get a property from the page
*/

var path = pageA.get('path');
// => 'pages/a.md';

/**
* Get a nested property from the page
*/

var data = pageA.get('data.foo.bar');
//=> 'Nested!';


/**
* Get content
*/

var pageB = app.pages.get('b').content;
//=> 'bbb...';
92 changes: 57 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function Template(options) {
if (!(this instanceof Template)) {
return new Template(options);
}
this.options = options || {};
Base.call(this, options);
this.init();
}
Expand All @@ -61,25 +62,6 @@ function Template(options) {

Base.extend(Template);

/**
*
* Expose `extend`, static method for allowing other classes to inherit
* from the `Item` class (and receive all of Item's prototype methods).
*
* ```js
* function MyCustomItem(options) {...}
* Item.extend(MyCustomItem);
* ```
*
* @param {Object} `Ctor` Constructor function to extend with `Item`
* @return {undefined}
* @api public
*/

Template.extend = function(Ctor) {
util.inherits(Ctor, Template);
};

/**
* `Template` prototype methods
*/
Expand All @@ -95,9 +77,6 @@ utils.delegate(Template.prototype, {
helpers.methods(this);
lookup(this);

this.options.layoutDelims = ['{%', '%}'];
this.options.layoutTag = 'body';

// temporary.
this.define('errors', {
compile: {
Expand All @@ -117,11 +96,11 @@ utils.delegate(Template.prototype, {
this.cache.context = {};
this.views = {};

this.set('Item', require('./lib/item'));
this.set('View', require('./lib/view'));
this.set('List', require('./lib/list'));
this.set('Views', require('./lib/views'));
this.set('Collection', require('./lib/collection'));
this.define('Item', require('./lib/item'));
this.define('View', require('./lib/view'));
this.define('List', require('./lib/list'));
this.define('Views', require('./lib/views'));
this.define('Collection', require('./lib/collection'));

this.viewTypes = {
layout: [],
Expand Down Expand Up @@ -158,6 +137,19 @@ utils.delegate(Template.prototype, {
});
},

/**
* Fallback on default settings if the given property
* is not defined or doesn't match the given `type` of value.
*/

defaults: function (prop, value, type) {
var val = get(this.options, prop);
if ((!type && typeOf(val) === 'undefined') || typeOf(val) !== type) {
set(this.options, prop, value);
}
return this;
},

/**
* Load data onto `app.cache.data`
*/
Expand Down Expand Up @@ -206,7 +198,7 @@ utils.delegate(Template.prototype, {
var self = this;

utils.arrayify(methods).forEach(function (method) {
self.mixin(method, function() {
self.define(method, function() {
return loaders[method].apply(loaders, arguments);
});
});
Expand All @@ -231,22 +223,28 @@ utils.delegate(Template.prototype, {
var plural = inflect().pluralize(name);
this.inflections[single] = plural;

if (typeof opts.renameKey === 'undefined') {
if (typeof opts.renameKey === 'undefined' && this.options.renameKey) {
opts.renameKey = this.options.renameKey;
}
opts.loaderType = opts.loaderType || this.options.loaderType || 'sync';

opts.plural = plural;
opts.inflection = single;
opts.loaders = loaders;
opts.app = this;
opts = extend({}, opts, this.options);

if (!opts.loaderType) {
opts.loaderType = 'sync';
}

var Views = this.get('Views');
var views = new Views(opts);
this.viewType(plural, views.viewType());

// add custom View constructor for collection items
var ViewClass = viewFactory(single, opts);
this.set(single[0].toUpperCase() + single.slice(1), ViewClass);
var classKey = single[0].toUpperCase() + single.slice(1);
this.define(classKey, ViewClass);

// init the collection object on `views`
this.views[plural] = views;
Expand Down Expand Up @@ -518,18 +516,23 @@ utils.delegate(Template.prototype, {

// if `view` is a function, it's probably from chaining
// a collection method
if (typeof view === 'function') return view.call(this);
if (typeof view === 'function') {
return view.call(this);
}

// if `view` is a string, see if it's a cache view
if (typeof view === 'string') view = this.lookup(view);
if (typeof view === 'string') {
view = this.lookup(view);
}

locals = locals || {};

// add `locals` to `view.contexts`
view.ctx('render', locals);
var data = this.cache.data;
for (var key in locals) {
if (locals.hasOwnProperty(key) && !this.cache.data.hasOwnProperty(key)) {
this.cache.data[key] = locals[key];
if (locals.hasOwnProperty(key) && !data.hasOwnProperty(key)) {
data[key] = locals[key];
}
}

Expand Down Expand Up @@ -732,6 +735,25 @@ utils.delegate(Template.prototype, {
}
});

/**
*
* Expose `extend`, static method for allowing other classes to inherit
* from the `Item` class (and receive all of Item's prototype methods).
*
* ```js
* function MyCustomItem(options) {...}
* Item.extend(MyCustomItem);
* ```
*
* @param {Object} `Ctor` Constructor function to extend with `Item`
* @return {undefined}
* @api public
*/

Template.extend = function(Ctor) {
util.inherits(Ctor, Template);
};

/**
* Expose `Template`
*/
Expand Down
Loading

0 comments on commit 72d0ae3

Please # to comment.