-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
265 lines (223 loc) · 6.42 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*!
* js-comments <https://github.com/jonschlinkert/js-comments>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
/**
* Module dependencies.
*/
var fs = require('fs');
var path = require('path');
var relative = require('relative');
var union = require('arr-union');
var parse = require('parse-comments');
var helpers = require('logging-helpers');
var writeFile = require('write');
var _ = require('lodash');
/**
* Default template to use
*/
var defaultTemplate = require('js-comments-template');
/**
* Parse comments from the given `str`.
*
* ```js
* var fs = require('fs');
* var str = fs.readFileSync('foo.js', 'utf8');
* comments.parse(str, options);
* ```
*
* @param {String} `str` The string to parse.
* @param {Object} `options` Options to pass to [parse-comments]
* @return {Array} Array of comment objects.
* @api public
*/
exports.parse = parse;
/**
* Process the given Lo-dash `template` string, passing a
* `comments` object as context.
*
* ```js
* comments.render(obj, options);
* ```
*
* @param {Array} `comments` Array of comment objects.
* @param {String} `template` The lo-dash template to use.
* @return {String}
* @api public
*/
exports.render = function render(comments, template, options) {
if (typeof template !== 'string') {
options = template;
template = null;
}
var defaults = {file: {path: ''}};
var opts = _.merge({}, defaults, options);
opts.file.path = relative(opts.src || opts.path || opts.file.path || '');
var ctx = _.cloneDeep(opts);
ctx.file.comments = comments;
_.merge(ctx, comments.options);
if (opts.filter !== false) {
ctx.file.comments = exports.filter(ctx.file.comments, ctx);
}
var settings = {};
settings.imports = _.merge({}, helpers, ctx.imports);
var fn = ctx.engine || _.template;
ctx.template = template || ctx.template || defaultTemplate;
var result = fn(ctx.template, settings)(ctx);
if (ctx.format) return exports.format(result);
return result;
};
/**
* Basic markdown corrective formatting
*/
exports.format = function format(str) {
str = str.replace(/(?:\r\n|\n){3,}/g, '\n\n');
var headingRe = /^(#{1,6})\s*([^\n]+)\s*/gm;
var boldRe = /^\s+\*\*([^\n]+)\*\*(?=\n)\s+/gm;
var match;
while(match = headingRe.exec(str)) {
str = str.split(match[0]).join(match[1] + ' ' + match[2] + '\n\n');
}
while(match = boldRe.exec(str)) {
str = str.split(match[0]).join('\n**' + match[1] + '**\n\n');
}
return str.trim();
};
/**
* Write markdown API documentation to the given `dest` from the code
* comments in the given JavaScript `src` file.
*
* @param {String} `src` Source file path.
* @param {String} `dest` Destination file path.
* @param {Object} `options`
* @return {String} API documentation
* @api public
*/
exports.renderFile = function renderFile(src, dest, options) {
var opts = _.merge({src: path.resolve(src), dest: path.resolve(dest)}, options);
var str = fs.readFileSync(opts.src, 'utf8');
var ctx = exports.filter(exports.parse(str, opts), opts);
var res = exports.format(exports.render(ctx, opts)).trim();
writeFile.sync(dest, res);
};
/**
* Filter and normalize the given `comments` object.
*
* @param {Object} `comments`
* @param {Object} `options`
* @return {Object} Normalized comments object.
*/
exports.filter = function filter(comments, opts) {
comments = comments || [];
opts = opts || {};
var len = comments.length, i = 0;
var res = [];
while (len--) {
var o = comments[i++];
if (o.comment.begin === 1) {
continue;
}
if (o.string && /^.{1,8}!/.test(o.string)) {
continue;
}
if (opts.stripBanner && i === 0) {
continue;
}
if (o.type === 'property') {
continue;
}
if (!o.api || o.api !== 'public') {
continue;
}
// If the user explicitly defines a `@type`,
// use that instead of the code context type
if (o && o.type) {
o.type = o.type;
}
if (o['public']) {
o.api = 'public';
}
// update line numbers
o.begin = o.begin || o.comment.begin;
o.context.begin = o.context.begin || o.begin;
o.end = o.end || o.comment.end;
o.line = o.end ? (o.end + 2) : o.begin;
if (o.returns && o.returns.length) {
o.returns.map(function (ele) {
var len = ele.description.length;
if (ele.description.charAt(0) === '{' && ele.description[len - 1] === '}') {
ele.type = ele.description.slice(1, len - 1);
delete ele.description;
}
});
}
if (o.doc) {
if (o.doc.indexOf('./') === 0) {
var src = opts.src || o.file && o.file.path;
if (src) {
var dir = path.dirname(src);
o.doc = path.resolve(dir, o.doc);
var str = fs.readFileSync(o.doc, 'utf8');
o.extras = str;
}
} else {
var tmp = o.doc;
var tag = makeTag(tmp, opts);
o.doc = null;
o.description = o.description || '';
o.description = tag + '\n\n' + o.description;
}
}
if (o.noname) {
o.heading.text = o.noname;
res.push(o);
o = null;
return res;
}
var heading = o.heading || {};
if (o.section === true) {
heading.level = 1;
}
var lvl = heading.level || 2;
if (o.name) {
heading.text = o.name;
}
if (!o.section) {
lvl = heading.lvl || 2;
}
var text = heading.text;
var prefix = '######'.slice(0, lvl + 1);
o.prefix = prefix;
o.lvl = lvl;
if (text) {
if (heading.prefix && heading.prefix === '.' && text.charAt(0) !== '.') {
text = heading.prefix + text;
heading = null;
}
o.prefixed = prefix + ' ' + text;
o.title = text;
if (/^\.{2,}/.test(o.title)) {
o.title = '.' + o.title.replace(/^\.+/, '');
}
}
o.examples = o.examples || [];
if (o.name && typeof opts.examples === 'object' && opts.examples.hasOwnProperty(o.name)) {
o.examples = union(o.examples, opts.examples[o.name]);
}
o.examples.forEach(function (example) {
o.description = o.description.split(example.block).join('');
o.description = o.description.split(/\s+\*\*Examples?\*\*\s+/).join('\n');
o.description = o.description.split(/\n{2,}/).join('\n').replace(/\s+$/, '');
});
res.push(o);
o = null;
}
return res;
};
function makeTag(str, opts) {
if (opts && opts.tag) return opts.tag(str);
return '<%= docs("' + str + '") %>';
}