Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Inital test suite

Added travis-ci integration
Added inital test suite using mocha (run tests with make test)
Added cache-control as config
Added wordpress export script
  • Loading branch information...
commit ebda9046bd83cc8473663df20b83c0e61269c6bd 1 parent 5ff6b1b
Guy Halford-Thompson authored
View
4 .travis.yaml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.6
View
9 glog_config.sample.json
@@ -1,7 +1,8 @@
{
"blog_repository" : "git@github.com:guyht/Guido.git",
- "author" : "Glog",
- "blog_title" : "The Glob Blog",
- "port" : 8080,
- "base_url" : ""
+ "author" : "Glog",
+ "blog_title" : "The Glob Blog",
+ "port" : 8080,
+ "base_url" : "",
+ "cache_time" : 28800
}
View
58 lib/glog.js
@@ -7,18 +7,19 @@ var path = require('path'),
jade = require('jade'),
exec = require('child_process').exec;
-/*
- * Scope variable to cache pages
- */
-var pages = {};
-
/*
* Setup module
*/
module.exports = new Glog();
-function Glog() {};
+function Glog() {
+
+ /*
+ * Object variable to track pages
+ */
+ this.pages = {};
+};
/*
* Function to handle loading of configs
@@ -29,7 +30,8 @@ Glog.prototype.load_configs = function(cb) {
var options = {
blog_title : 'Glog Blog'
},
- fn = this;
+ fn = this,
+ key;
fs.readFile(path.join('glog_config.json'), 'UTF-8', function(err, data) {
if(err) {
@@ -154,7 +156,7 @@ Glog.prototype.load_articles = function(options, cb) {
/*
* Render the entire blog and store pages in cache
*/
-Glog.prototype.render_blog = function(options, articles) {
+Glog.prototype.render_blog = function(options, articles, cb) {
var fn = this;
@@ -164,9 +166,9 @@ Glog.prototype.render_blog = function(options, articles) {
}
console.log('Compiling template for home page');
- var fn = jade.compile(data);
+ var ja = jade.compile(data);
- pages['/'] = fn({
+ fn.pages['/'] = ja({
'title' : options.blog_title,
'analytics_code' : options.analytics_code || null,
'base_url' : options.base_url || '',
@@ -178,15 +180,17 @@ Glog.prototype.render_blog = function(options, articles) {
// Loop through articles and compile individual pages
for(var i=0; i< articles.length; i++) {
- pages[articles[i].url] = fn({
+ fn.pages[articles[i].url] = ja({
'title' : options.blog_title,
'articles' : [articles[i]],
'disqus_id' : options.disqus_id || -1,
'analytics_code' : options.analytics_code || null,
- 'base_url' : options.base_url || '',
+ 'base_url' : options.base_url || '',
'single' : true
});
}
+
+ cb();
});
};
@@ -194,35 +198,38 @@ Glog.prototype.render_blog = function(options, articles) {
/*
* Handle a request for the home page
*/
-Glog.prototype.req_home = function(req, res, next) {
+Glog.prototype.req_home = function(req, res, next, options) {
console.log('Home page requested');
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
+ res.setHeader('Cache-Control', options.cache_time || 'no-cache');
res.setHeader('Server', 'Glog (NodeJS)');
- res.end(pages['/']);
+ res.end(this.pages['/']);
};
/*
* Handle a request for an individual article
*/
-Glog.prototype.req_article = function(req, res, next) {
+Glog.prototype.req_article = function(req, res, next, options) {
var fn = this,
url = [req.params.year, req.params.month, req.params.article].join('/');
console.log('Requesting ' + url);
- if(typeof pages[url] !== 'undefined') {
+ if(typeof this.pages[url] === 'undefined') {
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
+ res.setHeader('Cache-Control', options.cache_time || 'no-cache');
res.setHeader('Server', 'Glog (NodeJS)');
- res.end(pages[url]);
+ res.end(this.pages['/']);
} else {
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
+ res.setHeader('Cache-Control', options.cache_time || 'no-cache');
res.setHeader('Server', 'Glog (NodeJS)');
- res.end(pages['/']);
+ res.end(this.pages[url]);
}
};
@@ -243,14 +250,12 @@ Glog.prototype.rebuild = function(cb) {
next;
fn.load_configs(function(options) {
- fn.update_repo(options, function() {
+ fn.update_repo(options, function(err) {
fn.load_articles(options, function(articles) {
articles = articles.sort(function(a,b) {
return b.date - a.date;
});
- fn.render_blog(options, articles);
-
- cb(options);
+ fn.render_blog(options, articles, cb);
});
});
});
@@ -269,11 +274,12 @@ Glog.prototype.get_repo = function(repo, cb) {
exec('git clone ' + repo + ' blog_repo', function(error, stdout, stderr) {
if(error) {
fn.handle_error('Could not clone git repository: ' + error);
+ cb(false);
}
console.log('Stdout: ' + stdout);
console.log('Stderr: ' + stderr);
- cb();
+ cb(true);
});
};
@@ -299,17 +305,21 @@ Glog.prototype.update_repo = function(options, cb) {
exec('cd blog_repo; git pull origin master', function(error, stdout, stderr) {
if(err) {
fn.handle_error('Could not update blog repo: ' + error);
+ cb(false);
}
console.log('Stdout: ' + stdout);
console.log('Stderr: ' + stderr);
- cb();
+ cb(true);
});
}
});
};
+/*
+ * Format a date
+ */
Glog.formatDate = function(date) {
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
months = ['January' , 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
View
4 makefile
@@ -0,0 +1,4 @@
+test:
+ cd test; ../node_modules/.bin/mocha *.js -t 10000 --reporter spec
+
+.PHONY: test
View
6 package.json
@@ -6,6 +6,10 @@
"dependencies" : {
"connect" : ">= 1.8.4",
"marked" : ">= 0.1.4",
- "jade" : ">= 0.19.0"
+ "jade" : ">= 0.19.0",
+ "mocha": "0.8.x"
+ },
+ "scripts": {
+ "test": "make test"
}
}
View
46 server.js
@@ -3,27 +3,35 @@ var glog = require('./lib/glog'),
path = require('path');
-glog.rebuild(function(options) {
+glog.rebuild(function() {
+ glog.load_configs(function(options) {
- console.log('Starting server on port ' + options.port);
+ console.log('Starting server on port ' + options.port);
- var server = connect.createServer(
- connect.static(path.join('blog_repo', '/public')),
- connect.staticCache(),
- connect.router(function(app) {
- app.get('/', glog.req_home);
- app.get('/__render', function(req, res, next) {
- glog.rebuild(function(options) {
- res.end();
+ var server = connect.createServer(
+ connect.static(path.join('blog_repo', '/public')),
+ connect.staticCache(),
+ connect.router(function(app) {
+ app.get('/__render', function(req, res, next) {
+ glog.rebuild(function() {
+ res.end();
+ });
});
- });
- app.post('/__render', function(req, res, next) {
- glog.rebuild(function(options) {
- res.end();
+ app.post('/__render', function(req, res, next) {
+ glog.rebuild(function() {
+ res.end();
+ });
});
- });
- app.get('/:year/:month/:article', glog.req_article);
- app.get('/*', glog.req_home);
- })
- ).listen(options.port);
+ app.get('/', function(req, res, next) {
+ glog.req_home(req, res, next, options)
+ });
+ app.get('/:year/:month/:article', function(req, res, next) {
+ glog.req_article(req, res, next, options)
+ });
+ app.get('/*', function(req, res, next) {
+ glog.req_home(req, res, next, options)
+ });
+ })
+ ).listen(options.port);
+ });
});
1  test/blog_repo
@@ -0,0 +1 @@
+Subproject commit df79138145478c932fecce6ab16fbee3358723c8
View
6 test/glog_config.json
@@ -0,0 +1,6 @@
+{
+ "blog_repository" : "git@github.com:guyht/Guido.git",
+ "author" : "Glog",
+ "blog_title" : "The Glog Blog",
+ "port" : 8080
+}
View
67 test/glog_tests.js
@@ -0,0 +1,67 @@
+var glog = require('../lib/glog'),
+ assert = require('assert'),
+ exec = require('child_process').exec;
+
+describe('Glog', function() {
+
+ before(function(done) {
+ exec('rm -fr ./blog_repo', function(err, stdout, stderr) {
+ done();
+ });
+ });
+
+ it('should load configs from a JSON file', function(done) {
+ glog.load_configs(function(options) {
+ assert.equal(options.blog_title, 'The Glog Blog');
+ assert.equal(options.port, 8080);
+ assert.equal(options.author, 'Glog');
+ done();
+ });
+ });
+
+
+ it('should be cloned from a remote repository', function(done) {
+ glog.load_configs(function(options) {
+ glog.get_repo(options.blog_repository, function(err) {
+ console .log('err ' + err);
+ assert.ok(err);
+ done();
+ });
+ });
+ });
+
+
+ it('should be pulled from a remote repository', function(done) {
+ glog.load_configs(function(options) {
+ glog.update_repo(options.blog_repository, function(err) {
+ assert.ok(err);
+ done();
+ });
+ });
+ });
+
+
+ it('should load articles from the cloned repository', function(done) {
+ glog.load_configs(function(options) {
+ glog.load_articles(options, function(articles) {
+ assert.equal(articles.length, 2);
+ done();
+ });
+ });
+ });
+
+ it('should render the articles', function(done) {
+ glog.load_configs(function(options) {
+ glog.load_articles(options, function(articles) {
+
+ glog.render_blog(options, articles, function() {
+ assert.ok(glog.pages['/']);
+ done();
+ });
+ });
+ });
+ });
+
+});
+
+
View
71 utils/wp-export.php
@@ -0,0 +1,71 @@
+<?php
+
+$opts = 's::d:u:p:P::a:';
+
+$options = getopt($opts);
+
+if(!isset($options['u']) || !isset($options['d']) || !isset($options['p']) || !isset($options['a'])) {
+ show_help();
+}
+
+$author = $options['a'];
+$username = $options['u'];
+$database = $options['d'];
+$password = $options['p'];
+$server = isset($options['s']) ? $options['s'] : 'localhost';
+$port = isset($options['P']) ? $options['P'] : '3306';
+
+var_dump($options);
+
+// Connect to db
+$conn = mysql_connect($server.':'.$port, $username, $password);
+if(!$conn) {
+ die('Could not connect to database ' . mysql_error());
+}
+
+// Select database
+$db = mysql_select_db($database);
+if(!$db) {
+ die('Could not select database ' . mysql_error());
+}
+
+$rs = mysql_query('select post_date, post_content, post_title, post_name from wp_posts where post_status = \'publish\'');
+$nrows = mysql_num_rows($rs);
+
+echo("Found ".$nrows." posts");
+
+mkdir("articles");
+
+for($i=0;$i<$nrows;$i++) {
+ $out = "{\n";
+ $out .= "\t\"title\" : \"".mysql_result($rs, $i, 'post_title')."\",\n";
+ $out .= "\t\"date\" : \"".mysql_result($rs, $i, 'post_date')."\",\n";
+ $out .= "\t\"url\" : \"".mysql_result($rs, $i, 'post_name')."\",\n";
+ $out .= "\t\"author\" : \"".$author."\"\n";
+ $out .= "}\n";
+ $out .= "\n";
+ $out .= mysql_result($rs, $i, 'post_content');
+ $out .= "\n";
+
+ $file = "articles/".mysql_result($rs, $i, 'post_name').".txt";
+ echo("Writing post to file ".$file."\n");
+ $fh = fopen($file, 'w');
+ fwrite($fh, $out);
+ fclose($fh);
+}
+
+
+
+function show_help() {
+ echo("Usage:\n");
+ echo("\n");
+ echo("php wp-export.php -a AUTHOR -s SERVER -d DATABSE -u USER -p PASSWORD -P PORT\n");
+ echo("\n");
+ echo("Options:\n");
+ echo(" -s (Optional) Database URL\n");
+ echo(" -P (Optional) Database PORT\n");
+ echo(" -d Database name\n");
+ echo(" -u Database username\n");
+ echo(" -p Database password\n");
+ echo(" -a Author of the blog\n");
+}
Please sign in to comment.
Something went wrong with that request. Please try again.