Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Treat content and app paths separately

  • Loading branch information...
commit cec0dac3ccb448b6781af0a54dae6103640c913e 1 parent e3e71c6
@joehewitt authored
Showing with 59 additions and 33 deletions.
  1. +1 −1  README.md
  2. +2 −2 bin/nerve
  3. +14 −13 lib/Blog.js
  4. +42 −17 lib/Server.js
View
2  README.md
@@ -1,7 +1,7 @@
nerve
========
-A blogging platform.
+A Node.js blogging platform.
Installation
------------
View
4 bin/nerve
@@ -1,6 +1,6 @@
#!/usr/bin/env node
+var argv = require('optimist').argv;
var nerve = require('../lib/nerve');
-var blogPaths = process.argv.slice(2);
-nerve.run(blogPaths);
+nerve.run(argv._[0], argv.content, argv.config);
View
27 lib/Blog.js
@@ -19,18 +19,19 @@ var rePostFileName = /(\d{4})-(\d{2})-(\d{2}).(md|markdown)/;
// *************************************************************************************************
-function Blog(blogPath, settings) {
+function Blog(appPath, contentPath, settings) {
events.EventEmitter.call(this);
- this.path = blogPath;
+ this.appPath = appPath;
+ this.contentPath = contentPath;
this.baseURL = settings.baseURL || '';
this.hostName = settings.hostName;
this.postsPerPage = settings.postsPerPage || 10;
this.transforms = [];
- this.monitor(blogPath);
+ this.monitor(contentPath);
- var cachePath = settings.cachePath || path.join(blogPath, 'cache');
+ var cachePath = settings.cachePath || path.join(contentPath, 'cache');
this.diskCache = new DiskCache(cachePath);
if (settings.logsPath) {
@@ -38,7 +39,7 @@ function Blog(blogPath, settings) {
mkdirsSync(logsPath);
this.logPath = settings.logsPath;
} else {
- var logsPath = path.join(blogPath, 'logs');
+ var logsPath = path.join(contentPath, 'logs');
mkdirsSync(logsPath);
this.logPath = path.join(logsPath, 'log.txt');
}
@@ -89,7 +90,7 @@ subclass(Blog, events.EventEmitter, {
var postMap = {};
this.posts.forEach(function(post) {
- if (post.path in postMap) {
+ if (post.contentPath in postMap) {
postMap[post.path].push(post);
} else {
postMap[post.path] = [post];
@@ -157,13 +158,13 @@ subclass(Blog, events.EventEmitter, {
Step(
function() {
- fs.readdir(blog.path, this);
+ fs.readdir(blog.contentPath, this);
},
function(err, fileNames) {
if (err) return cb ? cb(err): 0;
articlePaths = _.map(fileNames, function(fileName) {
- return path.join(blog.path, fileName);
+ return path.join(blog.contentPath, fileName);
});
var stats = this.group();
@@ -335,11 +336,11 @@ subclass(Blog, events.EventEmitter, {
var blog = this;
var articleFileNames, articleFiles;
- fs.readdir(blog.path, function(err, fileNames) {
+ fs.readdir(blog.contentPath, function(err, fileNames) {
if (err) return cb ? cb(err): 0;
var filePaths = _.map(fileNames, function(fileName) {
- return path.join(blog.path, fileName);
+ return path.join(blog.contentPath, fileName);
});
async.map(filePaths, fs.lstat, function(err, fileStats) {
if (err) return cb ? cb(err): 0;
@@ -380,13 +381,13 @@ subclass(Blog, events.EventEmitter, {
},
_parsePostsFile: function(fileName, cb) {
- fs.readFile(path.join(this.path, fileName), 'utf8', _.bind(function(err, fileBody) {
+ fs.readFile(path.join(this.contentPath, fileName), 'utf8', _.bind(function(err, fileBody) {
this._statAndParsePosts(fileBody, fileName, cb);
}, this));
},
_statAndParsePosts: function(fileBody, fileName, cb) {
- fs.lstat(path.join(this.path, fileName), _.bind(function(err, stat) {
+ fs.lstat(path.join(this.contentPath, fileName), _.bind(function(err, stat) {
this._parsePosts(fileBody, fileName, stat, cb);
}, this));
},
@@ -417,7 +418,7 @@ subclass(Blog, events.EventEmitter, {
currentPost.title = title;
currentPost.slug = slug;
currentPost.mtime = fileStat.mtime;
- currentPost.path = path.join(this.path, fileName);
+ currentPost.path = path.join(this.contentPath, fileName);
currentPost.url = urlJoin(this.baseURL, relativeURL);
currentPost.date = postDate;
currentPost.group = postGroup;
View
59 lib/Server.js
@@ -52,7 +52,7 @@ Server.prototype = {
for (var i = 0; i < this.blogs.length; ++i) {
var blog = this.blogs[i];
if (!blog.hostName) {
- throw new Exception("Blog requires a host name (" + blog.path + ")");
+ throw new Exception("Blog requires a host name (" + blog.appPath + ")");
}
this.mainApp.use(express.vhost(blog.hostName, blog.app));
}
@@ -112,17 +112,17 @@ Server.prototype = {
var logStream = fs.createWriteStream(blog.logPath, {flags: 'a'});
app.configure(function(){
- app.set('views', path.join(blog.path, 'web', 'views'));
+ app.set('views', path.join(blog.appPath, 'views'));
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(stylus.middleware({
- src: path.join(blog.path, 'web', 'static'),
- compile: stylusCompileMethod(path.join(blog.path, 'web', 'static', 'stylesheets'))
+ src: path.join(blog.appPath, 'static'),
+ compile: stylusCompileMethod(path.join(blog.appPath, 'static', 'stylesheets'))
}));
app.use(express.logger({stream: logStream}));
app.use(app.router);
- app.use(express.static(path.join(blog.path, 'web', 'static')));
+ app.use(express.static(path.join(blog.appPath, 'static')));
});
app.configure('development', function() {
@@ -271,7 +271,7 @@ Server.prototype = {
}
function renderPage(req, res, name, locals) {
- var jadePath = path.join(blog.path, 'web', 'views', name + '.jade');
+ var jadePath = path.join(blog.appPath, 'views', name + '.jade');
jade.renderFile(jadePath, {locals: locals}, function(err, html) {
if (err) {
renderError(req, res, 500, err);
@@ -300,21 +300,46 @@ function stylusCompileMethod(stylesheetsPath) {
}
};
+function flattenSettings(settings, configName) {
+ var settings2 = {};
+ for (var name in settings) {
+ if (name == 'configs') {
+ var subsettings = settings[name][configName];
+ if (subsettings) {
+ for (var name in subsettings) {
+ settings2[name] = subsettings[name];
+ }
+ }
+ } else {
+ settings2[name] = settings[name];
+ }
+ }
+ return settings2;
+}
+
+function normalizePath(thePath) {
+ thePath = path.resolve(thePath);
+ thePath = thePath.replace('~', process.env.USER);
+ return thePath;
+}
+
// *************************************************************************************************
-exports.run = function(blogPaths) {
+exports.run = function(appPath, contentPath, configName) {
var server = new Server();
- _.each(blogPaths, function(blogPath) {
- blogPath = path.resolve(blogPath);
- if (!fs.lstatSync(blogPath)) {
- throw new Exception("Blog path not found (" + blogPath + ")");
- }
-
- var settings = JSON.parse(fs.readFileSync(path.join(blogPath, 'web', 'settings.json')));
- var blog = new Blog(blogPath, settings);
- server.addBlog(blog);
- })
+ appPath = normalizePath(appPath);
+ contentPath = normalizePath(contentPath);
+
+ if (!fs.lstatSync(appPath)) {
+ throw new Exception("Blog path not found (" + appPath + ")");
+ }
+
+ var settings = JSON.parse(fs.readFileSync(path.join(appPath, 'settings.json')));
+ settings = flattenSettings(settings, configName||'mac');
+
+ var blog = new Blog(appPath, contentPath, settings);
+ server.addBlog(blog);
server.restart();
}
Please sign in to comment.
Something went wrong with that request. Please try again.