-
Notifications
You must be signed in to change notification settings - Fork 18
/
mdn.js
108 lines (87 loc) · 3.95 KB
/
mdn.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
/*=====================================================================*/
/* serrano/prgm/project/hop/3.5.x/doc/mdn.js */
/* ------------------------------------------------------------- */
/* Author : Manuel Serrano */
/* Creation : Tue Oct 4 17:59:11 2016 */
/* Last change : Thu Jan 6 16:56:06 2022 (serrano) */
/* Copyright : 2016-22 Manuel Serrano */
/* ------------------------------------------------------------- */
/* Generate the MDN index */
/*=====================================================================*/
"use hopscript";
const mdn = "https://developer.mozilla.org"
const globalObject = "/en-US/docs/Web/JavaScript/Reference/Global_Objects";
const api = "/docs/Web/API/";
/*---------------------------------------------------------------------*/
/* stdlib */
/*---------------------------------------------------------------------*/
const stdlib = [ "Array", "Boolean", "Date", "Error", "Function",
"JSON", "Math", "Number", "Promise", "Regexp", "String" ];
/*---------------------------------------------------------------------*/
/* getChapterBindings ... */
/*---------------------------------------------------------------------*/
function getChapterBindings( chapter, _i, _arr ) {
function findDL( node ) {
if( !node ) {
return false;
} else {
for( ; node; node = node.nextSibling ) {
if( typeof node == "xml-element" ) {
switch( node.tagName ) {
case "dl": return node;
case "div": return findDL( node.childNodes[ 0 ] );
}
}
}
}
}
function getEntry( entry ) {
const nodes = entry.childNodes;
const a = nodes.find( (n, _i, __arr) => n.tagName == "a" );
if( !a ) return false;
const c = a.childNodes.find( (n, _i, __arr) => n.tagName == "code" );
if( !c ) return false;
let proto = c.innerHTML;
const i = proto.lastIndexOf( "." );
const k = i >= 0 ? proto.substring( i + 1 ) : proto;
if( k == "prototoype" ) return false;
const j = k.indexOf( "(" );
return {
key: j >= 0 ? k.substring( 0, j ) : k,
proto: proto,
chapter: "mdn",
type: proto.indexOf( "(" ) >= 0 ? "function" : "parameter",
url: a.href
};
}
function getSectionProtos( nodes ) {
let arr = [];
for( let i = 0; i < nodes.length; i++ ) {
if( typeof nodes[ i ] == "xml-element" ) {
if( nodes[ i ].tagName == "dt" ) {
const el = getEntry( nodes[ i ] );
if( el ) arr.push( el );
}
}
}
return arr;
}
const url = mdn + globalObject + "/" + chapter;
const html = require( url, "html" );
// get the properties
const properties = findDL( html.getElementById( "Properties" ) );
const properties2 = findDL( html.getElementById( "Properties_2" ) );
const methods = findDL( html.getElementById( "Methods" ) );
const methods2 = findDL( html.getElementById( "Methods_2" ) );
const genmethods = findDL( html.getElementById( chapter + "_generic_methods" ) );
return (properties ? getSectionProtos( properties.childNodes ): [] )
.concat( properties2 ? getSectionProtos( properties2.childNodes ): [] )
.concat( methods ? getSectionProtos( methods.childNodes ) : [] )
.concat( methods2 ? getSectionProtos( methods2.childNodes ) : [] )
.concat( genmethods ? getSectionProtos( genmethods.childNodes ) : [] );
}
/*---------------------------------------------------------------------*/
/* Index generator */
/*---------------------------------------------------------------------*/
const arrs = stdlib.map( getChapterBindings );
console.log( JSON.stringify( [].concat.apply( [], arrs ) ) );