-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathClassDefinition.js
More file actions
90 lines (80 loc) · 2.13 KB
/
ClassDefinition.js
File metadata and controls
90 lines (80 loc) · 2.13 KB
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
import { parse } from "url"
import toMarkdown from "to-markdown"
import makeFetchHappen from "make-fetch-happen"
import cheerio from "cheerio"
const fetch = makeFetchHappen.defaults({
cacheManager: ".cache/", // path where cache will be written (and read)
})
const KlassName = process.argv[2]
fetch(
"https://developers.google.com/maps/documentation/javascript/3.exp/reference"
)
.then(it => it.text())
.then(it => cheerio.load(it))
.then($ => {
const $content = $(`#${KlassName}`).parent()
return contentToJS(KlassName, $, $content)
})
.then(it => process.stdout.write(JSON.stringify(it)))
.catch(error => {
console.error(error)
process.exit(1)
})
function contentToJS(KlassName, $, $content) {
const constructor = $content
.find(`#${KlassName}`)
.next()
.find("code")
.text()
const $constructorTable = $content.find(
`[summary="class ${KlassName} - Constructor"]`
)
const constructorArgs = $constructorTable
.find(`tr > td > code`)
.text()
.match(/\S+\((.*)\)/)
const $methodsTable = $content.find(
`[summary="class ${KlassName} - Methods"]`
)
const methods = $methodsTable
.find("tbody > tr")
.map((i, tr) => {
const $tr = $(tr)
const [, name, args] = $tr
.find("td:first-child")
.text()
.replace("\n", "")
.match(/(\S+)\((.*)\)/)
const returnsDesc = toMarkdown(
$tr.find("td:nth-child(2) > div.desc").html()
)
return {
name,
args,
returns: $tr.find("td:nth-child(2) > div > code").text(),
returnsDesc,
}
})
.get()
const $eventsTable = $content.find(`[summary="class ${KlassName} - Events"]`)
const events = $eventsTable
.find("tbody > tr")
.map((i, tr) => {
const $tr = $(tr)
const name = $tr.find("td:first-child").text()
return {
name,
args: $tr.find("td:nth-child(2) > div > code").text(),
returnsDesc: $tr.find("td:nth-child(2) > div.desc").text(),
}
})
.get()
return {
constructor: {
name: constructor,
args: constructorArgs,
},
methods,
events,
}
}