Skip to content

Commit

Permalink
* Adding RSS-JS
Browse files Browse the repository at this point in the history
* Adding Atom
* Switching from EventEmitter to Promise
  • Loading branch information
Frank Boës committed Aug 5, 2016
1 parent 64a8263 commit 91cba8d
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 93 deletions.
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
)
;
121 changes: 58 additions & 63 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,116 @@
'use strict';

var EventEmitter = require( "events" ).EventEmitter;
var Mustache = require('mustache');
var fs = require('fs-extra');
var shell = require('shelljs');
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]
* @return {[type]} [description]
*/
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) {
Expand All @@ -125,16 +120,16 @@ 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()
.write(targetFile.replace(/(\.[a-z]+$)/,'-'+imageSize[0]+'x'+imageSize[1]+'$1'),function() {

})
;
})
}
}
});
};
Expand Down
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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.
Expand All @@ -48,6 +50,7 @@ var Index = function () {
}
return i ? index.slice(0,i) : index;
},

/**
* [getTags description]
* @return {[type]} [description]
Expand All @@ -69,6 +72,7 @@ var Index = function () {
});
return tags;
},

/**
* Get whole index, split up into separate pages.
* @param {[type]} itemsPerPage [description]
Expand All @@ -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]
Expand Down
41 changes: 41 additions & 0 deletions lib/rssjs.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions theme/atom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{config.name}}</title>
<link href="{{{config.baseUrl}}}{{{config.basePath}}}" />
<id>{{{config.baseUrl}}}{{{config.basePath}}}</id>
<!--description>{{config.description}}</description-->
<updated>{{pubDate}}</updated>
<!--language>{{config.language}}</language-->
<link href="{{{config.baseUrl}}}{{{config.basePath}}}posts.atom" rel="self" />
<author>
<name>{{config.name}}</name>
</author>
{{#index}}
<entry>
<title>{{meta.Title}}</title>
<summary type="html"><![CDATA[{{htmlTeaser}}]]></summary>
<link href="{{{config.baseUrl}}}{{{config.basePath}}}{{{meta.Url}}}" />
<updated>{{meta.isoDate}}</updated>
<id>{{{config.baseUrl}}}{{{config.basePath}}}{{{meta.Url}}}</id>
{{#meta.Tags}}
<category term="{{title}}" />
{{/meta.Tags}}
</entry>
{{/index}}
</feed>
42 changes: 23 additions & 19 deletions theme/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
<link title="fbo&euml;s" href="http://3960.org/" rel="me"/>
<link href="http://cdn.3960.org/css/styles.css" type="text/css" rel="stylesheet" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="{{config.baseUrl}}{{config.basePath}}posts.rss"/>
<link rel="alternate" type="application/atom+xml" title="ATOM" href="{{config.baseUrl}}{{config.basePath}}posts.atom"/>
<link rel="alternate" type="application/json" title="JSON" href="{{config.baseUrl}}{{config.basePath}}rss.json"/>
</head>
<body class="">
<body class="index">
<div id="container">
<header >
<h1 id="header"><a href="{{config.basePath}}">{{config.name}}</a></h1>
Expand All @@ -28,24 +30,26 @@ <h1 id="header"><a href="{{config.basePath}}">{{config.name}}</a></h1>
<h2>{{title}}</h2>
{{/title}}

{{#index}}
<article lang="{{meta.Language}}">
<span class="date">
<time datetime="{{meta.isoDate}}">{{meta.localeDate}}</time>
</span>
<h4><a href="{{meta.Url}}">{{meta.Title}}</a></h4>
{{{htmlTeaser}}}
<p><a href="{{meta.Url}}">Weiter lesen</a></p>
{{#meta.Tags.length}}
<p class="meta">
Andere Artikel zum Thema
{{#meta.Tags}}
· <a rel="tag" href="{{config.basePath}}tagged/{{id}}/">„{{title}}”</a>
{{/meta.Tags}}
</p>
{{/meta.Tags.length}}
</article>
{{/index}}
<div class="teasers">
{{#index}}
<article lang="{{meta.Language}}">
<span class="date">
<time datetime="{{meta.isoDate}}">{{meta.localeDate}}</time>
</span>
<h4><a href="{{meta.Url}}">{{meta.Title}}</a></h4>
{{{htmlTeaser}}}
<p><a href="{{meta.Url}}">Weiter lesen</a></p>
{{#meta.Tags.length}}
<p class="meta">
Andere Artikel zum Thema
{{#meta.Tags}}
· <a rel="tag" href="{{config.basePath}}tagged/{{id}}/">„{{title}}”</a>
{{/meta.Tags}}
</p>
{{/meta.Tags.length}}
</article>
{{/index}}
</div>

{{#currentPage}}
<div id="prevnext">
Expand Down
Loading

0 comments on commit 91cba8d

Please sign in to comment.