-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.js
149 lines (137 loc) · 3.76 KB
/
build.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
const fs = require('fs')
const path = require('path')
let data = require('./docA.json')
let docs = []
let section
function parseList(list, level = 0) {
let s = ''
for (let item of list.children) {
if (item.tag == 'ul') {
s += parseList(item, level + 1)
} else {
for (let itemChild of item.children) {
if (itemChild.tag == 'p') s += '\t'.repeat(level) + '- ' + itemChild.text + ' \n'
if (itemChild.tag == 'ul') s += parseList(itemChild, level + 1)
}
}
}
return s
}
const TableHeaders = [
"_**Combat ranks**_",
"_**Trade ranks**_",
"_**Exploration ranks**_",
"_**Federation ranks**_",
"_**Empire ranks**_",
"_**CQC ranks**_",
]
const ListHeaders = [
"Planet Classes",
"Atmosphere Classes",
"Volcanism classes",
"Crime types",
"BodyType values",
"Gases in AtmosphereComposition",
"Star Luminosity classes",
]
const ListHeaders2 = [
"WeaponMode should be one of",
"DamageType should be one of",
"CabinClass should be one of",
]
let currentHeader
for (let element of data.children) {
if (docs.length == 0 && element.tag != 'h1') continue
if (element.tag == 'h1') {
section = {
heading: element.text.replace(/\d+\s*/, ''),
paragraphs: []
}
docs.push(section)
}
if (element.tag == 'p') {
let text = element.text.trim()
if (!text) continue
if (text.startsWith('Example: {') && text.endsWith('}')) {
text = text.replace(/^Example: /, '')
section.paragraphs.push({text: "Example:"})
}
if (text.startsWith('{') && text.endsWith('}')) {
let code = text
try {
code = JSON.stringify(JSON.parse(text), null, '\t')
} catch (e) { }
text = '```\n' + code + '\n```'
}
if (TableHeaders.some(e => text.startsWith(e))) {
text = text.replace(/^[^\:]+:/, "$&\n\nIndex|Rank\n-:|\n")
text = text.replace(/, */g, "\n")
text = text.replace(/= */g, "|")
section.paragraphs.push({
text: text,
})
} else if (currentHeader == 'Engineer IDs') {
text = text.replace(/\d+/g, "\n$&|")
text = "ID|Name\n-:|" + text
section.paragraphs.push({
text: text,
})
} else if (ListHeaders.includes(currentHeader)) {
let paragraph = {
text: '- ' + text.replace(/,\s*$/, ''),
}
section.paragraphs.push(paragraph)
} else if (ListHeaders2.some(e => text.startsWith(e))) {
text = text.replace(/^([^\:]+:)\s*/, "$1\n\n- ")
text = text.replace(/, */g, "\n- ")
section.paragraphs.push({
text: text,
})
} else {
let paragraph = {
text: text,
}
section.paragraphs.push(paragraph)
}
}
if (element.tag == 'h2') {
let paragraph = {
text: '### ' + element.text.replace(/[\d\.]+\s*/, ''),
}
section.paragraphs.push(paragraph)
currentHeader = paragraph.text.substring(4)
}
if (element.tag == 'ul') {
let text = parseList(element)
let paragraph = {
text: text,
}
section.paragraphs.push(paragraph)
}
if (element.tag == 'div') {
let text = element.children.reduce((acc, cur) => acc + '- ' + cur.text.replace(/,/, '') + ' \n', '')
let paragraph = {
text: text,
}
section.paragraphs.push(paragraph)
}
}
for (let section of docs) {
let title = section.heading
if (title == 'Index') continue
let content = '## ' + title
content += '\n\n'
for (let paragraph of section.paragraphs) {
// for (let text of paragraph.text) {
// if (typeof text.t[0] != 'string') console.log(text.t)
// }
// paragraph.text = paragraph.text.map(e => {
// let t = e.t[0].text
// return t
// })
// paragraph.text = paragraph.text.map(e => e)
}
content += section.paragraphs.map(e => e.text).join('\n\n')
if (title == 'Introduction') title = 'index'
fs.writeFileSync(path.resolve('docs', `${title.replace(/"/g, '')}.md`), content)
}