From 27d32b86c800e0adaf112f9cb0ca3410878a9691 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Mon, 29 Dec 2014 19:13:23 +0800 Subject: [PATCH] First commit --- .gitignore | 6 ++++ .jshintrc | 13 ++++++++ .npmignore | 9 ++++++ .travis.yml | 11 +++++++ LICENSE | 7 +++++ README.md | 32 +++++++++++++++++++ gulpfile.js | 37 ++++++++++++++++++++++ index.js | 9 ++++++ lib/generator.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 46 ++++++++++++++++++++++++++++ test/index.js | 0 11 files changed, 250 insertions(+) create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 .npmignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 gulpfile.js create mode 100644 index.js create mode 100644 lib/generator.js create mode 100644 package.json create mode 100644 test/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..241a5d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +node_modules/ +tmp/ +*.log +.idea/ +coverage/ \ No newline at end of file diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..1e998ff --- /dev/null +++ b/.jshintrc @@ -0,0 +1,13 @@ +{ + "eqnull": true, + "expr": true, + "indent": 2, + "node": true, + "trailing": true, + "quotmark": "single", + "undef": true, + "unused": "vars", + "globals": { + "Promise": true + } +} \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..7c092c2 --- /dev/null +++ b/.npmignore @@ -0,0 +1,9 @@ +test/ +tmp/ +coverage/ +*.log +.jshintrc +.travis.yml +gulpfile.js +.idea/ +appveyor.yml \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..af6b1c9 --- /dev/null +++ b/.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 \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5c3aae5 --- /dev/null +++ b/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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c1f5211 --- /dev/null +++ b/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/ \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..6f5f95f --- /dev/null +++ b/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']); diff --git a/index.js b/index.js new file mode 100644 index 0000000..d5b7261 --- /dev/null +++ b/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')); \ No newline at end of file diff --git a/lib/generator.js b/lib/generator.js new file mode 100644 index 0000000..3790bc6 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d6f1379 --- /dev/null +++ b/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 (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" + } +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..e69de29