-
Notifications
You must be signed in to change notification settings - Fork 8
/
makeToC.groovy
70 lines (62 loc) · 2.65 KB
/
makeToC.groovy
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
// Copyright (c) 2019-2022 Egon Willighagen <[email protected]>
//
// GPL v3
// find all topics and makes a data file to be converted into an Index
//
// it takes one optional argument, which is appended to the output
import groovy.xml.XmlSlurper
def chapterCounter = 0
lang = args[0]
langFolder = ""
if (lang != "en") langFolder = "${lang}/"
refSectionLabels = [
"en": "References",
"es": "Referencias",
"nl": "Bronnen",
"ja": "References",
"pt": "Referências"
]
chapters = "order.txt"
new File(chapters).eachLine { chapter ->
if (chapter.startsWith("app")) return;
chapterCounter++
file = "src/${langFolder}/${chapter}.md"
if (!new File(file).exists()) return;
sectionCounter = 0
subsectionCounter = 0
new File(file).eachLine { line ->
if (line.startsWith("# ")) {
chapterTitle = line.substring(2).trim()
println "${chapterCounter}. [${chapterTitle}](${chapter}.md) <br />"
} else if (line.startsWith("## ") && !line.contains(refSectionLabels[lang])) {
subsectionCounter = 0
sectionTitle = line.substring(3).trim()
sectionCounter++
sectionHref = sectionTitle.toLowerCase().replaceAll(" ", "-").replaceAll("\\.", "")
println "${chapterCounter}.${sectionCounter}. [${sectionTitle}](${chapter}.i.md#${sectionHref}) <br />"
} else if (line.startsWith("### ")) {
subsectionCounter++
sectionTitle = line.substring(4).trim()
sectionHref = sectionTitle.toLowerCase().replaceAll(" ", "-").replaceAll("\\.", "")
println "${chapterCounter}.${sectionCounter}.${subsectionCounter}. [${sectionTitle}](${chapter}.i.md#${sectionHref}) <br />"
} else if (line.startsWith("<section")) {
def instruction = new XmlSlurper().parseText(line)
if (instruction.@level == "##") {
subsectionCounter = 0
sectionTitle = instruction.text()
sectionCounter++
sectionHref = sectionTitle.toLowerCase().replaceAll(" ", "-").replaceAll("\\.", "")
println "${chapterCounter}.${sectionCounter}. [${sectionTitle}](${chapter}.i.md#${sectionHref}) <br />"
} else if (instruction.@level == "###") {
subsectionCounter++
sectionTitle = instruction.text()
sectionHref = sectionTitle.toLowerCase().replaceAll(" ", "-").replaceAll("\\.", "")
println "${chapterCounter}.${sectionCounter}.${subsectionCounter}. [${sectionTitle}](${chapter}.i.md#${sectionHref}) <br />"
} else if (instruction.@level == "#") {
sectionTitle = instruction.text()
sectionHref = sectionTitle.toLowerCase().replaceAll(" ", "-").replaceAll("\\.", "")
println "${chapterCounter}. [${sectionTitle}](${chapter}.md) <br />"
}
}
}
}