-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmkdoc.js
114 lines (95 loc) · 3.29 KB
/
mkdoc.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
'use strict';
{
console.log('Generating docs...');
const fs = require('fs'),
nd = require('./dist/nd.js'),
{version}= require('./package.json');
fs.readFile('./runkit_example.js', 'utf8', (err, data) => {
if(err) throw err;
data = data.split('\n')
if( ! data[0].startsWith("const nd = require('nd4js") )
throw new Error('1st line of runkit_example.js must be nd4js import.');
data[0] = `const nd = require('nd4js@${version}') // <- 1st line is auto-generated`
fs.writeFile('./runkit_example.js', data.join('\n'), err => {if(err) throw err});
});
function create_doc_jsdom()
{
const
jsdom= require('jsdom'),
dom = new jsdom.JSDOM('<!DOCTYPE html>'),
doc = dom.window.document,
meta = doc.createElement('meta'),
title= doc.createElement('title'),
ul = doc.createElement('ul'),
h1 = doc.createElement('h1');
h1 .innerHTML = `ND4JS v${version} Documentation`
title.innerHTML = `ND4JS v${version} Documentation`
meta.setAttribute('charset', 'utf-8')
doc.lang = 'en'
doc.head.appendChild(meta)
doc.head.appendChild(title)
doc.body.style = 'font-family:monospace'
doc.body.appendChild(h1)
doc.body.appendChild(ul)
const ND = {...nd};
delete ND.Array
function addSection( key, val )
{
const
h2 = doc.createElement('h2'),
pre= doc.createElement('pre'),
li = doc.createElement('li'),
a = doc.createElement('a');
h2.id = key.replace(' ','_').replace('[','').replace(']','')
a.innerHTML = key
a.setAttribute('href', '#'+h2.id)
ul.appendChild(li)
li.appendChild(a)
h2 .innerHTML = key
pre.innerHTML = nd.help_str(val)
doc.body.appendChild(h2)
doc.body.appendChild(pre)
}
for( const [key,val] of Object.entries(ND) )
addSection('nd.'+key,val)
for( const [key,val] of Object.entries(ND.la) )
if( key != '__doc__' )
addSection('nd.la.'+key,val)
for( const [key,val] of Object.entries(ND.opt) )
if( key != '__doc__' )
addSection('nd.opt.'+key,val)
for( const [key,val] of Object.entries(ND.opt.line_search) )
if( key != '__doc__' )
addSection('nd.opt.line_search.'+key,val)
for( const [key,val] of Object.entries(ND.io) )
if( key != '__doc__' )
addSection('nd.io.'+key,val)
for( const [key,val] of Object.entries(ND.dt) )
if( key != '__doc__' )
addSection('nd.dt.'+key,val)
const classes = [
'nd.NDArray',
'nd.dt.Complex'
]
// ADD CLASS DOCS
for( const name of classes )
{
const clazz = name.split('.').slice(1).reduce((obj,name) => obj[name], nd)
for( const key of Object.getOwnPropertyNames(clazz) )
if( ! key.startsWith('__') )
try {
addSection(name+'.'+key+' [STATIC]', clazz[key] )
}
catch(err) {}
for( const key of Object.getOwnPropertyNames(clazz.prototype) )
if( ! key.startsWith('__') )
try {
addSection(name+'.'+key, clazz.prototype[key] )
}
catch(err) {}
}
return dom
}
const html = create_doc_jsdom();
fs.writeFile('./docs/doc.html', html.serialize(), err => { if(err) return console.log(err) });
}