-
Notifications
You must be signed in to change notification settings - Fork 8
/
createMarkdown.groovy
189 lines (178 loc) · 6.85 KB
/
createMarkdown.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
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
// Copyright (c) 2018-2020 Egon Willighagen <[email protected]>
//
// 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])
}
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("<sparql>")) {
def instruction = new XmlSlurper().parseText(line)
def srcLines = new File("sparql/${instruction.text()}.verbatim.${lang}.md").readLines()
srcLines.each { String srcLine -> println srcLine }
} else if (line.startsWith("<out")) {
def instruction = new XmlSlurper().parseText(line)
def srcLines = new File("sparql/${instruction.text()}.${lang}.out").readLines()
def recordsProcessed = 0
def recordsToOutput = 10000 // okay, so if the output has more, it will truncate them nevertheless #TODO
if ([email protected]())
recordsToOutput = Integer.parseInt([email protected]())
srcLines.each { String srcLine ->
if (srcLine.contains("<tr")) {
if (recordsProcessed == recordsToOutput) {
def message = "<a href=\"sparql/${instruction.text()}.code.html\">sparql/${instruction.text()}.rq</a>"
println " <tr><td colspan=\"2\">${labels["truncatedTable"][lang]} ${message}</td></tr>"
println "</table>"
}
recordsProcessed += 1
}
if (recordsProcessed < recordsToOutput) {
println srcLine
}
}
} 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("<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
}
}