From 6b98d7136429d19abc4a588aa884e2dd203931b2 Mon Sep 17 00:00:00 2001 From: neocotic Date: Mon, 18 Mar 2013 12:10:00 +0000 Subject: [PATCH] added callback argument to document function --- docco.js | 39 +++++++++++++++++++++++++++++++++------ docco.litcoffee | 22 +++++++++++++++++----- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/docco.js b/docco.js index cfcca95d..64b13875 100644 --- a/docco.js +++ b/docco.js @@ -1,27 +1,45 @@ -// Generated by CoffeeScript 1.6.1 +// Generated by CoffeeScript 1.6.2 (function() { var Docco, commander, config, configure, document, exec, ext, format, fs, getLanguage, highlight, l, languages, marked, parse, path, run, spawn, version, write, _, _ref, __slice = [].slice; - document = function(options) { + document = function(options, callback) { if (options == null) { options = {}; } configure(options); return exec("mkdir -p " + config.output, function() { - var files, nextFile; - exec("cp -f " + config.css + " " + config.output); + var complete, completed, files, nextFile, steps; + + steps = 2; + completed = 0; + complete = function(error) { + if (error) { + if (_.isFunction(callback)) { + return callback(error); + } else { + throw error; + } + } + if (++completed === steps) { + return typeof callback === "function" ? callback() : void 0; + } + }; + exec("cp -f " + config.css + " " + config.output, complete); if (fs.existsSync(config["public"])) { - exec("cp -fR " + config["public"] + " " + config.output); + steps++; + exec("cp -fR " + config["public"] + " " + config.output, complete); } files = config.sources.slice(); nextFile = function() { var source; + source = files.shift(); return fs.readFile(source, function(error, buffer) { var code, sections; + if (error) { - throw error; + complete(error); } code = buffer.toString(); sections = parse(source, code); @@ -29,6 +47,8 @@ write(source, sections); if (files.length) { return nextFile(); + } else { + return complete(); } }); }; @@ -38,6 +58,7 @@ parse = function(source, code) { var codeText, docsText, hasCode, i, lang, line, lines, match, prev, save, sections, _i, _j, _len, _len1; + lines = code.split('\n'); sections = []; lang = getLanguage(source); @@ -78,6 +99,7 @@ format = function(source, sections) { var code, i, language, section, _i, _len, _results; + language = getLanguage(source); _results = []; for (i = _i = 0, _len = sections.length; _i < _len; i = ++_i) { @@ -92,6 +114,7 @@ write = function(source, sections) { var destination, first, hasTitle, html, title; + destination = function(file) { return path.join(config.output, path.basename(file, path.extname(file)) + '.html'); }; @@ -121,6 +144,7 @@ configure = function(options) { var dir; + _.extend(config, _.pick.apply(_, [options].concat(__slice.call(_.keys(config))))); if (options.template) { config.layout = null; @@ -135,6 +159,7 @@ config.template = _.template(fs.readFileSync(config.template).toString()); return config.sources = options.args.filter(function(source) { var lang; + lang = getLanguage(source, config); if (!lang) { console.warn("docco: skipped unknown type (" + m + ")"); @@ -167,6 +192,7 @@ getLanguage = function(source) { var codeExt, codeLang, lang; + ext = config.extension || path.extname(source) || path.basename(source); lang = languages[ext]; if (lang && lang.name === 'markdown') { @@ -184,6 +210,7 @@ run = function(args) { var c; + if (args == null) { args = process.argv; } diff --git a/docco.litcoffee b/docco.litcoffee index 7f4850be..1c807e5f 100644 --- a/docco.litcoffee +++ b/docco.litcoffee @@ -77,25 +77,37 @@ assets, reading all the source files in, splitting them up into prose+code sections, highlighting each file in the appropriate language, and printing them out in an HTML template. - document = (options = {}) -> + document = (options = {}, callback) -> configure options exec "mkdir -p #{config.output}", -> - exec "cp -f #{config.css} #{config.output}" - exec "cp -fR #{config.public} #{config.output}" if fs.existsSync config.public + steps = 2 + completed = 0 + + complete = (error) -> + if error + if _.isFunction callback then return callback error + else throw error + + callback?() if ++completed is steps + + exec "cp -f #{config.css} #{config.output}", complete + if fs.existsSync config.public + steps++ + exec "cp -fR #{config.public} #{config.output}", complete files = config.sources.slice() nextFile = -> source = files.shift() fs.readFile source, (error, buffer) -> - throw error if error + complete error if error code = buffer.toString() sections = parse source, code format source, sections write source, sections - nextFile() if files.length + if files.length then nextFile() else complete() nextFile()