Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixes #2

  • Loading branch information...
commit 1a393e014af8c130290bb887ee718f358c1b93fe 1 parent a79769d
Oleg Podsechin authored
Showing with 24 additions and 5 deletions.
  1. +24 −5 lib/mcms.js
29 lib/mcms.js
View
@@ -3,7 +3,7 @@ var fs = require(COMMON_NODE ? 'fs-base' : 'fs');
var mustache = require(COMMON_NODE ? 'mustache' : 'ringo/mustache').to_html;
var markdown = require('github-flavored-markdown').parse;
-var PAGES = './pages', TEMPLATES = './templates', STATIC = './static', SEPARATOR = '|';
+var PAGES = './pages', TEMPLATES = './templates', INCLUDES = './includes', STATIC = './static', SEPARATOR = '|';
var ABSOLUTE_STATIC = fs.absolute(STATIC);
var MIME_TYPES = {
@@ -19,7 +19,7 @@ var MIME_TYPES = {
".zip": "application/zip"
};
-var pages = {}, list = [], templates = {};
+var pages = {}, templates = {}, includes = {}, list = [];
function friendly(url) {
return url.toLowerCase() //
@@ -34,6 +34,17 @@ function read(file) {
return fs.openRaw(file).read().decodeToString();
}
+function merge() {
+ var result = {};
+ for( var i = arguments.length; i > 0; --i) {
+ var obj = arguments[i - 1];
+ for( var property in obj) {
+ result[property] = obj[property];
+ }
+ }
+ return result;
+}
+
fs.listTree(PAGES).splice(1).forEach(function(file) {
if(!fs.isDirectory(fs.join(PAGES, file))) {
var ext = fs.extension(file);
@@ -62,6 +73,15 @@ fs.listTree(TEMPLATES).splice(1).forEach(function(file) {
}
});
+fs.exists(INCLUDES) && fs.list(INCLUDES).forEach(function(file) {
+ var ext = fs.extension(file);
+ var contents = read(fs.join(INCLUDES, file));
+ if(ext == '.md') {
+ contents = markdown(contents);
+ }
+ includes[file.substr(0, file.lastIndexOf(ext))] = contents;
+});
+
exports.app = function(request) {
var uri = request.pathInfo.substr(1);
if(!uri.indexOf('static')) {
@@ -90,11 +110,11 @@ exports.app = function(request) {
|| templates[fs.join(fs.directory(name), 'index')]
|| templates['index'];
// TODO inject templates as well -- or separate them out into "partials"?
- page.body = mustache(template, {
+ page.body = mustache(template, merge({
// TODO deal with case when title == 'index'
title: page.title,
body: markdown(md)
- });
+ }, includes));
}
return {
status: page ? 200 : 404,
@@ -107,6 +127,5 @@ exports.app = function(request) {
};
if(require.main === module) {
- console.log('main');
require("ringo/httpserver").main(module.id);
}
Please sign in to comment.
Something went wrong with that request. Please try again.