-
Notifications
You must be signed in to change notification settings - Fork 0
/
includeLearningObjectives.gradle
93 lines (71 loc) · 2.85 KB
/
includeLearningObjectives.gradle
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
import static groovy.io.FileType.FILES
class LearningObjective {
String id
String title
int chapter
int chapterIndex
LearningObjective(String id, String title, String chapter, String chapterIndex) {
this.id = id
this.title = title
this.chapter = Integer.valueOf(chapter)
this.chapterIndex = Integer.valueOf(chapterIndex)
}
}
tasks.register('includeLearningObjectives') {
doLast {
def contentDE = createLearningObjectivesContent("DE", ~/\[\[(LZ-\d*-\d*)\]\]\s*==== (LZ (\d*)-(\d*).*)/, "Verzeichnis der Lernziele")
def contentEN = createLearningObjectivesContent("EN", ~/\[\[(LG-\d*-\d*)\]\]\s*==== (LG (\d*)-(\d*).*)/, "List of Learning Goals")
writeLearningObjectives(contentDE, contentEN)
}
ext.createLearningObjectivesContent = { language, pattern, headline ->
def learningObjectives = collectLearningObjectives(pattern)
sortLearningObjectives(learningObjectives)
return compileLearningObjectives(language, headline, learningObjectives)
}
ext.collectLearningObjectives = { pattern ->
def docsFolder = new File(workDirectory, '/docs')
def learningObjectives = []
docsFolder.traverse(type: FILES) { file ->
if (file.name ==~ '.*[.](ad|adoc|asciidoc)$') {
def content = file.text
def matcher = content =~ pattern
for (result in matcher.results()) {
def id = result.group(1)
def title = result.group(2)
def chapter = result.group(3)
def chapterIndex = result.group(4)
learningObjectives.add(new LearningObjective(id, title, chapter, chapterIndex))
}
}
}
return learningObjectives
}
}
ext.sortLearningObjectives = { learningObjectives ->
learningObjectives.sort { a, b ->
if (a.chapter == b.chapter) {
a.chapterIndex <=> b.chapterIndex
} else {
a.chapter <=> b.chapter
}
}
}
ext.compileLearningObjectives = { language, headline, learningObjectives ->
def content = '' << ''
content <<= "// tag::" + language + "[]\n"
content <<= "== " + headline + "\n\n"
learningObjectives.each { learningObjective ->
content <<= "- <<" + learningObjective.id + ", " + learningObjective.title + ">>\n"
}
content <<= "// end::" + language + "[]\n"
return content.toString()
}
ext.writeLearningObjectives = { contentDE, contentEN ->
def docsFolder = new File(workDirectory, '/docs')
def outFile = new File(docsFolder, '/learning-objectives.adoc')
outFile.withWriter('UTF-8') { writer ->
writer.writeLine("// this is autogenerated - please do not modify manually!\n")
writer.writeLine(contentDE)
writer.writeLine(contentEN)
}
}