forked from documentationjs/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
230 lines (214 loc) · 8.13 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict';
var fs = require('fs'),
sort = require('./lib/sort'),
nest = require('./lib/nest'),
filterAccess = require('./lib/filter_access'),
filterJS = require('./lib/filter_js'),
dependency = require('./lib/input/dependency'),
shallow = require('./lib/input/shallow'),
parseJavaScript = require('./lib/parsers/javascript'),
polyglot = require('./lib/parsers/polyglot'),
github = require('./lib/github'),
hierarchy = require('./lib/hierarchy'),
inferName = require('./lib/infer/name'),
inferKind = require('./lib/infer/kind'),
inferAugments = require('./lib/infer/augments'),
inferParams = require('./lib/infer/params'),
inferProperties = require('./lib/infer/properties'),
inferMembership = require('./lib/infer/membership'),
inferReturn = require('./lib/infer/return'),
inferAccess = require('./lib/infer/access'),
formatLint = require('./lib/lint').formatLint,
lintComments = require('./lib/lint').lintComments;
/**
* Build a pipeline of comment handlers.
* @param {...Function|null} args - Pipeline elements. Each is a function that accepts
* a comment and can return a comment or undefined (to drop that comment).
* @returns {Function} pipeline
* @private
*/
function pipeline() {
var elements = arguments;
return function (comment) {
for (var i = 0; comment && i < elements.length; i++) {
if (elements[i]) {
comment = elements[i](comment);
}
}
return comment;
};
}
/**
* Given an array of indexes and options for whether to resolve shallow
* or deep dependencies, resolve dependencies.
*
* @param {Array<string>|string} indexes files to process
* @param {Object} options options
* @param {Function} callback called with results
* @returns {undefined}
*/
function expandInputs(indexes, options, callback) {
var inputFn = (options.polyglot || options.shallow) ? shallow : dependency;
inputFn(indexes, options, callback);
}
/**
* Generate JavaScript documentation as a list of parsed JSDoc
* comments, given a root file as a path.
*
* @param {Array<string>|string} indexes files to process
* @param {Object} options options
* @param {Array<string>} options.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [options.polyglot=false] parse comments with a regex rather than
* a proper parser. This enables support of non-JavaScript languages but
* reduces documentation's ability to infer structure of code.
* @param {boolean} [options.shallow=false] whether to avoid dependency parsing
* even in JavaScript code. With the polyglot option set, this has no effect.
* @param {Array<string|Object>} [options.order=[]] optional array that
* defines sorting order of documentation
* @param {Array<string>} [options.access=[]] an array of access levels
* to output in documentation
* @param {Object} [options.hljs] hljs optional options
* @param {boolean} [options.hljs.highlightAuto=false] hljs automatically detect language
* @param {Array} [options.hljs.languages] languages for hljs to choose from
* @param {Function} callback to be called when the documentation generation
* is complete, with (err, result) argumentsj
* @returns {undefined} calls callback
* @public
*/
module.exports.build = function (indexes, options, callback) {
options = options || {};
if (typeof indexes === 'string') {
indexes = [indexes];
}
return expandInputs(indexes, options, function (error, inputs) {
if (error) {
return callback(error);
}
try {
callback(null, buildSync(inputs, options));
} catch (e) {
callback(e);
}
});
};
/**
* Generate JavaScript documentation given a list of inputs. This internal
* method does not support require-following and it returns its results
* synchronously, rather than by calling a callback.
*
* @param {Array<string>} indexes files to process
* @param {Object} options options
* @param {Array<string>} options.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [options.polyglot=false] parse comments with a regex rather than
* a proper parser. This enables support of non-JavaScript languages but
* reduces documentation's ability to infer structure of code.
* @param {boolean} [options.shallow=false] whether to avoid dependency parsing
* even in JavaScript code. With the polyglot option set, this has no effect.
* @param {Array<string|Object>} [options.order=[]] optional array that
* defines sorting order of documentation
* @param {Array<string>} [options.access=[]] an array of access levels
* to output in documentation
* @param {Object} [options.hljs] hljs optional options
* @param {boolean} [options.hljs.highlightAuto=false] hljs automatically detect language
* @param {Array} [options.hljs.languages] languages for hljs to choose from
* @returns {Object} list of results
* @public
*/
function buildSync(indexes, options) {
options = options || {};
options.hljs = options.hljs || {};
if (!options.access) {
options.access = ['public', 'undefined', 'protected'];
}
var parseFn = (options.polyglot) ? polyglot : parseJavaScript;
return filterAccess(options.access,
hierarchy(
sort(indexes.map(function (index) {
if (typeof index === 'string') {
return {
source: fs.readFileSync(index, 'utf8'),
file: index
};
}
return index;
}).filter(filterJS(options.extension, options.polyglot))
.reduce(function (memo, file) {
return memo.concat(parseFn(file));
}, [])
.map(pipeline(
inferName(),
inferAccess(options.inferPrivate),
inferAugments(),
inferKind(),
inferParams(),
inferProperties(),
inferReturn(),
inferMembership(),
nest,
options.github && github
))
.filter(Boolean), options)));
}
/**
* Lint files for non-standard or incorrect documentation
* information, returning a potentially-empty string
* of lint information intended for human-readable output.
*
* @param {Array<string>|string} indexes files to process
* @param {Object} options options
* @param {Array<string>} options.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [options.polyglot=false] parse comments with a regex rather than
* a proper parser. This enables support of non-JavaScript languages but
* reduces documentation's ability to infer structure of code.
* @param {boolean} [options.shallow=false] whether to avoid dependency parsing
* even in JavaScript code. With the polyglot option set, this has no effect.
* @param {Function} callback to be called when the documentation generation
* is complete, with (err, result) argumentsj
* @returns {undefined} calls callback
* @public
*/
module.exports.lint = function lint(indexes, options, callback) {
options = options || {};
if (typeof indexes === 'string') {
indexes = [indexes];
}
var parseFn = (options.polyglot) ? polyglot : parseJavaScript;
return expandInputs(indexes, options, function (error, inputs) {
if (error) {
return callback(error);
}
callback(null,
formatLint(hierarchy(
inputs
.filter(filterJS(options.extension, options.polyglot))
.reduce(function (memo, file) {
return memo.concat(parseFn(file));
}, [])
.map(pipeline(
lintComments,
inferName(),
inferAccess(options.inferPrivate),
inferAugments(),
inferKind(),
inferParams(),
inferProperties(),
inferReturn(),
inferMembership(),
nest))
.filter(Boolean))));
});
};
module.exports.expandInputs = expandInputs;
module.exports.buildSync = buildSync;
module.exports.formats = {
html: require('./lib/output/html'),
md: require('./lib/output/markdown'),
remark: require('./lib/output/markdown_ast'),
json: require('./lib/output/json')
};