This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
141 lines (126 loc) · 4.61 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
var util = require('./util.js'),
repoPathParse = require('repo-path-parse'),
table = require('markdown-table'),
reformat = util.reformat,
escape = util.escape,
getTag = util.getTag,
getTags = util.getTags,
u = require('util');
/**
* A [Markdown](http://daringfireball.net/projects/markdown/) formatter
* for [dox](https://github.com/tj/dox). Takes dox's JSON output as stdin
* and writes Markdown to stdout.
*
* * Generates API documentation as well as entire READMEs (like this one!)
* * README generation supports [Travis-CI](https://travis-ci.org/) badges
*
* ## CLI Usage
*
* dox -r < index.js | doxme
*
* The `doxme` bin supports one important option: `--readme`. When
* provided, it generates a full README.md file, including title,
* description, installation and test instructions, and a
* [Travis-CI](https://travis-ci.org/) badge if the project is tested
* with travis.
*
* dox -r < index.js | doxme --readme > README.md
*
* ## See Also
*
* * [gulp-doxme](https://github.com/tomekwi/gulp-doxme) runs doxme within a
* [Gulp](http://gulpjs.com/) pipeline
*
* @module doxme
* @param {Object} dox the output of dox as a parsed JSON object
* @param {boolean} readme whether to output a readme or just docs
* @param {Object} package a parsed package.json
* @param {boolean} travis whether to output a travis badge along with docs
* @returns {String} documentation
* @example
* var fs = require('fs');
* var dox = require('dox');
* var doxme = require('doxme');
*
* var sourceCode = fs.readFileSync('./index.js', 'utf8');
* var documentation = doxme(dox.parseComments(sourceCode));
*/
module.exports = function(dox, readme, pkg, travis) {
var output = '';
function log() {
output += u.format.apply(u, arguments) + '\n';
}
if (readme) {
log('# ' + pkg.name + '\n');
if (travis) {
var repoInfo = repoPathParse(pkg.repository.url);
log('[![build status](https://secure.travis-ci.org/%s/%s.png)](http://travis-ci.org/%s/%s)\n',
repoInfo.owner, repoInfo.repo, repoInfo.owner, repoInfo.repo);
}
log(pkg.description + '\n');
}
dox.forEach(function(d) {
var access = getTag(d.tags, 'access');
// do not generate documentation for private members
if (access && access.string === 'private') return;
var name = '';
var alias = getTag(d.tags, ['alias', 'function', 'func', 'method']);
var mod = getTag(d.tags, 'module');
if (alias) {
name = alias.string;
} else if (mod) {
name = mod.string.replace('/', '.');
} else {
name = d.ctx && d.ctx.name;
}
if (name !== '') {
var args = getTags(d.tags, 'param').map(function(p) {
return p.name;
}).join(', ');
if (args) {
log('\n### `%s(%s)`\n', name, args);
} else {
log('\n### `%s`\n', name);
}
if (d.description) {
log('%s\n', reformat(d.description.full));
}
var params = getTags(d.tags, 'param');
if (params.length) {
log('### Parameters\n');
log(table(
[['parameter', 'type', 'description']]
.concat(params.map(function(p) {
var type = p.typesDescription.match(/^{/) ?
p.typesDescription.replace(/\|/g, '/') :
escape(p.types.join(','));
return ['`' + p.name + '`', type,
(p.optional ? '_optional:_ ' : '') +
reformat(p.description)];
}))
));
log('\n');
}
var examples = getTags(d.tags, 'example');
if (examples.length) {
log('### Example');
examples.forEach(function(p) {
log('\n```js\n%s\n```\n', p.string);
});
}
var returns = getTag(d.tags, ['returns', 'return']);
if (returns) {
log('\n**Returns** `%s`, %s\n', returns.types.join(','), reformat(returns.description));
}
}
});
if (readme) {
log('## Installation\n');
log('Requires [nodejs](http://nodejs.org/).\n');
log('```sh\n$ npm install ' + pkg.name + '\n```\n');
if (pkg.scripts && pkg.scripts.test) {
log('## Tests\n\n```sh\n$ npm test\n```\n');
}
}
return output;
};