forked from CodeCrafter47/TabOverlayWiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_wiki.kts
executable file
·199 lines (179 loc) · 7.1 KB
/
update_wiki.kts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env kotlin
import java.io.File
var has_error = false
println("Updating ATO wiki")
update_wiki(
File("pages"),
File("images"),
File("AdvancedTabOverlay.wiki"),
false,
"AdvancedTabOverlay",
"icons/",
"advancedtaboverlay.seehidden"
)
println("Updating BTLP wiki")
update_wiki(
File("pages"),
File("images"),
File("BungeeTabListPlus.wiki"),
true,
"BungeeTabListPlus",
"heads/",
"bungeetablistplus.seevanished"
)
if (has_error) {
System.exit(1)
}
fun update_wiki(
pagesSrc: File,
imagesSrc: File,
target: File,
isBTLP: Boolean,
pluginName: String,
iconFolder: String,
seevanishPerm: String
) {
// create file if not exists
if (!target.exists()) {
target.mkdir();
}
val prevFiles = target.listFiles().filter { it.isFile && it.name.endsWith(".md") }.map { it.name }.toMutableList()
val linkTargets =
pagesSrc.listFiles()
.filter { it.isFile && it.name.endsWith(".md") }
.map { it.name.replace(".md", "").replace(" ", "-") }
// copy images
imagesSrc.listFiles()
.filter { it.isFile }
.forEach { it.copyTo(File(File(target, "images"), it.name), true) }
// copy markdown
pagesSrc.listFiles()
.filter { it.isFile && it.name.endsWith(".md") }
.forEach { file ->
var lines = file.readLines()
// check for todo
for ((line, text) in lines.withIndex()) {
if (text.contains("todo")) {
println("TODO: ${file.name} $line")
has_error = true
}
}
// handle [!]: ifATO
var linesNew = mutableListOf<String>()
var restrictedBlock = false
for (text in lines) {
if (!restrictedBlock && text.contains("[!]: ifATO"))
restrictedBlock = true
else if (restrictedBlock && text.contains("[!]: endIF", ignoreCase = true))
restrictedBlock = false
else if (!restrictedBlock || !isBTLP)
linesNew.add(text)
}
lines = linesNew
// handle [!]: ifBTLP
linesNew = mutableListOf<String>()
restrictedBlock = false
for (text in lines) {
if (!restrictedBlock && text.contains("[!]: ifBTLP"))
restrictedBlock = true
else if (restrictedBlock && text.contains("[!]: endIF", ignoreCase = true))
restrictedBlock = false
else if (!restrictedBlock || isBTLP)
linesNew.add(text)
}
lines = linesNew
// handle [!]: ToC
linesNew = mutableListOf<String>()
for ((line, text) in lines.withIndex()) {
if (text.contains("[!]: ToC", ignoreCase = true)) {
for (l in lines.subList(line + 1, lines.size)) {
if (l.contains("##### ")) {
// ignore tier 5,6
} else if (l.contains("#### ")) {
// tier 4
val substring = l.substring(l.indexOf("#### ") + 5)
val label = extractLabel(substring)
linesNew.add(" * [$substring]($label)")
} else if (l.contains("### ")) {
// tier 3
val substring = l.substring(l.indexOf("### ") + 4)
val label = extractLabel(substring)
linesNew.add(" * [$substring]($label)")
}
}
} else {
linesNew.add(text)
}
}
lines = linesNew
// check for issues
for ((line, text) in lines.withIndex()) {
// lines containing [!] indicate an error.
// The [!] is used by template commands and should be removed at this stage.
if (text.contains("[!]")) {
println("TODO: [!] ${file.name} $line")
has_error = true
}
// check targets of local links
"\\[[^]]+\\]\\(([^)]+)\\)".toRegex().findAll(text).forEach { match ->
if ((!linkTargets.contains(match.groupValues[1]) && !match.groupValues[1].startsWith("http") && !match.groupValues[1].startsWith("#")) || match.groupValues[1].contains(
' '
)
) {
println("Unknown link target: ${file.name} ${match.groupValues[1]} $line")
has_error = true
}
}
}
// remove double blank lines
linesNew = mutableListOf<String>()
var blankBefore = false
for (text in lines) {
if (!blankBefore || !text.trim().isEmpty()) {
linesNew.add(text)
}
if (!blankBefore && text.trim().isEmpty()) {
blankBefore = true
} else if (!text.trim().isEmpty()) {
blankBefore = false
}
}
lines = linesNew
// replace !!xxx placeholders
var text = lines.joinToString(separator = "\n")
text = text.replace("!!name", pluginName)
text = text.replace("!!iconFolder", iconFolder)
text = text.replace("!!seevanishedperm", seevanishPerm)
// check if there is any !! left in the file
if (text.contains("!!")) {
println("${file.name} contains !!")
has_error = true
}
File(target, file.name).writeText(text)
prevFiles.remove(file.name)
}
if (!prevFiles.isEmpty()) {
println("Old files: ${prevFiles.joinToString()}")
}
}
fun extractLabel(substring: String): String {
return "#" + substring
.replace("`", "")
.replace("$", "")
.replace("{", "")
.replace("}", "")
.replace("!", "")
.replace(":", "")
.replace("<", "")
.replace(">", "")
.replace("[", "")
.replace("]", "")
.replace("/", "")
.replace(".", "")
.replace(",", "")
.replace("?", "")
.replace("\\(.*\\)".toRegex(), "")
.trim()
.replace(" ", "-")
.toLowerCase()
}