-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
255 lines (227 loc) · 8.02 KB
/
main.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
#!/usr/bin/env ringo
/**
* @fileoverview Script to create static JsDoc documentation.
* Use -h or --help to get a list of available options.
*
* @see http://code.google.com/p/jsdoc-toolkit/
*/
// stdlib
var system = require('system');
var files = require('ringo/utils/files');
var fs = require('fs');
var {Parser} = require('ringo/args');
var {ScriptRepository} = require('ringo-jsdoc');
var strings = require('ringo/utils/strings');
var objects = require('ringo/utils/objects');
var {Reinhardt} = require('reinhardt');
var templates = new Reinhardt({
loader: './templates/',
filters: require('./jsdocFilters')
});
var {repositoryList, moduleDoc, moduleList, structureModuleDoc, getRepositoryName} = require('./jsdocserializer');
var defaultContext = {
"baseUri": "//ringojs.org"
};
/**
* Renders jsdoc html files for the given repository into the given directory.
*
* @param {Object} repository
* @param {String} directory
*/
var renderRepository = exports.renderRepository = function (repository, directory) {
// need apps/jsdoc on path for skin extend to work
if (require.paths.indexOf(module.directory) == -1) {
require.paths.push(module.directory);
}
print ('Writing to ' + directory + '...');
copyStaticFiles(directory);
print ('Module index');
writeRepositoryIndex(directory, repository);
print(repository.path);
writeModuleList(directory, repository);
moduleList(repository).forEach(function(module) {
print('\t' + module.name);
writeModuleDoc(directory, repository, module);
});
print('Finished writing to ' + directory);
return;
}
/**
* Copy static files of this webapp to target directory
*
* @param {String} directory
*/
function copyStaticFiles(directory) {
fs.makeTree(fs.join(directory, 'static'));
fs.copyTree(fs.join(module.directory, 'static'), fs.join(directory, 'static'));
}
/**
* Write the html file listing all modules to directory.
*
* @param {String} directory directory of html file to be written
* @param {Object} repository repository descriptor
*/
function writeModuleList(directory, repository) {
var context = objects.merge(defaultContext, {
repositoryName: repository.name,
title: 'Module Overview - ' + repository.name,
modules: moduleList(repository, true),
rootPath: './'
});
var moduleIndexHtml = (templates.getTemplate('moduleIndex.html')).render(context);
fs.write(fs.join(directory, 'index.html'), moduleIndexHtml);
}
/**
* Write html page documenting one module to the directory.
*
* @param {String} directory
* @param {Object} repository repository descriptor
* @param {Object} module module descriptor
*/
function writeModuleDoc(directory, repository, module){
var moduleDirectory = directory;
var modules = [];
moduleDirectory = fs.join(directory, module.id);
fs.makeTree(moduleDirectory);
modules = moduleList(repository);
var slashCount = strings.count(module.id, '/');
var relativeRoot = '../' + strings.repeat('../', slashCount);
function toLink(target) {
// if link target roughly matches "foo/bar#xxx.yyy"
// format as API reference link
if (target.match(/^[\w\/\.#]+$/)) {
var [module, hash] = target.split("#");
if (!module) {
return [target, target.slice(1)];
} else {
var href = relativeRoot + module + "/" + defaultContext.indexhtml;
if (hash) href += "#" + hash;
return [href, target.replace("#", ".")];
}
}
return null;
}
var docs = moduleDoc(repository.path, module, toLink);
if (docs == null) {
throw new Error('Could not parse JsDoc for ' + repository.path + module.id);
}
var context = objects.merge(defaultContext, {
rootPath: relativeRoot,
repositoryName: repository.name,
title: module.name + ' - ' + repository.name,
moduleId: module.id,
modules: modules,
item: structureModuleDoc(docs),
iterate: function(value) {
return value && value.length ? {each: value} : null;
},
debug: function(value) {
print(value.toSource());
return null;
},
commaList: function(value) {
return value && value.length ? value.join(', ') : '';
}
});
var moduleHtml = (templates.getTemplate('module.html')).render(context);
var moduleFile = fs.join(moduleDirectory, 'index.html');
fs.write(moduleFile, moduleHtml);
}
function writeRepositoryIndex(directory, repository) {
var modules = moduleList(repository).map(function(module) {
module.data = structureModuleDoc(moduleDoc(repository.path, module));
module.moduleName = module.name;
return module;
});
var context = objects.merge(defaultContext, {
rootPath: './',
repositoryName: repository.name,
title: 'Index: ' + repository.name,
modules: modules
});
var indexHtml = (templates.getTemplate('repositoryIndex.html')).render(context);
var indexFile = fs.join(directory, 'index_all.html');
fs.write(indexFile, indexHtml);
}
/**
* Create static documentation for a Repository.
*
* ringo-doc -s /home/foo/ringojs/modules/
*
* You can specify a human readable name for the module which will
* be display in the documentation:
*
* ringo-doc -s /home/foo/ringojs/modules -n "Ringojs Modules"
*
* @param args
*/
function main(args) {
/**
* Print script help
*/
function help() {
print('Create JsDoc documentation for CommonJs modules.');
print('Usage:');
print(' ringo ' + script + ' -s [sourcepath]');
print('Options:');
print(parser.help());
}
var script = args.shift();
var parser = new Parser();
parser.addOption('s', 'source', 'repository', 'Path to repository');
parser.addOption('d', 'directory', 'directory', 'Directory for output files (default: "out")');
parser.addOption('t', 'templates', 'directory', 'Template directory');
parser.addOption('n', 'name', 'name', 'Name of the Repository (default: auto generated from path)');
parser.addOption('p', 'package', 'package.json', 'Use package manifest to adjust module names.')
parser.addOption(null, 'file-urls', null, 'Add "index.html" to all URLs for file:// serving.');
parser.addOption('h', 'help', null, 'Print help message and exit');
var opts = parser.parse(args);
if (opts.help) {
help();
return;
}
if (!opts.source) {
throw new Error('No source specified.');
}
if (opts.templates) {
templates = new Reinhardt({
loader: opts.templates,
filters: require('./jsdocFilters')
});
}
if (opts.package) {
// read package.json manifest
var packageJson = fs.absolute(opts.package);
var pkg = opts.package = require(packageJson);
if (pkg.main) {
// make main module absolute
pkg.main = fs.absolute(fs.join(fs.directory(packageJson), pkg.main))
}
}
var directory = fs.join(opts.directory || './out/');
var repository = {
path: opts.source,
name: opts.name || getRepositoryName(opts.source),
package: opts.package || {}
};
defaultContext.indexhtml = opts['fileUrls'] ? 'index.html' : '';
// check if export dir exists & is empty
var dest = new fs.Path(directory);
if (dest.exists() && !dest.isDirectory()) {
throw new Error(dest + ' exists but is not a directory.');
} else if (dest.isDirectory() && dest.list().length > 0) {
throw new Error('Directory ' + dest + ' exists but is not empty');
}
if (!fs.isDirectory(repository.path)) {
throw new Error('Invalid source specified. Must be directory.');
}
renderRepository(repository, directory, false);
}
if (require.main == module) {
try {
main(system.args);
} catch (error) {
print(error);
print('Use -h or --help to get a list of available options.');
}
}