-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
192 lines (160 loc) · 4.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// license: MIT
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var markdox = require('markdox');
var path = require('path');
var async = require('async');
var _ = require('underscore');
module.exports = function(options) {
var stream = gutil.noop();
var output = [];
var afterRender = stream
.pipe(module.exports.parse(options))
.pipe(module.exports.format())
.pipe(module.exports.render())
.pipe(through.obj(function(chunk, enc, callback) {
output.push(chunk);
return callback();
}));
function write(chunk, enc, callback) {
return stream.write(chunk, enc, callback);
}
function flush(callback) {
var self = this;
afterRender.on('finish', function(err) {
output.forEach(self.push.bind(self));
callback(err)
});
stream.end();
}
var retVal = through.obj(write, flush);
return retVal;
};
_.extend(module.exports, {
parse: stream(requireBuffer(parse), noop),
format: stream(requireProperty('javadoc', format), noop),
render: stream(requireProperty('formattedDoc', maybeConcat), render, createArray('chunks')),
});
function parse(self, options, chunk, callback) {
chunk.path = path.relative(process.cwd(), chunk.path);
chunk.cwd = process.cwd();
markdox.parse(chunk.path, options, function(err, doc) {
if (err) {
self.emit('error', gulpError(err));
return callback();
}
chunk.javadoc = doc;
self.push(chunk);
return callback();
});
}
function format(self, options, chunk, callback) {
try {
chunk.formattedDoc = options.formatter({
filename: chunk.path,
javadoc: chunk.javadoc,
options: options,
});
if (!chunk.formattedDoc) {
self.emit('error', gulpError('No document returned from formatter: '+ chunk.formatterDoc));
}
self.push(chunk);
return callback();
} catch(e) {
self.emit('error', gulpError(e));
return callback(e);
}
}
function maybeConcat(self, options, chunk, callback) {
var path = chunk.path;
if (typeof options.concat === 'string') {
path = options.concat;
}
var chunks = self.chunks;
(chunks[path] = chunks[path] || []).push(chunk);
return callback();
}
function render(self, callback) {
// intermediate map used to assure
// the same order of chunks after async processing
var rendered = {};
var length = 0;
async.each(_.keys(self.chunks), render0, flush);
function render0(outputPath, cb) {
var input = self.chunks[outputPath];
var inputDocs = input.map(function(file) { return file.formattedDoc; });
var options = input[0].markdoxOptions;
var index = length++;
markdox.generate(inputDocs, options, function(err, result) {
if (err) {
self.emit('error', gulpError(err));
return cb(err);
}
var file = new gutil.File({ path: outputPath });
file.contents = new Buffer(result);
rendered[index] = file;
return cb();
});
}
function flush(err) {
_.keys(rendered)
.sort(function(a, b) { return a > b; })
.map(function(index) { return rendered[index]; })
.forEach(function(file) { self.push(file); })
;
callback(err);
}
}
function noop(self, callback) {
return callback();
}
function stream(transform, flush) {
var initializers = [].slice.call(arguments, 2);
return function(options) {
function transformDecorator(chunk, enc, callback) {
chunk.markdoxOptions = _.defaults(options || chunk.markdoxOptions || {}, {
output: false,
encoding: enc,
formatter: markdox.defaultFormatter,
});
if (chunk.isNull()) {
this.push(chunk);
return callback();
}
return transform(this, chunk.markdoxOptions, chunk, callback);
}
function flushDecorator(callback) {
return flush(this, callback);
}
var retVal = through.obj(transformDecorator, flushDecorator);
initializers.forEach(function(init) { init(retVal); });
return retVal;
};
}
function requireBuffer(chunkHandler) {
return function(self, options, chunk, callback) {
if (chunk.isStream()) {
self.emit('error', gulpError('Streams are not supported'));
return callback();
}
return chunkHandler(self, options, chunk, callback);
};
}
function requireProperty(propertyName, chunkHandler) {
return function(self, options, chunk, callback) {
if (typeof chunk[propertyName] === 'undefined') {
self.emit('error', gulpError('Couldn\'t find property on data chunk: "'+ propertyName +'"'));
return callback();
}
return chunkHandler(self, options, chunk, callback);
};
}
function createArray(propertyName) {
return function(self) {
self[propertyName] = [];
};
}
function gulpError(message) {
return new gutil.PluginError('gulp-markdox', message);
}