forked from cbou/markdox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdox.js
197 lines (161 loc) · 4.49 KB
/
markdox.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
/*!
* Module dependencies.
*/
var dox = require('dox')
, fs = require('fs')
, async = require('async')
, version = require('../package.json').version
, formatter = require('../lib/formatter').format
, compiler = require('../lib/compiler').compile
, async = require('async')
, fs = require('fs')
, util = require('util')
, ejs = require('ejs')
, u = require('underscore')
, path = require('path');
/*!
* Library version.
*/
exports.version = version;
exports.defaultFormatter = formatter;
exports.defaultCompiler = compiler;
exports.defaultTemplate = __dirname + '/../templates/template.md.ejs';
/**
* Parses and generates the documentation for given files.
*
* ### Available options:
*
* * {String} output: Path or the output to produce
* * {String} template: Path or the custom template
* * {String} encoding: Encoding of templates and files to parse
* * {Function} formatter: Custom formatter
* * {Function} compiler: Compiler (for example for CoffeeScript IcedCoffeeScript)
*
* ### Examples:
*
* var markdox = require('markdox');
* markdox.parse('/path/to/file.js', callback);
*
* @param {String|Array} files Files to process
* @param {Object|Function|String} options The options or the callback (if there is not options) or the output option
* @param {Function} callback The callback, it gets two arguments (err, output)
*
* @api public
*/
exports.process = function (files, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
} else if (typeof options === 'string') {
options = {
output: options
};
}
callback = callback || function() {};
options = options || {};
options = u.defaults(options, {
output: false
, encoding: 'utf-8'
, formatter: formatter
});
if (!util.isArray(files)) {
files = [files];
}
var docfiles = [];
async.forEach(files, function(file, callback){
exports.parse(file, options, function(err, doc){
var docfile = {
filename: file,
javadoc: doc
};
var formatedDocfile = options.formatter(docfile);
docfiles.push(formatedDocfile);
callback(err);
});
}, function (err) {
exports.generate(docfiles, options, function(err, output){
if (err) {
throw err;
}
if (typeof options.output === 'string') {
fs.writeFile(options.output, output, options.encoding, function(err){
if (err) {
throw err;
}
callback(null, output);
});
} else {
callback(null, output);
}
});
});
};
/**
* Parses the given file.
*
* ### Examples:
*
* var markdox = require('markdox');
* markdox.parse('/path/to/file.js', callback);
*
* @param {String} filepath Filepath to parse
* @param {Object|Function} options The options or the callback (if there is not options)
* @param {Function} callback The callback, it gets two arguments (err, result)
*
* @api public
*/
exports.parse = function(filepath, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
callback = callback || function() {};
options = options || {};
options = u.defaults(options, {
encoding: 'utf-8'
, compiler: compiler
});
fs.readFile(filepath, options.encoding, function(err, data){
data = options.compiler(filepath, data);
callback(err, dox.parseComments(data, {raw: true}));
});
};
/**
* Generates the output for comments.
*
* @param {Object} docfiles Comments to render
* @param {Object|Function|String} options The options or the callback (if there is not options)
* @param {Function} callback The callback, it gets two arguments (err, output)
*
* @api public
*/
exports.generate = function(docfiles, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
callback = callback || function() {};
options = options || {};
options = u.defaults(options, {
template: exports.defaultTemplate
, encoding: 'utf-8'
});
ejs.open = '<?';
ejs.close = '?>';
fs.readFile(options.template, options.encoding, function(err, data){
if (err) {
throw err;
}
// Remove indentation
data = data.replace(/\n */g, '\n');
var output = ejs.render(data, {
docfiles: docfiles,
escape: function(html) {
return String(html);
}
});
// Remove double lines
output = output.replace(/\n{3,}/g, '\n\n');
callback(null, output);
});
};