From b1cf60766da8770e11761777c87afaa72388ca5c Mon Sep 17 00:00:00 2001 From: Johan Nordberg Date: Mon, 2 Apr 2012 05:02:21 +0200 Subject: [PATCH] Clean up and document code some --- src/common.coffee | 25 ++++--------------------- src/content.coffee | 7 +++++++ src/renderer.coffee | 4 +++- src/server.coffee | 4 ++-- src/templates.coffee | 1 + 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/common.coffee b/src/common.coffee index c86451bf..a7ca5f04 100644 --- a/src/common.coffee +++ b/src/common.coffee @@ -18,6 +18,8 @@ stripExtension = (filename) -> exports.stripExtension = stripExtension rfc822 = (date) -> + ### return a rfc822 representation of a javascript Date object + http://www.w3.org/Protocols/rfc822/#z28 ### pad = (i) -> if i < 10 then '0' + i else i tzoffset = (offset) -> hours = Math.floor offset / 60 @@ -39,25 +41,6 @@ rfc822 = (date) -> exports.rfc822 = rfc822 -copyFile = (source, destination, overwrite, callback) -> - if !callback? - callback = overwrite - overwrite = false - - path.exists destination, (exists) -> - if exists and !overwrite - callback new Error "File #{ destination } already exists." - else - fs.stat source, (error) -> - if error - callback error - else - read = fs.createReadStream source - write = fs.createWriteStream destination - util.pump read, write, callback - -exports.copyFile = copyFile - class cli extends winston.Transport name: 'cli' @@ -86,8 +69,7 @@ class cli extends winston.Transport msg = msg.yellow if meta msg += util.format ' %j', meta - if level != 'help' then msg = ' ' + msg # flatiron pads help messages :/ - process.stdout.write msg + '\n' + process.stdout.write " #{ msg }\n" @emit 'logged' callback null, true @@ -101,6 +83,7 @@ exports.logger = new winston.Logger transports: transports exports.readJSON = (filename, callback) -> + ### read and try to parse *filename* as json ### async.waterfall [ (callback) -> fs.readFile filename, callback diff --git a/src/content.coffee b/src/content.coffee index 97e9b59c..7773f7ac 100644 --- a/src/content.coffee +++ b/src/content.coffee @@ -101,9 +101,11 @@ ContentTree.fromDirectory = (directory, base, callback) -> callback = base base = directory + # create the base tree from *directory* tree = new ContentTree path.relative(base, directory) async.waterfall [ + # read directory async.apply fs.readdir, directory (filenames, callback) -> async.forEach filenames, (filename, callback) -> @@ -112,14 +114,17 @@ ContentTree.fromDirectory = (directory, base, callback) -> async.apply fs.lstat, filename (stats, callback) -> if stats.isDirectory() + # recursively map directories to content tree instances ContentTree.fromDirectory filename, base, (error, result) -> tree[path.relative(directory, filename)] = result tree._.directories.push result callback error else if stats.isFile() + # map any files found to content plugins basename = path.basename filename relname = path.relative base, filename # iterate backwards over all content plugins + # and check if any plugin can handle this file match = false for i in [contentPlugins.length - 1..0] by -1 plugin = contentPlugins[i] @@ -143,6 +148,7 @@ ContentTree.fromDirectory = (directory, base, callback) -> callback error, tree ContentTree.inspect = (tree, depth=0) -> + ### return a pretty formatted string representing the content *tree* ### rv = [] pad = '' for i in [0..depth] @@ -163,6 +169,7 @@ ContentTree.inspect = (tree, depth=0) -> util = require 'util' ContentTree.flatten = (tree) -> + ### return all the items in the *tree* as an array of content plugins ### rv = [] for key, value of tree if value instanceof ContentTree diff --git a/src/renderer.coffee b/src/renderer.coffee index c31a28b9..039144e1 100644 --- a/src/renderer.coffee +++ b/src/renderer.coffee @@ -14,7 +14,7 @@ render = (contents, templates, location, locals, callback) -> logger.verbose "rendering into: #{ location }" logger.info "rendering tree:\n#{ ContentTree.inspect(contents, 1) }\n" - locals.contents = contents # all pages have access to the content-tree + locals.contents = contents # all plugins have access to the content-tree renderPlugin = (content, callback) -> ### render *content* plugin, calls *callback* with true if a file is written; @@ -42,12 +42,14 @@ render = (contents, templates, location, locals, callback) -> directory = path.join location, tree.filename async.waterfall [ (callback) -> + # create directory for tree fs.mkdir directory, (error) -> if not error or error.code == 'EEXIST' callback() else callback error (callback) -> + # recursively render tree and its plugins async.map Object.keys(tree), (key, callback) -> item = tree[key] if item instanceof ContentTree diff --git a/src/server.coffee b/src/server.coffee index 988c14ee..ff00706f 100644 --- a/src/server.coffee +++ b/src/server.coffee @@ -10,8 +10,6 @@ mime = require 'mime' {logger, extend, stripExtension} = require './common' {loadTemplates, ContentTree} = require './' -#{renderResource, renderPage} = require './renderer' - colorCode = (code) -> s = code.toString() switch Math.floor code / 100 @@ -31,11 +29,13 @@ setup = (options, callback) -> logger.verbose "contentHandler: #{ uri }" async.waterfall [ (callback) -> + # load contents and templates async.parallel templates: async.apply loadTemplates, options.templates contents: async.apply ContentTree.fromDirectory, options.contents , callback (result, callback) -> + # render if uri matches {contents, templates} = result async.detect ContentTree.flatten(contents), (item, callback) -> callback (uri is item.url) diff --git a/src/templates.coffee b/src/templates.coffee index 4c79ef0e..9de531a7 100644 --- a/src/templates.coffee +++ b/src/templates.coffee @@ -34,6 +34,7 @@ loadTemplates = (location, callback) -> nosort: true loadPluginTemplates = (plugin, callback) -> + ### scans *location* and loads any templates for *plugin* if its glob pattern matches ### async.waterfall [ async.apply glob, plugin.pattern, opts (files, callback) ->