-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdocmaker.js
436 lines (327 loc) · 11.2 KB
/
docmaker.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* eslint-disable no-console, no-useless-escape */
let fs = require('fs');
let marked = require('marked');
let Handlebars = require('handlebars');
let jsonlint = require('jsonlint');
let hljs = require('highlight.js');
let encoding = 'utf8';
let config, versions;
let configFile = './docmaker.json';
let mdRend = new marked.Renderer();
let path = require('path');
let versionFile = './versions.json';
let rendCode = mdRend.code;
mdRend.code = function(code, lang){
let button = '';
if( lang === 'js' ){
button = '<button class="run run-inline-code"><span class="fa fa-play"></span></button>';
}
return rendCode.call(this, code, lang) + button;
};
try {
jsonlint.parse( fs.readFileSync( path.join(__dirname, configFile), 'utf8') ); // validate first for convenience
config = require( configFile );
jsonlint.parse( fs.readFileSync( path.join(__dirname, versionFile), 'utf8') ); // validate first for convenience
versions = require( versionFile );
} catch(e){
console.error('\n`' + configFile + '` could not be read; check the JSON is formatted correctly via jsonlint');
throw e;
}
config.version = process.env.VERSION || 'snapshot';
function linkifyArg( arg ){
let link = config.fnArgLinks[ arg.name ];
if( link ){
arg.linkedName = '<a href="'+ link +'">' + arg.name + '</a>';
} else {
arg.linkedName = arg.name;
}
}
// let html = converter.makeHtml("**I am bold!**");
// let html = Handlebars.compile();
function md2html( file ){
file = file.substr( file.length - 3 ) === '.md' ? file : file + '.md'; // add extension if need be
let md;
try{
md = fs.readFileSync( path.join(__dirname, './md', file) , 'utf8');
} catch(e){
throw 'A markdown file named `' + file + '` was referenced but could not be read';
}
// let html = converter.makeHtml( md );
//let html = mdConvertor( md );
marked.setOptions({
highlight: function(code, lang){
let ret;
if( lang ){
ret = hljs.highlight(lang, code).value;
} else {
ret = hljs.highlightAuto(code).value;
}
return ret;
},
smartypants: true,
renderer: mdRend,
gfm: true
} );
let html = marked.parse( md );
return html;
}
function templateToHtml(context) {
if (context.mdTemplate === "intro") {
generate_versions(context);
}
let introHtmlTemplate = md2html(context.mdTemplate);
let introTemplate = Handlebars.compile(introHtmlTemplate);
let infoHtml = introTemplate(context);
let html = marked.parse(infoHtml);
return html;
}
function toUrl( str ){
str = str || '';
str = str.replace(/ /g, '-');
str = str.replace(/\&|\,|\;|\(|\)/g, '');
str = str.toLowerCase();
return str;
}
function makeBookmark( id ){
return '<a href="#'+ id +'"><span class="fa fa-bookmark"></span></a>';
}
function parseSubsections( section ){
let parentName = section.name;
let html = section.html;
let matches = html.match(/\<h2.*?\>.+?\<\/h2\>/g);
let psubs = [];
for( let i = 0; matches && i < matches.length; i++ ){
let match = matches[i];
let name = match.match(/\<h2.*?\>(.+)\<\/h2\>/)[1];
let id = toUrl(parentName) + '/' + toUrl(name);
psubs.push({
name: name,
fromMd: true,
id: id,
bookmark: makeBookmark(id)
});
section.html = section.html.replace(match, '<h2 id="' + id + '">' + name + ' ' + makeBookmark(id) + '</h2>');
}
return psubs;
}
function populateDemo( demo ){
if( demo.github ){
demo.githubUrl = 'https://github.com/' + demo.github;
if( !demo.viewUrl ){ // use github pages url if unspecified
let gh = demo.github.match(/([^/]+)\/([^/]+)/);
demo.viewUrl = 'https://' + gh[1] + '.github.io/' + gh[2];
}
} else { // main repo demo
demo.githubUrl = 'https://github.com/cytoscape/cytoscape.js/tree/master/documentation/demos/' + demo.id;
demo.viewUrl = 'demos/' + demo.id;
}
demo.imgUrl = 'img/demos/' + demo.id + '.png';
}
function processFields( fields ){
for( let i = 0; fields && i < fields.length; i++ ){
let field = fields[i];
field.descr = marked.parse( field.descr || '' );
linkifyArg( field );
let subfields = field.fields;
if( subfields ){
processFields( subfields );
}
}
}
function compileAliases( section, fn ){
if( fn.pureAliases ){
let procdAliases = [];
for( let k = 0; k < fn.pureAliases.length; k++ ){
let pa = '' + fn.pureAliases[k];
procdAliases.push({
name: pa,
id: section.id + '/' + pa
});
}
fn.processedPureAliases = procdAliases;
}
}
function compileConfig( config ){
let sections = config.sections;
let parent = config;
for( let i = 0; sections && i < sections.length; i++ ){
let section = sections[i];
if (section.mdTemplate) {
section.html = templateToHtml(section);
let psubs = parseSubsections( section );
let subs = section.sections = section.sections || [];
section.sections = subs.concat( psubs );
}
if( section.layout ){ section.name = section.layout.name; }
section.id = (parent.name ? (toUrl(parent.name) + '/') : '') + toUrl( section.name );
section.bookmark = makeBookmark( section.id );
if( section.md ){
section.html = md2html( section.md );
let psubs = parseSubsections( section );
let subs = section.sections = section.sections || [];
section.sections = subs.concat( psubs );
}
if( section.mddescr ){
section.descr = md2html( section.mddescr );
}
if( section.demos ){
let demos = section.demos;
for( let j = 0; j < demos.length; j++ ){
let demo = demos[j];
populateDemo( demo );
}
}
if( section.demo ){
populateDemo( section.demo );
}
if( section.layout ){
let layout = section.layout;
section.name = layout.name;
layout.code = fs.readFileSync( path.join(__dirname, '../src/extensions/layout/' + layout.name + '.js'), 'utf8' );
try {
layout.options = layout.code.match(/defaults\s*\=\s*(\{(?:.|\s)+?\}\;)/)[1];
let lopts = layout.options;
// cleanup indent
lopts = lopts.replace(/\n[ ]{4}/g, '\n ');
lopts = lopts.replace(/[ ]{2}\}\;/g, '};');
// add name
lopts = lopts.replace(/\{/, '{\n name: \'' + layout.name + '\',\n');
// wrap w/ code
lopts = 'let options = ' + lopts + '\n\ncy.layout( options );';
// highlight
lopts = hljs.highlight('js', lopts).value;
layout.optionsFormatted = lopts;
} catch(e){
throw 'Error processing layout options for `'+ layout.name +'`; must have `defaults = { ... };`';
}
}
if( section.fns ){
let fns = section.fns;
for( let j = 0; j < fns.length; j++ ){
let fn = fns[j];
fn.altIds = [];
fn.altIds.push( section.id + '/' + fn.name );
fn.id = fn.name;
fn.bookmark = makeBookmark( fn.id );
fn.descr = fn.descr ? marked.parse( fn.descr ) : undefined;
if( fn.md ){
fn.html = md2html( fn.md );
// the html for functions should only have h3 tags, not h1 or h2
fn.html = fn.html.replace(/\<h2/g, '<h3');
fn.html = fn.html.replace(/\<\/h2\>/g, '</h3>');
fn.html = fn.html.replace(/\<h1/g, '<h3');
fn.html = fn.html.replace(/\<\/h1\>/g, '</h3>');
}
if( fn.pureAliases ){
fn.pureAliases.forEach(function( aId ){
fn.altIds.push( section.id + '/' + aId );
fn.altIds.push( aId );
});
}
let formatsHaveDiffNames = false;
if( fn.formats ){
let formats = fn.formats;
for( let k = 0; k < formats.length; k++ ){
let format = formats[k];
format.name = format.name || fn.name; // copy name to format if not specified
format.descr = marked.parse( format.descr || '' );
fn.altIds.push( section.id + '/' + format.name );
fn.altIds.push( format.name );
if( format.args ){
for( let m = 0; m < format.args.length; m++ ){
let arg = format.args[m];
linkifyArg( arg );
arg.descr = marked.parse( arg.descr || '' );
processFields( arg.fields );
}
}
if( format.name !== fn.name ){
formatsHaveDiffNames = true;
}
compileAliases( section, format );
}
} // if
// mark as diff names
if( formatsHaveDiffNames ){
fn.aliases = true;
}
compileAliases( section, fn );
} // for
// sort functions by name within a section
// fns.sort(function(a, b){
// return a.name.toLowerCase() > b.name.toLowerCase();
// });
}
if( section.sections ){ // then compile those subsections too
compileConfig( section );
}
}
}
function sortSoftwareVersions(versions, type) {
return versions.sort((a, b) => {
var aParts, bParts;
if (type === 'major') {
aParts = a.version.split('.');
bParts = b.version.split('.');
}
else {
aParts = a.split('.');
bParts = b.split('.');
}
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
const aNum = Number(aParts[i]) || 0;
const bNum = Number(bParts[i]) || 0;
if (aNum > bNum) return -1;
else if (aNum < bNum) return 1;
}
return 0;
});
}
function getMilestoneLink(minor_ver) {
return "https://github.com/cytoscape/cytoscape.js/issues?q=milestone%3A".concat(minor_ver).concat("+is%3Aclosed");
}
function getVersionMap(all_versions) {
const version_map = new Map();
const breakpoint = /(\d+.\d+)/;
all_versions.forEach((e) => {
v = e.split(breakpoint).filter(Boolean);
if (version_map.has(v[0])) version_map.get(v[0]).push(v[1]);
else {
version_map.set(v[0], [v[1]]);
}
});
return version_map;
}
function generate_versions(context) {
let all_versions = versions.versions;
unique_versions = [...new Set(all_versions)]
const version_map = getVersionMap(unique_versions);
const data = {
major_release: []
};
for (const major_version of version_map.entries()) {
const temp = {"version" : major_version[0]};
temp["minor_release"] = [];
const sorted = sortSoftwareVersions(major_version[1], "minor");
for (let i = 0, len = sorted.length; i < len; i++) {
// sorted[i] represent the minor version and its object contains the version and the link
const temp_minor = {};
temp_minor["minor_ver"] = major_version[0].concat(sorted[i]);
temp_minor["link"] = getMilestoneLink(temp_minor["minor_ver"]);
temp["minor_release"].push(temp_minor);
}
// console.log(temp);
data.major_release.push(temp);
};
let sortedRelease = sortSoftwareVersions(data.major_release, "major");
context["major_release"] = sortedRelease;
}
function writeDocs(){
let context = config;
compileConfig( config );
let htmlTemplate = fs.readFileSync( path.join(__dirname, './template.html'), encoding);
let template = Handlebars.compile( htmlTemplate );
let html = template( context );
fs.writeFileSync( path.join(__dirname, 'index.html'), html, encoding);
}
writeDocs();