Permalink
Browse files

remove index from (as|des)cending arrays

  • Loading branch information...
olegp committed Jan 11, 2013
1 parent dd33f71 commit e68e6506d9ef5f2861cb4dae2b19a0430796929c
Showing with 144 additions and 130 deletions.
  1. +1 −1 README.md
  2. +143 −129 lib/mcms.js
View
@@ -24,7 +24,7 @@ Once the server is running, open [http://localhost:8080](http://localhost:8080)
The easiest way to get started with a new site is to clone an already existing one and modify it to get the site you want (TODO: add link to repo). Alternatively, if you're starting from scratch you can also create a directory for your site. Inside it, add a single file named `index.js` with the following line:
- exports.app = require('mcms').app;
+ exports.app = require('mcms').mcms();
In the same directory, create the following sub-directories:
View
@@ -3,148 +3,162 @@ 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', INCLUDES = './includes', STATIC = './static', SEPARATOR = '|';
-var ABSOLUTE_STATIC = fs.absolute(STATIC);
-
var MIME_TYPES = {
- ".css": "text/css",
- ".gif": "image/gif",
- ".ico": "image/vnd.microsoft.icon",
- ".jpeg": "image/jpeg",
- ".jpg": "image/jpeg",
- ".js": "application/javascript",
- ".json": "application/json",
- ".png": "image/png",
- ".txt": "text/plain",
- ".zip": "application/zip"
+ ".css":"text/css",
+ ".gif":"image/gif",
+ ".ico":"image/vnd.microsoft.icon",
+ ".jpeg":"image/jpeg",
+ ".jpg":"image/jpeg",
+ ".js":"application/javascript",
+ ".json":"application/json",
+ ".png":"image/png",
+ ".txt":"text/plain",
+ ".zip":"application/zip"
};
-var pages = {}, templates = {}, includes = {}, ascending = [], descending;
+var mcms = exports.mcms = function(root) {
+ root = root || fs.workingDirectory();
+ var PAGES = fs.resolve(root, './pages'),
+ TEMPLATES = fs.resolve(root, './templates'),
+ INCLUDES = fs.resolve(root, './includes'),
+ STATIC = fs.resolve(root, './static');
+ var ABSOLUTE_STATIC = fs.absolute(STATIC);
+ var SEPARATOR = '|';
-function friendly(url) {
- return url.toLowerCase() //
- .replace(/^\s+|\s+$/g, "") // trim leading and trailing spaces
- .replace(/[_|\s]+/g, "-") // change all spaces and underscores to a hyphen
- .replace(/[^a-zF0-9-\/]+/g, "") // remove non alpha, digit, '-', '/' chars
- .replace(/[-]+/g, "-") // replace multiple hyphens with one
- .replace(/^-+|-+$/g, ""); // trim leading and trailing hyphens
-}
+ var pages = {}, templates = {}, includes = {}, ascending = [], descending;
-function read(file) {
- return fs.openRaw(file).read().decodeToString();
-}
+ function friendly(url) {
+ return url.toLowerCase()//
+ .replace(/^\s+|\s+$/g, "")// trim leading and trailing spaces
+ .replace(/[_|\s]+/g, "-")// change all spaces and underscores to a hyphen
+ .replace(/[^a-zF0-9-\/]+/g, "")// remove non alpha, digit, '-', '/' chars
+ .replace(/[-]+/g, "-")// replace multiple hyphens with one
+ .replace(/^-+|-+$/g, ""); // trim leading and trailing hyphens
+ }
-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;
-}
+ 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);
- var title = fs.base(file, ext), i = title.indexOf(SEPARATOR);
- // trim leading prefix (which is used for lexicographic sorting)
- if(i != -1) {
- title = title.substring(i + 1);
- }
- var link = friendly(file.substr(0, file.lastIndexOf(ext)));
- file = fs.join(PAGES, file);
- var page = {
- file: file,
- title: title,
- link: link,
- modified: fs.lastModified(file)
- };
- ascending.push(pages[link] = page);
- }
-});
+ fs.listTree(PAGES).splice(1).forEach(function (file) {
+ if (!fs.isDirectory(fs.join(PAGES, file))) {
+ var ext = fs.extension(file);
+ var title = fs.base(file, ext), i = title.indexOf(SEPARATOR);
+ // trim leading prefix (which is used for lexicographic sorting)
+ if (i != -1) {
+ title = title.substring(i + 1);
+ }
+ var link = friendly(file.substr(0, file.lastIndexOf(ext)));
+ file = fs.join(PAGES, file);
+ var page = {
+ file:file,
+ title:title,
+ link:link,
+ modified:fs.lastModified(file)
+ };
+ pages[link] = page;
+ if(title != 'index') {
+ ascending.push(page);
+ }
+ }
+ });
-descending = ascending.slice().reverse();
+ descending = ascending.slice().reverse();
-fs.listTree(TEMPLATES).splice(1).forEach(function(file) {
- var path = fs.join(TEMPLATES, file);
- if(!fs.isDirectory(path)) {
- var ext = fs.extension(file);
- templates[friendly(file.substr(0, file.lastIndexOf(ext)))] = read(path);
- }
-});
+ fs.listTree(TEMPLATES).splice(1).forEach(function (file) {
+ var path = fs.join(TEMPLATES, file);
+ if (!fs.isDirectory(path)) {
+ var ext = fs.extension(file);
+ templates[friendly(file.substr(0, file.lastIndexOf(ext)))] = read(path);
+ }
+ });
-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;
-});
+ 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;
+ });
// serve a static file
-function serve(uri) {
- var file = fs.absolute(fs.join(ABSOLUTE_STATIC, uri
- .substr('static'.length + 1)));
- if(!file.indexOf(ABSOLUTE_STATIC) && fs.exists(file)) {
- return {
- status: 200,
- 'Content-Type': MIME_TYPES[fs.extension(file)],
- // TODO add etag or last-modified header
- // TODO add support for caching
- body: fs.openRaw(file)
- };
- } else {
- return {
- status: 404,
- headers: {},
- body: []
- };
- }
-}
+ function serve(uri) {
+ var file = fs.absolute(fs.join(ABSOLUTE_STATIC, uri
+ .substr('static'.length + 1)));
+ if (!file.indexOf(ABSOLUTE_STATIC) && fs.exists(file)) {
+ return {
+ status:200,
+ 'Content-Type':MIME_TYPES[fs.extension(file)],
+ // TODO add etag or last-modified header
+ // TODO add support for caching
+ body:fs.openRaw(file)
+ };
+ } else {
+ return {
+ status:404,
+ headers:{},
+ body:[]
+ };
+ }
+ }
// load a dynamically generated page
-function load(name) {
- var page = pages[name];
- if(page) {
- if(!page.body) {
- var content = read(page.file);
- if(fs.extension(page.file) == '.md') {
- content = markdown(content);
- }
- var template = templates[page.link]
- || templates[fs.join(fs.directory(page.link), 'index')]
- || templates['index'];
- page.body = mustache(template, merge({
- title: page.title == 'index' ? '' : page.title,
- body: content,
- ascending: ascending,
- descending: descending
- }, includes));
- }
- return page.body;
- }
- return '';
-}
+ function load(name) {
+ var page = pages[name];
+ if (page) {
+ if (!page.body) {
+ var content = read(page.file);
+ if (fs.extension(page.file) == '.md') {
+ content = markdown(content);
+ }
+ var template = templates[page.link]
+ || templates[fs.join(fs.directory(page.link), 'index')]
+ || templates['index'];
+ page.body = mustache(template, merge({
+ title:page.title == 'index' ? '' : page.title,
+ body:content,
+ ascending:ascending,
+ descending:descending
+ }, includes));
+ }
+ return page.body;
+ }
+ return '';
+ }
-exports.app = function(request) {
- var uri = request.pathInfo.substr(1);
- if(!uri.indexOf('static')) {
- return serve(uri);
- }
- var name = friendly(uri) || 'index';
- var page = load(name);
- return {
- status: page ? 200 : 404,
- headers: {
- 'Content-Type': 'text/html'
- },
- body: [page || load('404')]
- };
-};
+ return function (request) {
+ var uri = request.pathInfo.substr(1);
+ if (!uri.indexOf('static')) {
+ return serve(uri);
+ }
+ var name = friendly(uri) || 'index';
+ var page = load(name);
+ return {
+ status:page ? 200 : 404,
+ headers:{
+ 'Content-Type':'text/html'
+ },
+ body:[page || load('404')]
+ };
+ };
+}
-if(require.main === module && !COMMON_NODE) {
- require("ringo/httpserver").main(module.id);
-}
+if (require.main === module) {
+ if(COMMON_NODE) {
+ exports.app = mcms();
+ } else {
+ require("ringo/httpserver").main(mcms());
+ }
+}

0 comments on commit e68e650

Please sign in to comment.