forked from Lombra/elite-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
160 lines (144 loc) · 4.22 KB
/
build.js
File metadata and controls
160 lines (144 loc) · 4.22 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
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
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' && itemChild.text.length > 0)
s += '\t'.repeat(level) + '- ' + itemChild.text.trim() + '\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**_",
"_**Military Ranks**_",
"_**Exobiologist Ranks**_",
]
const ListHeaders = [
"Star Descriptions",
"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
let isListHeader = false
function createTable(header, text) {
let rows = [
header,
'|---:|---|',
]
for (let row of text.split(/ ?,/)) {
let s = row.split(/\s*=\s*/)
rows.push(`|${s[0].trim()}|${s[1].replace(/^'(.+)'$/, '$1')}|`)
}
return rows.join('\n')
}
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 = '```json\n' + code + '\n```'
}
if (TableHeaders.some(e => text.startsWith(e))) {
let matches = text.match(/^([^\:]+:)(.+)/)
text = matches[1]
text += '\n\n'
text += createTable('|Index|Rank|', matches[2])
section.paragraphs.push({ text })
} else if (currentHeader == 'Engineer IDs') {
text = text.replace(/\d+/g, ",$&=").replace(/^,/, '')
text = createTable('|ID|Name|', text)
section.paragraphs.push({ text })
} else if (ListHeaders.includes(currentHeader)) {
let paragraph = {
text: '- ' + text.replace(/,\s*$/, ''), noNewLine: true,
}
isListHeader = true
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 })
} else {
section.paragraphs.push({ text })
}
}
if (element.tag == 'h2') {
if (isListHeader)
section.paragraphs[section.paragraphs.length - 1].noNewLine = false
isListHeader = false
currentHeader = element.text.replace(/[\d\.]+\s*/, '').trim()
let paragraph = {
text: '## ' + currentHeader,
}
section.paragraphs.push(paragraph)
}
if (element.tag == 'ul') {
let text = parseList(element)
section.paragraphs.push({ text: text.trimEnd() })
}
if (element.tag == 'div') {
let text = element.children.reduce((acc, cur) => acc + '- ' + cur.text.replace(/,/, '') + '\n', '')
section.paragraphs.push({ text: text.trimEnd() })
}
}
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 + (e.noNewLine ? '' : '\n')).join('\n')
if (title == 'Introduction') title = 'index'
fs.writeFileSync(path.resolve('docs', `${title.replace(/"/g, '')}.md`), content)
}