-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateMarkdown.groovy
More file actions
201 lines (189 loc) · 7.1 KB
/
Copy pathcreateMarkdown.groovy
File metadata and controls
201 lines (189 loc) · 7.1 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
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
200
201
// Copyright (c) 2018-2020 Egon Willighagen <egon.willighagen@gmail.com>
//
// GPL v3
import groovy.xml.XmlSlurper
input = args[0]
langs = [ "en", "ja", "nl", "es", "pt" ]
lang = "en"
if (input.substring(4).contains("/"))
lang = input.substring(4,4+input.substring(4).indexOf("/"))
println "<!--- THIS FILE IS AUTOGENERATED. DO NOT EDIT IT. -->\n"
labels = [
"truncatedTable": [
"en": "This table is truncated. See the full table at",
"nl": "Deze tabel is niet compleet. De volledige inhoud is te vinden op",
"ja": "This table is truncated. See the full table at",
"es": "This table is truncated. See the full table at"
],
"missing": [
"en": "Missing",
"nl": "Ontbreekt",
"ja": "Missing",
"es": "Missing"
]
]
bibliography = new HashMap<String,String>();
def bibLines = new File("references.dat").readLines()
bibLines.each { String line ->
splitString = '=1. '; fields = []
fields[0] = line.substring(0,line.indexOf(splitString))
fields[1] = line.substring(line.indexOf(splitString)+splitString.length())
bibliography.put(fields[0], fields[1])
}
chapterCounters = new HashMap<String,String>();
chapterCounter = 0
appendixCounter = 0
def chapterLines = new File("order.txt").readLines()
chapterLines.each { String line ->
if (line.startsWith("app")) {
appendixCounter++
chapterCounters.put(line, (char)(64+appendixCounter))
} else {
chapterCounter++
chapterCounters.put(line, "" + chapterCounter)
}
}
sectionChapters = new HashMap<String,String>();
sectionNumbers = new HashMap<String,String>();
def secdataLines = new File("sections.txt").readLines()
secdataLines.each { String line ->
data = line.split("\t")
label = data[0]
chapter = data[1]
number = data[2]
sectionChapters.put(data[0], data[1])
sectionNumbers.put(data[0], data[2])
}
pkgs = new HashMap<String,String>();
modules = new HashMap<String,String>();
def lines = new File("classinfo.tsv").readLines()
lines.each { String line ->
data = line.split("\t")
topic = data[0]
pkg = data[1]
module = data[2]
pkgs.put(topic, pkg)
modules.put(topic, module)
}
references = new HashMap<String,String>();
bibList = "";
refCounter = 0;
topicCounter = 0;
slashPos = input.lastIndexOf("/")
context = input.substring(slashPos+1, input.indexOf("."))
currentChapterCounter = chapterCounters.get(context)
translations = "[ "
boolean hasTranslations = false
for (l10nLang in langs) {
langFolder = (l10nLang == "en" ? "" : "${l10nLang}/")
if (l10nLang == lang) {
translations += "**${l10nLang}** "
} else if (new File("src/${langFolder}${context}.md").exists()) {
hasTranslations = true
if (lang == "en") {
pageFolder = l10nLang == "en" ? "" : "${l10nLang}/"
} else {
pageFolder = l10nLang == "en" ? "../" : "../${l10nLang}/"
}
translations += "[${l10nLang}](${pageFolder}${context}.md) "
}
}
translations += " ]\n"
if (hasTranslations) println translations
lines = new File(input).readLines()
lines.each { String line ->
if (line.startsWith("<code>")) {
def instruction = new XmlSlurper().parseText(line)
def srcLines = new File("code/${instruction.text()}.verbatim.md").readLines()
srcLines.each { String srcLine -> println srcLine }
} else if (line.startsWith("<out>")) {
def instruction = new XmlSlurper().parseText(line)
println "```plain"
def srcLines = new File("code/${instruction.text()}.out").readLines()
srcLines.each { String srcLine -> println srcLine }
println "```"
} else if (line.startsWith("<iframe>")) {
def instruction = new XmlSlurper().parseText(line)
def srcLines = new File("sparql/${instruction.text()}.iframe.${lang}.md").readLines()
srcLines.each { String srcLine -> println srcLine }
} else if (line.startsWith("<section")) {
def instruction = new XmlSlurper().parseText(line)
println "<a name=\"sec:${instruction.@label}\"></a>"
println "${instruction.@level} ${instruction.text()}"
} else if (line.startsWith("<toc>")) {
def instruction = new XmlSlurper().parseText(line)
def filename = instruction.text()
if (new File(filename.replace(".txt", ".${lang}.txt")).exists())
filename = filename.replace(".txt", ".${lang}.txt")
def srcLines = new File(filename).readLines()
srcLines.each { String srcLine -> println srcLine.replaceAll(".i.md", ".md") }
} else if (line.contains("<references/>")) {
println bibList
} else {
while (line.contains(".i.md")) {
line = line.replace(".i.md", ".md")
}
while (line.contains("<cite>")) {
citeStart = line.indexOf("<cite>")
citeEnd = line.indexOf("</cite>")
cites = line.substring(citeStart+6, citeEnd)
if (cites.isEmpty()) cites = "?"
replacement = ""
if (!references.containsKey(cites)) {
refCounter++
references.put(cites, "" + refCounter)
bibList += "${refCounter}. <a name=\"citeref${refCounter}\"></a>"
if (bibliography.get(cites) != null) {
bibList += bibliography.get(cites) + "\n"
} else {
bibList += "${labels["missing"][lang]}\n"
}
replacement = "<a href=\"#citeref${refCounter}\">${refCounter}</a>"
} else {
existingCounter = Integer.valueOf(references.get(cites))
replacement = "<a href=\"#citeref${existingCounter}\">${existingCounter}</a>"
}
line = line.substring(0, citeStart) + replacement + line.substring(citeEnd+7)
}
while (line.contains("<topic")) {
topicCounter++
topicStart = line.indexOf("<topic")
topicEnd = line.indexOf("</topic>")
topicsXML = line.substring(topicStart, topicEnd+8)
def topicsInstruction = new XmlSlurper().parseText(topicsXML)
replacement = topicsInstruction.text()
replacement = "<a name=\"tp${topicCounter}\">" + replacement + "</a>"
line = line.substring(0, topicStart) + replacement + line.substring(topicEnd+8)
}
while (line.contains("<class")) {
classStart = line.indexOf("<class")
classEnd = line.indexOf("</class>")
classXML = line.substring(classStart, classEnd+8)
def classInstruction = new XmlSlurper().parseText(classXML)
classname = classInstruction.text()
if (pkgs.containsKey(classname)) {
replacement = "[`${classname}`](http://cdk.github.io/cdk/latest/docs/api/" +
pkgs.get(classname).replace(".", "/") + "/${classname}.html)"
} else {
replacement = "`" + classname + "`"
}
line = line.substring(0, classStart) + replacement + line.substring(classEnd+8)
}
while (line.contains("<xref")) {
xrefStart = line.indexOf("<xref")
xrefEnd = line.indexOf("</xref>")
xrefXML = line.substring(xrefStart, xrefEnd+7)
def xrefInstruction = new XmlSlurper().parseText(xrefXML)
xrefname = xrefInstruction.text()
if (sectionChapters.containsKey(xrefname)) {
doc = ""
if (sectionChapters.get(xrefname) != context) doc = sectionChapters.get(xrefname) + ".md"
replacement = "[${sectionNumbers.get(xrefname)}](${doc}#sec:${xrefname})"
} else {
replacement = "??"
}
line = line.substring(0, xrefStart) + replacement + line.substring(xrefEnd+7)
}
println line
}
}