Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jan 25, 2015
2 parents 1e07226 + 45797af commit f210770
Show file tree
Hide file tree
Showing 27 changed files with 501 additions and 184 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hexo

[![Build Status](https://travis-ci.org/hexojs/hexo.svg?branch=master)](https://travis-ci.org/hexojs/hexo) [![NPM version](https://badge.fury.io/js/hexo.svg)](http://badge.fury.io/js/hexo) [![Coverage Status](https://img.shields.io/coveralls/hexojs/hexo.svg)](https://coveralls.io/r/hexojs/hexo?branch=master) [![Build status](https://ci.appveyor.com/api/projects/status/hpx3lduqjj2t6uqq/branch/master?svg=true)](https://ci.appveyor.com/project/tommy351/hexo/branch/master)
[![Build Status](https://travis-ci.org/hexojs/hexo.svg?branch=master)](https://travis-ci.org/hexojs/hexo) [![NPM version](https://badge.fury.io/js/hexo.svg)](http://badge.fury.io/js/hexo) [![Coverage Status](https://coveralls.io/repos/hexojs/hexo/badge.svg?branch=master)](https://coveralls.io/r/hexojs/hexo?branch=master) [![Build status](https://ci.appveyor.com/api/projects/status/hpx3lduqjj2t6uqq/branch/master?svg=true)](https://ci.appveyor.com/project/tommy351/hexo/branch/master)

A fast, simple & powerful blog framework, powered by [Node.js](http://nodejs.org).

Expand Down
5 changes: 1 addition & 4 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var _ = require('lodash');
var File = require('./file');
var util = require('hexo-util');
var fs = require('hexo-fs');
var prettyHrtime = require('pretty-hrtime');
var crypto = require('crypto');
var chalk = require('chalk');

Expand Down Expand Up @@ -291,7 +290,6 @@ Box.prototype._dispatch = function(item){
var self = this;
var ctx = this.context;
var base = this.base;
var start = process.hrtime();

// Skip processing files
if (this.processingFiles[path]) return;
Expand All @@ -317,8 +315,7 @@ Box.prototype._dispatch = function(item){
return Promise.method(processor.process).call(ctx, file).thenReturn(count + 1);
}, 0).then(function(count){
if (count){
var interval = prettyHrtime(process.hrtime(start));
ctx.log.debug('Processed in %s: %s', chalk.cyan(interval), chalk.magenta(path));
ctx.log.debug('Processed: %s', chalk.magenta(path));
}
}, function(err){
ctx.log.error({err: err}, 'Process failed: %s', chalk.magenta(path));
Expand Down
15 changes: 15 additions & 0 deletions lib/extend/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ Filter.prototype.register = function(type, fn, priority){
});
};

Filter.prototype.unregister = function(type, fn){
if (!type) throw new TypeError('type is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');

var list = this.list(type);
if (!list || !list.length) return;

for (var i = 0, len = list.length; i < len; i++){
if (list[i] === fn){
list.splice(i, 1);
break;
}
}
};

Filter.prototype.exec = function(type, data, options){
options = options || {};

Expand Down
3 changes: 2 additions & 1 deletion lib/hexo/create_logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ ConsoleStream.prototype.write = function(data){

// Error
if (data.err){
msg += chalk.gray(data.err.stack || data.err.message) + '\n';
var err = data.err.stack || data.err.message;
if (err) msg += chalk.gray(err) + '\n';
}

process.stdout.write(msg);
Expand Down
12 changes: 4 additions & 8 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var Router = require('./router');
var Theme = require('../theme');
var defaultConfig = require('./default_config');
var loadDatabase = require('./load_database');
var prettyHrtime = require('pretty-hrtime');

var libDir = pathFn.dirname(__dirname);
var sep = pathFn.sep;
Expand Down Expand Up @@ -301,20 +300,17 @@ Hexo.prototype._generate = function(options){
// Run generators
return Promise.reduce(keys, function(result, key){
var generator = generators[key];
var start = process.hrtime();

return generator.call(self, siteLocals).then(function(data){
var interval = prettyHrtime(process.hrtime(start));
log.debug('Generator: %s', chalk.magenta(key));

log.debug('Generator in %s: %s', chalk.cyan(interval), chalk.magenta(key));

return data ? result.concat(data) : data;
return data ? result.concat(data) : result;
});
}, []);
})
// Add routes
.each(function(item){
// Add routes
if (item.path == null) return;
if (typeof item !== 'object' || item.path == null) return;

var path = route.format(item.path);
var data = item.data;
Expand Down
67 changes: 61 additions & 6 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,32 @@ Post.prototype.create = function(data, replace, callback){
// Get the scaffold
this._getScaffold(data.layout)
]).spread(function(path, scaffold){
// Wrap title with quotations
data.title = '"' + data.title + '"';
data.date = data.date.format('YYYY-MM-DD HH:mm:ss');

// Split data part from the raw scaffold
var split = yfm.split(scaffold);
var separator = split.separator || '---';
var jsonMode = separator[0] === ';';

var frontMatter;

if (jsonMode){
frontMatter = prepareJFM(_.clone(data));
} else {
frontMatter = prepareYFM(_.clone(data));
}

// Compile front-matter with data
var content = swig.compile(split.data)(data) + '\n';
var content = swig.compile(split.data)(frontMatter) + '\n';

// Parse front-matter
var compiled = yaml.load(content);
var compiled;

if (jsonMode){
compiled = JSON.parse('{' + content + '}');
} else {
compiled = yaml.load(content);
}

// Add data which are not in the front-matter
var keys = Object.keys(data);
Expand All @@ -89,10 +103,26 @@ Post.prototype.create = function(data, replace, callback){
}

if (Object.keys(obj).length){
content += yaml.dump(obj);
if (jsonMode){
if (content){
content = content.trim() + ',\n';
}

content += JSON.stringify(obj, null, ' ')
// Remove indention
.replace(/\n {2}/g, function(){
return '\n';
})
// Remove prefixing and trailing braces
.replace(/^{\n|}$/g, '');
} else {
content += yaml.dump(obj);
}
}

content += '---\n';
// Add separators
if (split.prefixSeparator) content = separator + '\n' + content;
content += separator + '\n';

// Concat content
content += split.content;
Expand All @@ -117,6 +147,31 @@ Post.prototype.create = function(data, replace, callback){
}).nodeify(callback);
};

// Prepare data for JSON front-matter:
// - Add quotations for strings
function prepareJFM(data){
var keys = Object.keys(data);
var key = '';
var item;

for (var i = 0, len = keys.length; i < len; i++){
key = keys[i];
item = data[key];

if (typeof item === 'string'){
data[key] = '"' + item + '"';
}
}

return data;
}

function prepareYFM(data){
data.title = '"' + data.title + '"';

return data;
}

Post.prototype._getScaffold = function(layout){
var ctx = this.context;

Expand Down
42 changes: 29 additions & 13 deletions lib/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Render.prototype.render = function(data, options, callback){

var ctx = this.context;
var self = this;
var ext = '';

return new Promise(function(resolve, reject){
if (!data) return reject(new TypeError('No input file or string!'));
Expand All @@ -40,13 +41,20 @@ Render.prototype.render = function(data, options, callback){

fs.readFile(data.path).then(resolve, reject);
}).then(function(text){
var ext = data.engine || getExtname(data.path);
data.text = text;
ext = data.engine || getExtname(data.path);
if (!ext || !self.isRenderable(ext)) return text;

var renderer = self.renderer.get(ext);
return renderer.call(ctx, {path: data.path, text: text}, options);
return renderer.call(ctx, data, options);
}).then(function(result){
return toString(result, data);
var output = self.getOutput(ext) || ext;
result = toString(result, data);

return ctx.execFilter('after_render:' + output, result, {
context: ctx,
args: [data]
});
}).nodeify(callback);
};

Expand All @@ -56,23 +64,31 @@ Render.prototype.renderSync = function(data, options){
options = options || {};

var ctx = this.context;
var text = '';

if (data.text != null){
text = data.text;
} else if (data.path){
text = fs.readFileSync(data.path);
if (data.text == null){
if (!data.path) throw new TypeError('No input file or string!');
data.text = fs.readFileSync(data.path);
}

if (text == null) throw new TypeError('No input file or string!');
if (data.text == null) throw new TypeError('No input file or string!');

var ext = data.engine || getExtname(data.path);
if (!ext || !this.isRenderableSync(ext)) return text;
var result;

if (ext && this.isRenderableSync(ext)){
var renderer = this.renderer.get(ext, true);
result = renderer.call(ctx, data, options);
} else {
result = data.text;
}

var renderer = this.renderer.get(ext, true);
var result = renderer.call(ctx, {path: data.path, text: text}, options);
var output = this.getOutput(ext) || ext;
result = toString(result, data);

return toString(result, data);
return ctx.execFilterSync('after_render:' + output, result, {
context: ctx,
args: [data]
});
};

function toString(result, options){
Expand Down
4 changes: 1 addition & 3 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ function generateConsole(args){
function generateFile(path){
var data = route.get(path);
var dest = pathFn.join(publicDir, path);
var start = process.hrtime();

// TODO: Retry when EMFILE error occurred
return fs.ensureWriteStream(dest).then(function(stream){
return pipeStream(data, stream);
}).then(function(){
var interval = prettyHrtime(process.hrtime(start));
log.info('Generated in %s: %s', chalk.cyan(interval), chalk.magenta(path));
log.info('Generated: %s', chalk.magenta(path));
});
}

Expand Down
5 changes: 0 additions & 5 deletions lib/plugins/renderer/html.js

This file was deleted.

14 changes: 8 additions & 6 deletions lib/plugins/renderer/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module.exports = function(ctx){
var renderer = ctx.extend.renderer;

var html = require('./html');
var plain = require('./plain');

renderer.register('htm', 'html', html, true);
renderer.register('html', 'html', html, true);
renderer.register('htm', 'html', plain, true);
renderer.register('html', 'html', plain, true);
renderer.register('css', 'css', plain, true);
renderer.register('js', 'js', plain, true);

renderer.register('json', 'json', require('./json'), true);

renderer.register('swig', 'html', require('./swig'), true);

var yml = require('./yaml');
var yaml = require('./yaml');

renderer.register('yml', 'json', yml, true);
renderer.register('yaml', 'json', yml, true);
renderer.register('yml', 'json', yaml, true);
renderer.register('yaml', 'json', yaml, true);
};
5 changes: 5 additions & 0 deletions lib/plugins/renderer/plain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function planRenderer(data){
return data.text;
}

module.exports = planRenderer;
4 changes: 2 additions & 2 deletions lib/plugins/renderer/yaml.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var yaml = require('js-yaml');
var escape = require('hexo-front-matter').escape;
var escapeYAML = require('hexo-front-matter').escape;

function yamlHelper(data){
return yaml.load(escape(data.text));
return yaml.load(escapeYAML(data.text));
}

module.exports = yamlHelper;
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo",
"version": "3.0.0-beta.3",
"version": "3.0.0-beta.4",
"description": "A fast, simple & powerful blog framework, powered by Node.js.",
"main": "lib/hexo",
"bin": {
Expand Down Expand Up @@ -34,7 +34,7 @@
"bunyan": "^1.2.3",
"chalk": "^0.5.1",
"cheerio": "^0.18.0",
"hexo-front-matter": "^0.1.0",
"hexo-front-matter": "^0.2.1",
"hexo-fs": "^0.1.0",
"hexo-i18n": "^0.2.0",
"hexo-util": "^0.1.0",
Expand All @@ -49,7 +49,7 @@
"swig": "1.4.2",
"tildify": "^1.0.0",
"titlecase": "^1.0.2",
"warehouse": "1.0.0-rc.4"
"warehouse": "1.0.0-rc.5"
},
"devDependencies": {
"chai": "^1.9.1",
Expand All @@ -62,6 +62,7 @@
"gulp-mocha": "^2.0.0",
"hexo-renderer-marked": "^0.2.3",
"jshint-stylish": "^1.0.0",
"mocha": "^2.0.1"
"mocha": "^2.0.1",
"sinon": "^1.12.2"
}
}
Loading

0 comments on commit f210770

Please sign in to comment.