Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #167 from neocotic/async-support

Callback
  • Loading branch information...
commit 0b4678fb30ee1c450a419832a29b5c309e587336 2 parents 92051d9 + ab08a11
@jashkenas authored
Showing with 34 additions and 13 deletions.
  1. +24 −8 docco.js
  2. +10 −5 docco.litcoffee
View
32 docco.js
@@ -1,27 +1,34 @@
-// 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);
- if (fs.existsSync(config["public"])) {
- exec("cp -fR " + config["public"] + " " + config.output);
- }
+ var complete, files, nextFile;
+
+ 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);
+ };
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;
+ return callback(error);
}
code = buffer.toString();
sections = parse(source, code);
@@ -29,6 +36,8 @@
write(source, sections);
if (files.length) {
return nextFile();
+ } else {
+ return complete();
}
});
};
@@ -38,6 +47,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 +88,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 +103,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 +133,7 @@
configure = function(options) {
var dir;
+
_.extend(config, _.pick.apply(_, [options].concat(__slice.call(_.keys(config)))));
if (options.template) {
config.layout = null;
@@ -135,6 +148,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 +181,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 +199,7 @@
run = function(args) {
var c;
+
if (args == null) {
args = process.argv;
}
View
15 docco.litcoffee
@@ -77,25 +77,30 @@ 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
+ 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
+
files = config.sources.slice()
nextFile = ->
source = files.shift()
fs.readFile source, (error, buffer) ->
- throw error if error
+ return callback 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()
Please sign in to comment.
Something went wrong with that request. Please try again.