Skip to content

Commit

Permalink
Hexo._generate: save cache for rendered contents
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Feb 12, 2015
1 parent af46793 commit e8e45ed
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,18 @@ Hexo.prototype._generate = function(options){

var locals = new Locals(path, data);
var layoutLength = layout.length;
var cache;

function saveCache(result){
cache = result;
return result;
}

return self.execFilter('template_locals', locals, {context: self})
.then(function(locals){
route.set(path, function(){
if (cache != null) return cache;

var view, name;

for (var i = 0; i < layoutLength; i++){
Expand All @@ -364,7 +372,7 @@ Hexo.prototype._generate = function(options){

if (view){
log.debug('Rendering %s: %s', name, chalk.magenta(path));
return view.render(locals);
return view.render(locals).then(saveCache);
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/console/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ function printList(title, list){
if (nameB.length > length) length = nameB.length;
}

return nameA - nameB;
if (nameA < nameB) return -1;
else if (nameA > nameB) return 1;
else return 0;
}).forEach(function(item){
var padding = length - item.name.length + 2;
str += ' ' + chalk.bold(item.name);
Expand Down
8 changes: 7 additions & 1 deletion lib/plugins/console/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ module.exports = function(ctx){

console.register('clean', 'Removed generated files and cache.', require('./clean'));

console.register('config', 'List the current configuration.', require('./config'));
console.register('config', 'Get or set configurations.', {
usage: '[name] [value]',
arguments: [
{name: 'name', desc: 'Setting name. Leave it blank if you want to show all configurations.'},
{name: 'value', desc: 'New value of a setting. Leave it blank if you just want to show a single configuration.'}
]
}, require('./config'));

console.register('deploy', 'Deploy your website.', {
options: [
Expand Down
34 changes: 33 additions & 1 deletion test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,39 @@ describe('Hexo', function(){

it('_generate() - validate locals');

it('_generate() - do nothing if it\'s generating');
it('_generate() - do nothing if it\'s generating', function(){
var spy = sinon.spy();
hexo.extend.generator.register('test', spy);

hexo._isGenerating = true;
hexo._generate();
spy.called.should.be.false;
hexo._isGenerating = false;
});

it('_generate() - reset cache for new route', function(){
var count = 0;

hexo.theme.setView('test.swig', '{{ page.count }}');

hexo.extend.generator.register('test', function(){
return {
path: 'test',
layout: 'test',
data: {count: count++}
};
});

// First generation
return hexo._generate().then(function(){
return checkStream(route.get('test'), '0');
}).then(function(){
// Second generation
return hexo._generate();
}).then(function(){
return checkStream(route.get('test'), '1');
});
});

it('execFilter()', function(){
hexo.extend.filter.register('exec_test', function(data){
Expand Down

0 comments on commit e8e45ed

Please sign in to comment.