From 6b98d7136429d19abc4a588aa884e2dd203931b2 Mon Sep 17 00:00:00 2001 From: neocotic Date: Mon, 18 Mar 2013 12:10:00 +0000 Subject: [PATCH 1/4] 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() From 80b91e48083d6957e80ca2a659a067d1fc3ef4af Mon Sep 17 00:00:00 2001 From: neocotic Date: Tue, 19 Mar 2013 13:07:05 +0000 Subject: [PATCH 2/4] reduced code size and greatly improved simplicity --- docco.js | 31 +++++++++++-------------------- docco.litcoffee | 22 +++++++--------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/docco.js b/docco.js index 64b13875..fd59694d 100644 --- a/docco.js +++ b/docco.js @@ -7,29 +7,20 @@ if (options == null) { options = {}; } + if (callback == null) { + callback = function(error) { + if (error) { + throw error; + } + }; + } configure(options); return exec("mkdir -p " + config.output, function() { - var complete, completed, files, nextFile, steps; + var complete, files, nextFile; - 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; - } + complete = function() { + return exec(["cp -f " + config.css + " " + config.output, fs.existsSync(config["public"]) ? "cp -fR " + config["public"] + " " + config.output : void 0].join('&'), callback); }; - 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 = function() { var source; @@ -39,7 +30,7 @@ var code, sections; if (error) { - complete(error); + return callback(error); } code = buffer.toString(); sections = parse(source, code); diff --git a/docco.litcoffee b/docco.litcoffee index 1c807e5f..f7b4f16f 100644 --- a/docco.litcoffee +++ b/docco.litcoffee @@ -77,31 +77,23 @@ 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 = {}, callback) -> + document = (options = {}, callback = (error) -> throw error if error) -> configure options exec "mkdir -p #{config.output}", -> - steps = 2 - completed = 0 + complete = -> + exec [ + "cp -f #{config.css} #{config.output}" + "cp -fR #{config.public} #{config.output}" if fs.existsSync config.public + ].join('&'), callback - 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) -> - complete error if error + return callback error if error code = buffer.toString() sections = parse source, code From 917e1b158276e63b5e873cd605293ae796eebe6f Mon Sep 17 00:00:00 2001 From: neocotic Date: Tue, 19 Mar 2013 13:27:49 +0000 Subject: [PATCH 3/4] further changes and refinement --- docco.js | 16 ++++++++-------- docco.litcoffee | 7 ++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/docco.js b/docco.js index fd59694d..29f73f85 100644 --- a/docco.js +++ b/docco.js @@ -7,19 +7,19 @@ if (options == null) { options = {}; } - if (callback == null) { - callback = function(error) { - if (error) { - throw error; - } - }; - } configure(options); return exec("mkdir -p " + config.output, function() { var complete, files, nextFile; + if (callback == null) { + callback = function(error) { + if (error) { + throw error; + } + }; + } complete = function() { - return exec(["cp -f " + config.css + " " + config.output, fs.existsSync(config["public"]) ? "cp -fR " + config["public"] + " " + config.output : void 0].join('&'), callback); + return exec(["cp -f " + config.css + " " + config.output, fs.existsSync(config["public"]) ? "cp -fR " + config["public"] + " " + config.output : void 0].join('&&'), callback); }; files = config.sources.slice(); nextFile = function() { diff --git a/docco.litcoffee b/docco.litcoffee index f7b4f16f..9a7eec59 100644 --- a/docco.litcoffee +++ b/docco.litcoffee @@ -77,16 +77,17 @@ 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 = {}, callback = (error) -> throw error if error) -> + document = (options = {}, callback) -> configure options exec "mkdir -p #{config.output}", -> - complete = -> + callback ?= (error) -> throw error if error + complete = -> exec [ "cp -f #{config.css} #{config.output}" "cp -fR #{config.public} #{config.output}" if fs.existsSync config.public - ].join('&'), callback + ].join('&&'), callback files = config.sources.slice() From ab08a11b63ec269e7d4a661485feae6fd7ff0a3e Mon Sep 17 00:00:00 2001 From: neocotic Date: Tue, 19 Mar 2013 13:48:12 +0000 Subject: [PATCH 4/4] fingers-crossed: final refinements and tweaks --- docco.js | 14 ++++++-------- docco.litcoffee | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/docco.js b/docco.js index 29f73f85..23b3b632 100644 --- a/docco.js +++ b/docco.js @@ -11,15 +11,13 @@ return exec("mkdir -p " + config.output, function() { var complete, files, nextFile; - if (callback == null) { - callback = function(error) { - if (error) { - throw error; - } - }; - } + callback || (callback = function(error) { + if (error) { + throw error; + } + }); complete = function() { - return exec(["cp -f " + config.css + " " + config.output, fs.existsSync(config["public"]) ? "cp -fR " + config["public"] + " " + config.output : void 0].join('&&'), callback); + return exec(["cp -f " + config.css + " " + config.output, fs.existsSync(config["public"]) ? "cp -fR " + config["public"] + " " + config.output : void 0].join(' && '), callback); }; files = config.sources.slice(); nextFile = function() { diff --git a/docco.litcoffee b/docco.litcoffee index 9a7eec59..0bc3ce41 100644 --- a/docco.litcoffee +++ b/docco.litcoffee @@ -82,12 +82,12 @@ out in an HTML template. exec "mkdir -p #{config.output}", -> - callback ?= (error) -> throw error if error - complete = -> + callback or= (error) -> throw error if error + complete = -> exec [ "cp -f #{config.css} #{config.output}" "cp -fR #{config.public} #{config.output}" if fs.existsSync config.public - ].join('&&'), callback + ].join(' && '), callback files = config.sources.slice()