Skip to content
Permalink
Browse files

Hexo._generate: save cache for rendered contents

  • Loading branch information
tommy351 committed Feb 12, 2015
1 parent af46793 commit e8e45ed0f379f975147a214fbf52b2c28a6c806a
Showing with 52 additions and 4 deletions.
  1. +9 −1 lib/hexo/index.js
  2. +3 −1 lib/plugins/console/help.js
  3. +7 −1 lib/plugins/console/index.js
  4. +33 −1 test/scripts/hexo/hexo.js
@@ -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++){
@@ -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);
}
}

@@ -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);
@@ -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: [
@@ -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){

0 comments on commit e8e45ed

Please sign in to comment.
You can’t perform that action at this time.