Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
First commit
  • Loading branch information
tommy351 committed Dec 29, 2014
0 parents commit 27d32b8
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
.DS_Store
node_modules/
tmp/
*.log
.idea/
coverage/
13 changes: 13 additions & 0 deletions .jshintrc
@@ -0,0 +1,13 @@
{
"eqnull": true,
"expr": true,
"indent": 2,
"node": true,
"trailing": true,
"quotmark": "single",
"undef": true,
"unused": "vars",
"globals": {
"Promise": true
}
}
9 changes: 9 additions & 0 deletions .npmignore
@@ -0,0 +1,9 @@
test/
tmp/
coverage/
*.log
.jshintrc
.travis.yml
gulpfile.js
.idea/
appveyor.yml
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: node_js

node_js:
- "0.10"
- "0.11"

script:
- npm test

after_script:
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
Copyright (c) 2014 Tommy Chen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
@@ -0,0 +1,32 @@
# hexo-generator-archive

[![Build Status](https://travis-ci.org/hexojs/hexo-generator-archive.svg?branch=master)](https://travis-ci.org/hexojs/hexo-generator-archive) [![NPM version](https://badge.fury.io/js/hexo-generator-archive.svg)](http://badge.fury.io/js/hexo-generator-archive) [![Coverage Status](https://img.shields.io/coveralls/hexojs/hexo-generator-archive.svg)](https://coveralls.io/r/hexojs/hexo-generator-archive?branch=master)

Archive generator for [Hexo].

## Installation

``` bash
$ npm install hexo-generator-archive --save
```

## Options

``` yaml
archive_dir: archives
archive_generator:
per_page: 10
yearly: true
monthly: true
```

- **archive_dir**: Archive directory
- **per_page**: Posts displayed per page. (0 = disable pagination)
- **yearly**: Generate yearly archive.
- **monthly**: Generate monthly archive.

## License

MIT

[Hexo]: http://hexo.io/
37 changes: 37 additions & 0 deletions gulpfile.js
@@ -0,0 +1,37 @@
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var rirmaf = require('rimraf');

var lib = 'lib/**/*.js';

gulp.task('coverage', function(){
return gulp.src(lib)
.pipe($.istanbul())
.pipe($.istanbul.hookRequire());
});

gulp.task('coverage:clean', function(callback){
rirmaf('coverage', callback);
});

gulp.task('mocha', ['coverage'], function(){
return gulp.src('test/index.js')
.pipe($.mocha({
reporter: 'spec'
}))
.pipe($.istanbul.writeReports());
});

gulp.task('jshint', function(){
return gulp.src(lib)
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.jshint.reporter('fail'));
});

gulp.task('watch', function(){
gulp.watch(lib, ['mocha', 'jshint']);
gulp.watch(['test/index.js'], ['mocha']);
});

gulp.task('test', ['mocha', 'jshint']);
9 changes: 9 additions & 0 deletions index.js
@@ -0,0 +1,9 @@
var merge = require('utils-merge');

hexo.config.archive_generator = merge({
per_page: 10,
yearly: true,
monthly: true
}, hexo.config.archive_generator);

hexo.extend.generator.register('archive', require('./lib/generator'));
80 changes: 80 additions & 0 deletions lib/generator.js
@@ -0,0 +1,80 @@
var pagination = require('hexo-pagination');

function archiveGenerator(locals){
var config = this.config;
var archiveDir = config.archive_dir;
var allPosts = locals.posts.sort('-date');
var perPage = config.archive_generator.per_page || config.per_page;
var result = [];

if (!allPosts.length) return;

if (archiveDir[archiveDir.length - 1] !== '/') archiveDir += '/';

function generate(path, posts, options){
options = options || {};
options.archive = true;

result = result.concat(pagination(path, posts, {
perPage: perPage,
layout: ['archive', 'index'],
data: options
}));
}

generate(archiveDir, allPosts);

if (!config.archive_generator.yearly) return result;

var posts = {};

// Organize posts by date
allPosts.forEach(function(post){
var date = post.date;
var year = date.year();
var month = date.month() + 1; // month is started from 0

if (!posts.hasOwnProperty(year)){
// 13 arrays. The first array is for posts in this year
// and the other arrays is for posts in this month
posts[year] = [
[], [], [], [], [], [], [], [], [], [], [], [], []
];
}

posts[year][0].push(post);
posts[year][month].push(post);
});

// TODO: Add "createQuery" API in warehouse
var Query = this.model('Post').Query;
var years = Object.keys(posts);
var year, data, month, monthData, url;

// Yearly
for (var i = 0, len = years.length; i < len; i++){
year = +years[i];
data = posts[year];
url = archiveDir + year + '/';
if (!data[0].length) continue;

generate(url, new Query(data[0]), {year: year});

if (!config.archive_generator.monthly) continue;

// Monthly
for (month = 1; month <= 12; month++){
monthData = data[month];
if (!monthData.length) continue;

generate(url + (month < 10 ? '0' + month : month) + '/', new Query(monthData), {
year: year,
month: month
});
}
}

return result;
}

module.exports = archiveGenerator;
46 changes: 46 additions & 0 deletions package.json
@@ -0,0 +1,46 @@
{
"name": "hexo-generator-archive",
"version": "0.0.1",
"description": "Archive generator for Hexo.",
"main": "index",
"scripts": {
"test": "gulp test"
},
"directories": {
"lib": "./lib"
},
"engines": {
"node": ">= 0.10.0"
},
"repository": {
"type": "git",
"url": "git://github.com/hexojs/hexo-generator-archive.git"
},
"bugs": {
"url": "https://github.com/hexojs/hexo-generator-archive/issues"
},
"homepage": "http://hexo.io/",
"keywords": [
"hexo",
"generator",
"archive"
],
"author": "Tommy Chen <tommy351@gmail.com> (http://zespia.tw)",
"license": "MIT",
"devDependencies": {
"chai": "^1.9.1",
"coveralls": "^2.11.2",
"gulp": "^3.8.9",
"gulp-istanbul": "^0.5.0",
"gulp-jshint": "^1.8.6",
"gulp-load-plugins": "^0.8.0",
"gulp-mocha": "^2.0.0",
"jshint-stylish": "^1.0.0",
"mocha": "^2.0.1",
"rimraf": "^2.2.8"
},
"dependencies": {
"hexo-pagination": "0.0.2",
"utils-merge": "^1.0.0"
}
}
Empty file added test/index.js
Empty file.

0 comments on commit 27d32b8

Please sign in to comment.