diff --git a/index.js b/index.js index 7f9b4dae..e9366f9b 100755 --- a/index.js +++ b/index.js @@ -3,10 +3,12 @@ var Generator = require('./lib/generator'); -Generator.init(); -Generator.on('articlesBuild', function (post) { - Generator.buildOtherPages(); - Generator.copyImages(); -}); - -Generator.buildArticles(); +Generator + .buildArticles + .then( + function (post) { + Generator.buildOtherPages(); + Generator.copyImages(); + } + ) +; diff --git a/lib/generator.js b/lib/generator.js index 3cec946e..2a56aee8 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -1,6 +1,5 @@ 'use strict'; -var EventEmitter = require( "events" ).EventEmitter; var Mustache = require('mustache'); var fs = require('fs-extra'); var shell = require('shelljs'); @@ -8,62 +7,56 @@ var glob = require("glob"); var PostReader = require('./post_reader'); var config = require('./config'); var gm = require('gm').subClass({imageMagick: true}); +var dateFormat = require('dateformat'); +var RssJs = require('./rssjs'); +var Promise = require('promise/lib/es6-extensions'); /** * Generator used for creating the blog * @constructor */ -var Generator = new EventEmitter(); - -var templates; +var Generator = {}; + +var templates = { + post: fs.readFileSync(config.directories.theme+'/post.html', 'utf8'), + index: fs.readFileSync(config.directories.theme+'/index.html', 'utf8'), + rss: fs.readFileSync(config.directories.theme+'/rss.xml', 'utf8'), + atom: fs.readFileSync(config.directories.theme+'/atom.xml', 'utf8'), + sitemap: fs.readFileSync(config.directories.theme+'/sitemap.xml', 'utf8') +}; +Object.keys(templates).map(function (template) { + Mustache.parse(template); +}); var index = require('./index')(); -/** - * [init description] - * @return {[type]} [description] - */ -Generator.init = function ( ) { - templates = { - post: fs.readFileSync(config.directories.theme+'/post.html', 'utf8'), - index: fs.readFileSync(config.directories.theme+'/index.html', 'utf8'), - rss: fs.readFileSync(config.directories.theme+'/rss.xml', 'utf8'), - sitemap: fs.readFileSync(config.directories.theme+'/sitemap.xml', 'utf8') - }; - - Object.keys(templates).map(function (template) { - Mustache.parse(template); - }); -} - /** * [buildArticles description] * @return {[type]} [description] */ -Generator.buildArticles = function () { - glob(config.directories.data + "/**/*.md", function (er, files) { - var i, finished = 0; - - PostReader.on('parsed', function (post) { - shell.mkdir('-p', config.directories.htdocs + '/' + post.meta.Url); - fs.writeFile(config.directories.htdocs + '/' + post.meta.Url + 'index.html', Mustache.render(templates.post, { - meta: post.meta, - content: post.html, - config: config - })); - // console.log("Wrote " + post.meta.Url + "index.html"); - index.push(post); - finished ++; - if (finished === files.length) { - console.log("Wrote "+files.length+" articles"); - Generator.emit('articlesBuild', finished); +Generator.buildArticles = new Promise ( + function(resolve, reject) { + glob(config.directories.data + "/**/*.md", function (er, files) { + var i, finished = 0; + PostReader.on('parsed', function (post) { + shell.mkdir('-p', config.directories.htdocs + '/' + post.meta.Url); + fs.writeFile(config.directories.htdocs + '/' + post.meta.Url + 'index.html', Mustache.render(templates.post, { + meta: post.meta, + content: post.html, + config: config + })); + index.push(post); + finished ++; + if (finished === files.length) { + console.log("Wrote " + finished + " articles"); + resolve( finished ); + } + }); + for (i = 0; i < files.length; i++) { + PostReader.parse(files[i]); } }); - - for (i = 0; i < files.length; i++) { - PostReader.parse(files[i]); - } - }); -}; + } +); /** * [buildOtherPages description] @@ -71,51 +64,53 @@ Generator.buildArticles = function () { */ Generator.buildOtherPages = function ( ) { var pagedPosts = index.getPagedPosts(5), page, indexFilename; + var fileReady = function(filename) { + console.log("Wrote " + filename); + }; - fs.writeFile(config.directories.htdocs + '/sitemap.json', JSON.stringify(index.getPosts()), function() { - console.log("Wrote sitemap.json"); - }); + fs.writeFile(config.directories.htdocs + '/sitemap.json', JSON.stringify(index.getPosts()), fileReady("sitemap.json")); fs.remove(config.directories.htdocs + '/index*', function (err) { for (page = 0; page < pagedPosts.length; page ++) { var curPageObj = index.getPageData(page, pagedPosts.length); curPageObj.index = pagedPosts[page]; curPageObj.config = config; - fs.writeFile(config.directories.htdocs + '/' + curPageObj.currentUrl, Mustache.render(templates.index, curPageObj), function() { - console.log("Wrote index file"); - }); + fs.writeFile(config.directories.htdocs + '/' + curPageObj.currentUrl, Mustache.render(templates.index, curPageObj), fileReady('index file')); } }); fs.writeFile(config.directories.htdocs + '/posts.rss', Mustache.render(templates.rss, { index: index.getPosts(10), + pubDate: dateFormat(index.pubDate, 'ddd, dd mmm yyyy hh:MM:ss o'), config: config - }), function () { - console.log("Wrote posts.rss"); - }); + }), fileReady('posts.rss')); + + fs.writeFile(config.directories.htdocs + '/rss.json', JSON.stringify(RssJs(index.getPosts(), dateFormat(index.pubDate, 'ddd, dd mmm yyyy hh:MM:ss o'))), fileReady('rss.json')); + + fs.writeFile(config.directories.htdocs + '/posts.atom', Mustache.render(templates.atom, { + index: index.getPosts(10), + pubDate: dateFormat(index.pubDate, 'isoDateTime'), + config: config + }), fileReady('posts.atom')); fs.writeFile(config.directories.htdocs + '/sitemap.xml', Mustache.render(templates.sitemap, { index: index.getPosts(10), config: config - }),function () { - console.log("Wrote sitemap.xml"); - }); + }), fileReady('sitemap.xml')); var tags = index.getTags(); fs.remove(config.directories.htdocs + '/tagged', function (err) { Object.keys(tags).map(function (key) { shell.mkdir('-p', config.directories.htdocs + '/tagged/' + tags[key].id); tags[key].config = config; - fs.writeFile(config.directories.htdocs + '/tagged/' + tags[key].id + '/index.html', Mustache.render(templates.index, tags[key]), function() { - console.log("Wrote tagged/" + tags[key].id + '/index.html'); - }); + fs.writeFile(config.directories.htdocs + '/tagged/' + tags[key].id + '/index.html', Mustache.render(templates.index, tags[key]), fileReady("tagged/" + tags[key].id + '/index.html')); }); }); }; Generator.copyImages = function () { glob(config.directories.data + "/**/*.{png,jpg,gif}", function (er, files) { - var i, copiedFiles = 0; + var i, j, copiedFiles = 0; var copyEnd = function () { copiedFiles ++; if (copiedFiles === files.length) { @@ -125,8 +120,8 @@ Generator.copyImages = function () { for (i = 0; i < files.length; i++) { var targetFile = files[i].replace(/^user\//, config.directories.htdocs + '/'); fs.copy(files[i], targetFile, copyEnd); - config.imageSizes.forEach(function(imageSize) { - // console.log(targetFile.replace(/(\.[a-z]+$)/,'-'+imageSize[0]+'x'+imageSize[1]+'$1')); + for (j = 0; j < config.imageSizes.length; j++) { + var imageSize = config.imageSizes[j]; gm(files[i]) .resize(imageSize[0], imageSize[1]) .noProfile() @@ -134,7 +129,7 @@ Generator.copyImages = function () { }) ; - }) + } } }); }; diff --git a/lib/index.js b/lib/index.js index 9e50dd92..47a86c44 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,9 +5,10 @@ * @constructor */ var Index = function () { - var index = []; - var tags = {}; + var index = []; + var tags = {}; var isSorted = true; + var pubDate = new Date(); var sortIndex = function() { index.sort(function(a,b){ @@ -37,6 +38,7 @@ var Index = function () { isSorted = false; index.push(post); }, + /** * Get all posts, sorted by date. * @param {integer} i Only return i results. If left empty, all results will be returned. @@ -48,6 +50,7 @@ var Index = function () { } return i ? index.slice(0,i) : index; }, + /** * [getTags description] * @return {[type]} [description] @@ -69,6 +72,7 @@ var Index = function () { }); return tags; }, + /** * Get whole index, split up into separate pages. * @param {[type]} itemsPerPage [description] @@ -94,6 +98,7 @@ var Index = function () { } while (page * itemsPerPage < index.length); return pages; }, + /** * Get meta data for a single page. * @param {[type]} curPage [description] diff --git a/lib/rssjs.js b/lib/rssjs.js new file mode 100644 index 00000000..b1e7b636 --- /dev/null +++ b/lib/rssjs.js @@ -0,0 +1,41 @@ +'use strict'; + +/** + * Represents all posts + * @constructor + */ +var RssJs = function (index, pubDate) { + var config = require('./config'); + var items = []; + + index.forEach(function(item){ + var tags = []; + if (item.meta.Tags !== undefined) { + item.meta.Tags.forEach(function(t){ + tags.push(t.title); + }); + } + items.push({ + title: item.meta.Title, + description: item.htmlTeaser, + link: config.baseUrl + config.basePath + item.meta.Url, + pubDate: item.meta.rfcDate, + guid: config.baseUrl + config.basePath + item.meta.Url, + categories: tags + }); + }); + + return { + version: 2.0, + channel: { + title: config.name, + link: config.baseUrl + config.basePath, + description: config.description, + language: config.language, + lastBuildDate: pubDate, + items: items + } + }; +}; + +module.exports = RssJs; diff --git a/package.json b/package.json index a7a815fd..ec88f441 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "gulp-plumber": "^1.1.0", "gulp-shell": "^0.5.2", "jshint": "^2.9.2", - "nodeunit": "^0.9.1" + "nodeunit": "^0.9.1", + "promise": "^7.1.1" }, "directories": { "doc": "docs", diff --git a/theme/atom.xml b/theme/atom.xml new file mode 100644 index 00000000..94200570 --- /dev/null +++ b/theme/atom.xml @@ -0,0 +1,25 @@ + + + {{config.name}} + + {{{config.baseUrl}}}{{{config.basePath}}} + + {{pubDate}} + + + + {{config.name}} + + {{#index}} + + {{meta.Title}} + + + {{meta.isoDate}} + {{{config.baseUrl}}}{{{config.basePath}}}{{{meta.Url}}} + {{#meta.Tags}} + + {{/meta.Tags}} + + {{/index}} + diff --git a/theme/index.html b/theme/index.html index d15822c0..9ae65221 100644 --- a/theme/index.html +++ b/theme/index.html @@ -15,8 +15,10 @@ + + - +

{{config.name}}

@@ -28,24 +30,26 @@

{{config.name}}

{{title}}

{{/title}} - {{#index}} -
- - - -

{{meta.Title}}

- {{{htmlTeaser}}} -

Weiter lesen

- {{#meta.Tags.length}} -

- Andere Artikel zum Thema - {{#meta.Tags}} - · - {{/meta.Tags}} -

- {{/meta.Tags.length}} -
- {{/index}} +
+ {{#index}} +
+ + + +

{{meta.Title}}

+ {{{htmlTeaser}}} +

Weiter lesen

+ {{#meta.Tags.length}} +

+ Andere Artikel zum Thema + {{#meta.Tags}} + · + {{/meta.Tags}} +

+ {{/meta.Tags.length}} +
+ {{/index}} +
{{#currentPage}}
diff --git a/theme/post.html b/theme/post.html index 62964a62..c73f2e7c 100644 --- a/theme/post.html +++ b/theme/post.html @@ -18,8 +18,10 @@ + + - +

{{config.name}}

diff --git a/theme/rss.xml b/theme/rss.xml index 109671ea..df87f692 100644 --- a/theme/rss.xml +++ b/theme/rss.xml @@ -5,6 +5,7 @@ {{{config.baseUrl}}}{{{config.basePath}}} {{config.description}} {{config.language}} + {{pubDate}} {{#index}} diff --git a/user/_config.json b/user/_config.json index 9104beb3..ce9f9db9 100644 --- a/user/_config.json +++ b/user/_config.json @@ -4,6 +4,7 @@ "basePath": "/", "description": "Most-simple blog", "language": "en", + "siteEmail": "", "deploy":"", "imageSizes": [ [200,200],