Skip to content

Commit

Permalink
Fixed date helper doesn't support timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Feb 13, 2015
1 parent 5759840 commit c313b5f
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 82 deletions.
34 changes: 19 additions & 15 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var _ = require('lodash');
var File = require('./file');
var util = require('hexo-util');
var fs = require('hexo-fs');
var crypto = require('crypto');
var chalk = require('chalk');
var through2 = require('through2');
var ShasumStream = require('./shasum_stream');

var Pattern = util.Pattern;
var escapeRegExp = util.escapeRegExp;
Expand Down Expand Up @@ -101,7 +100,11 @@ Box.prototype.process = function(callback){
if (!exist) return;
return self._loadFiles();
}).then(function(files){
if (files && files.length) return self._process(files);
if (!files || !files.length) return;

return self._process(files).finally(function(){
files.length = 0;
});
}).nodeify(callback);
};

Expand All @@ -110,8 +113,8 @@ Box.prototype.load = Box.prototype.process;
function listDir(path){
return fs.listDir(path).catch(function(err){
// Return an empty array if path does not exist
if (err.cause.code !== 'ENOENT') throw err;
return [];
if (err.cause.code === 'ENOENT') return [];
throw err;
}).map(escapeBackslash);
}

Expand Down Expand Up @@ -166,6 +169,8 @@ Box.prototype._loadFiles = function(){

return result;
}).map(function(item){
existed.length = 0;

switch (item.type){
case 'create':
case 'update':
Expand All @@ -179,15 +184,14 @@ Box.prototype._loadFiles = function(){

function getShasum(path){
return new Promise(function(resolve, reject){
var hash = crypto.createHash('sha1');
var stream = fs.createReadStream(path);

stream.pipe(through2(function(chunk, enc, callback){
hash.update(chunk);
callback();
}, function(){
resolve(hash.digest('hex'));
})).on('error', reject);
var src = fs.createReadStream(path);
var stream = new ShasumStream();

src.pipe(stream)
.on('error', reject)
.on('finish', function(){
resolve(stream.getShasum());
});
});
}

Expand Down Expand Up @@ -246,7 +250,7 @@ Box.prototype._handleDeletedFile = function(path){
if (!cache) return resolve();

ctx.log.debug('Deleted: %s', chalk.magenta(id));
return cache.remove().then(resolve, reject);
cache.remove().then(resolve, reject);
}).thenReturn({
type: 'delete',
path: path
Expand Down
32 changes: 32 additions & 0 deletions lib/box/shasum_stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var Stream = require('stream');
var Transform = Stream.Transform;
var crypto = require('crypto');

function ShasumStream(options){
Transform.call(this, options);

this._hash = crypto.createHash('sha1');
this._shasum = '';
}

require('util').inherits(ShasumStream, Transform);

ShasumStream.prototype._transform = function(chunk, enc, callback){
var buffer = chunk instanceof Buffer ? chunk : new Buffer(chunk, enc);

this._hash.update(buffer);
callback();
};

ShasumStream.prototype._flush = function(callback){
this._shasum = this._hash.digest('hex');
callback();
};

ShasumStream.prototype.getShasum = function(){
return this._shasum;
};

module.exports = ShasumStream;
11 changes: 7 additions & 4 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@ Hexo.prototype.load = function(callback){
self.theme.process()
]);
}).then(function(){
return self._generate();
return self._generate({cache: true});
}).nodeify(callback);
};

Hexo.prototype.watch = function(callback){
var self = this;

function generate(){
return self._generate({watch: true});
return self._generate({cache: false});
}

return loadDatabase(this).then(function(){
Expand Down Expand Up @@ -307,7 +307,7 @@ Hexo.prototype._generate = function(options){
Locals.prototype.theme = _.extend({}, config, theme.config, config.theme_config);
Locals.prototype._ = _;
Locals.prototype.layout = 'layout';
Locals.prototype.cache = !options.watch;
Locals.prototype.cache = options.cache;
Locals.prototype.env = this.env;
Locals.prototype.view_dir = pathFn.join(this.theme_dir, 'layout') + sep;

Expand Down Expand Up @@ -362,7 +362,7 @@ Hexo.prototype._generate = function(options){
return self.execFilter('template_locals', locals, {context: self})
.then(function(locals){
route.set(path, function(){
if (cache != null) return cache;
if (options.cache && cache != null) return cache;

var view, name;

Expand All @@ -387,6 +387,9 @@ Hexo.prototype._generate = function(options){
route.remove(removed[i]);
}

routeList.length = 0;
newRouteList.length = 0;

self.emit('generateAfter');

// Run after_generate filters
Expand Down
34 changes: 22 additions & 12 deletions lib/plugins/helper/date.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
'use strict';

var moment = require('moment');
var moment = require('moment-timezone');
var isMoment = moment.isMoment;
var isDate = require('util').isDate;

function output(date, format, lang, timezone){
if (date == null) date = moment();
if (!isMoment(date)) date = moment(isDate(date) ? date : new Date(date));

function output(date, format, lang){
if (!isMoment(date)) date = moment(date);
if (lang) date = date.locale(lang);
if (timezone) date = date.tz(timezone);

return date.format(format);
}

function toISOString(date){
if (date == null){
return new Date().toISOString();
}

if (date instanceof Date || isMoment(date)){
return date.toISOString();
} else {
return new Date(date).toISOString();
}

return new Date(date).toISOString();
}

function dateHelper(date, format){
/* jshint validthis: true */
return output(date, format || this.config.date_format, getLanguage.call(this));
var config = this.config;
return output(date, format || config.date_format, getLanguage(this), config.timezone);
}

function timeHelper(date, format){
/* jshint validthis: true */
return output(date, format || this.config.time_format, getLanguage.call(this));
var config = this.config;
return output(date, format || config.time_format, getLanguage(this), config.timezone);
}

function fullDateHelper(date, format){
/* jshint validthis: true */
if (format){
return output(date, format, getLanguage.call(this));
return output(date, format, getLanguage(this), this.config.timezone);
} else {
return this.date(date) + ' ' + this.time(date);
}
}

function timeTagHelper(date, format){
/* jshint validthis: true */
return '<time datetime="' + toISOString(date) + '">' + this.date(date, format, getLanguage.call(this)) + '</time>';
var config = this.config;
return '<time datetime="' + toISOString(date) + '">' + this.date(date, format, getLanguage(this), config.timezone) + '</time>';
}

function getLanguage(){
/* jshint validthis: true */
var lang = this.page.lang || this.page.language || this.config.language;
function getLanguage(ctx){
var lang = ctx.page.lang || ctx.page.language || ctx.config.language;
if (!lang) return;

if (Array.isArray(lang)) lang = lang[0];
Expand Down
Loading

0 comments on commit c313b5f

Please sign in to comment.