Skip to content

Commit

Permalink
Implement include/exclude files
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Dec 6, 2015
1 parent 4eaec8a commit 8cca1f9
Show file tree
Hide file tree
Showing 13 changed files with 627 additions and 577 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ $ hexo generate

## More Information

- Read the [documentation](http://hexo.io/)
- Find solutions in [troubleshooting](http://hexo.io/docs/troubleshooting.html)
- Read the [documentation](https://hexo.io/)
- Find solutions in [troubleshooting](https://hexo.io/docs/troubleshooting.html)
- Join discussion on [Google Group](https://groups.google.com/group/hexo)
- See the [plugin list](https://github.com/hexojs/hexo/wiki/Plugins) and the [theme list](https://github.com/hexojs/hexo/wiki/Themes) on wiki
- See the [plugin list](https://hexo.io/plugins/) and the [theme list](https://hexo.io/themes/) on wiki
- Follow [@hexojs](https://twitter.com/hexojs) for latest news

## License
Expand Down
26 changes: 1 addition & 25 deletions lib/plugins/generator/page.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
'use strict';

var minimatch = require('minimatch');
var _ = require('lodash');

function pageGenerator(locals) {
var skipRender = this.config.skip_render || [];
if (!Array.isArray(skipRender)) skipRender = [skipRender];
skipRender = _.compact(skipRender);

var skipRenderLen = skipRender.length;

function isSkipRender(path) {
if (!skipRenderLen) return false;

for (var i = 0; i < skipRenderLen; i++) {
if (minimatch(path, skipRender[i])) return true;
}

return false;
}

return locals.pages.map(function(page) {
var layout = page.layout;
var path = page.path;

if (isSkipRender(page.source)) {
return {
path: page.source,
data: page.raw
};
} else if (!layout || layout === 'false') {
if (!layout || layout === 'false' || layout === 'off') {
return {
path: path,
data: page.content
Expand Down
183 changes: 99 additions & 84 deletions lib/plugins/processor/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,128 @@ var common = require('./common');
var Promise = require('bluebird');
var yfm = require('hexo-front-matter');
var pathFn = require('path');
var util = require('hexo-util');
var Pattern = util.Pattern;

module.exports = function(ctx) {
function processPage(file) {
var Page = ctx.model('Page');
var path = file.path;
var doc = Page.findOne({source: path});
var config = ctx.config;
var timezone = config.timezone;

if (file.type === 'delete') {
if (doc) {
return doc.remove();
}

exports.process = function(file) {
if (this.render.isRenderable(file.path)) {
return processPage.call(this, file);
}

return processAsset.call(this, file);
};

exports.pattern = common.ignoreTmpAndHiddenFile;

function processPage(file) {
var Page = this.model('Page');
var path = file.path;
var doc = Page.findOne({source: path});
var self = this;
var config = this.config;
var timezone = config.timezone;

if (file.type === 'delete') {
if (doc) {
return doc.remove();
return;
}

return;
}
return file.changed().then(function(changed) {
if (!changed && doc) return;

return file.changed().then(function(changed) {
if (!changed && doc) return;
return Promise.all([
file.stat(),
file.read()
]).spread(function(stats, content) {
var data = yfm(content);
var output = ctx.render.getOutput(path);

return Promise.all([
file.stat(),
file.read()
]).spread(function(stats, content) {
var data = yfm(content);
var output = self.render.getOutput(path);
data.source = path;
data.raw = content;

data.source = path;
data.raw = content;
data.date = common.toDate(data.date);

data.date = common.toDate(data.date);
if (data.date) {
if (timezone) data.date = common.timezone(data.date, timezone);
} else {
data.date = stats.ctime;
}

if (data.date) {
if (timezone) data.date = common.timezone(data.date, timezone);
} else {
data.date = stats.ctime;
}
data.updated = common.toDate(data.updated);

data.updated = common.toDate(data.updated);
if (data.updated) {
if (timezone) data.updated = common.timezone(data.updated, timezone);
} else {
data.updated = stats.mtime;
}

if (data.updated) {
if (timezone) data.updated = common.timezone(data.updated, timezone);
} else {
data.updated = stats.mtime;
}
if (data.permalink) {
data.path = data.permalink;
delete data.permalink;

if (data.permalink) {
data.path = data.permalink;
delete data.permalink;
if (data.path[data.path.length - 1] === '/') {
data.path += 'index';
}

if (data.path[data.path.length - 1] === '/') {
data.path += 'index';
if (!pathFn.extname(data.path)) {
data.path += '.' + output;
}
} else {
var extname = pathFn.extname(path);
data.path = path.substring(0, path.length - extname.length) + '.' + output;
}

if (!pathFn.extname(data.path)) {
data.path += '.' + output;
if (!data.layout && output !== 'html' && output !== 'htm') {
data.layout = false;
}
} else {
var extname = pathFn.extname(path);
data.path = path.substring(0, path.length - extname.length) + '.' + output;
}

if (!data.layout && output !== 'html' && output !== 'htm') {
data.layout = false;
}
// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
var doc = Page.findOne({source: path});

// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
var doc = Page.findOne({source: path});
if (doc) {
return doc.replace(data);
}

return Page.insert(data);
});
});
}

function processAsset(file) {
var id = file.source.substring(ctx.base_dir.length).replace(/\\/g, '/');
var Asset = ctx.model('Asset');
var doc = Asset.findById(id);

if (file.type === 'delete') {
if (doc) {
return doc.replace(data);
return doc.remove();
}

return Page.insert(data);
return;
}

return file.changed().then(function(changed) {
return Asset.save({
_id: id,
path: file.path,
modified: changed
});
});
});
}
}

function processAsset(file) {
var id = file.source.substring(this.base_dir.length).replace(/\\/g, '/');
var Asset = this.model('Asset');
var doc = Asset.findById(id);
return {
pattern: new Pattern(function(path) {
if (common.isTmpFile(path) || common.isMatch(path, ctx.config.exclude)) return;

if (file.type === 'delete') {
if (doc) {
return doc.remove();
}
if (common.isHiddenFile(path) && !common.isMatch(path, ctx.config.include)) {
return;
}

return;
}
return {
renderable: ctx.render.isRenderable(path) && !common.isMatch(path, ctx.config.skip_render)
};
}),

return file.changed().then(function(changed) {
return Asset.save({
_id: id,
path: file.path,
modified: changed
});
});
}
process: function assetProcessor(file) {
if (file.params.renderable) {
return processPage(file);
}

return processAsset(file);
}
};
};
19 changes: 17 additions & 2 deletions lib/plugins/processor/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var Pattern = require('hexo-util').Pattern;
var moment = require('moment-timezone');
var minimatch = require('minimatch');
var _ = require('lodash');

var DURATION_MINUTE = 1000 * 60;

Expand All @@ -11,8 +13,7 @@ function isTmpFile(path) {
}

function isHiddenFile(path) {
if (path[0] === '_') return true;
return /\/_/.test(path);
return /(^|\/)[_\.]/.test(path);
}

exports.ignoreTmpAndHiddenFile = new Pattern(function(path) {
Expand Down Expand Up @@ -45,3 +46,17 @@ exports.timezone = function(date, timezone) {

return new Date(ms - diff);
};

exports.isMatch = function(path, patterns) {
if (!patterns) return false;
if (!Array.isArray(patterns)) patterns = [patterns];

patterns = _.compact(patterns);
if (!patterns.length) return false;

for (var i = 0, len = patterns.length; i < len; i++) {
if (minimatch(path, patterns[i])) return true;
}

return false;
};
59 changes: 31 additions & 28 deletions lib/plugins/processor/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ var util = require('hexo-util');
var pathFn = require('path');
var Pattern = util.Pattern;

exports.process = function(file) {
var Data = this.model('Data');
var path = file.params.path;
var extname = pathFn.extname(path);
var id = path.substring(0, path.length - extname.length);
var doc = Data.findById(id);

if (file.type === 'delete') {
if (doc) {
return doc.remove();
module.exports = function(ctx) {
return {
pattern: new Pattern('_data/*path'),
process: function dataProcessor(file) {
var Data = ctx.model('Data');
var path = file.params.path;
var extname = pathFn.extname(path);
var id = path.substring(0, path.length - extname.length);
var doc = Data.findById(id);

if (file.type === 'delete') {
if (doc) {
return doc.remove();
}

return;
}

return file.changed().then(function(changed) {
if (!changed && doc) return;

return file.render();
}).then(function(result) {
if (result == null) return;

return Data.save({
_id: id,
data: result
});
});
}

return;
}

return file.changed().then(function(changed) {
if (!changed && doc) return;

return file.render();
}).then(function(result) {
if (result == null) return;

return Data.save({
_id: id,
data: result
});
});
};
};

exports.pattern = new Pattern('_data/*path');
2 changes: 1 addition & 1 deletion lib/plugins/processor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function(ctx) {
var processor = ctx.extend.processor;

function register(name) {
var obj = require('./' + name);
var obj = require('./' + name)(ctx);
processor.register(obj.pattern, obj.process);
}

Expand Down
Loading

1 comment on commit 8cca1f9

@Xuanwo
Copy link
Contributor

@Xuanwo Xuanwo commented on 8cca1f9 Dec 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref: #1333

Hope this commit can fix the issue, when the js file is over 1MB, there will be a problem.

Please sign in to comment.