Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clean up and document code some
  • Loading branch information
jnordberg committed Apr 2, 2012
1 parent ea953d6 commit b1cf607
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
25 changes: 4 additions & 21 deletions src/common.coffee
Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/content.coffee
Expand Up @@ -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) ->
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/renderer.coffee
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/server.coffee
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/templates.coffee
Expand Up @@ -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) ->
Expand Down

0 comments on commit b1cf607

Please sign in to comment.