generated from egonw/sparqlmarkdown-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createMarkdown.groovy
145 lines (137 loc) · 5.44 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
// Copyright (c) 2018-2019 Egon Willighagen <[email protected]>
//
// GPL v3
import groovy.xml.XmlSlurper
input = args[0]
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;
context = input.substring(0, input.indexOf("."))
currentChapterCounter = chapterCounters.get(context)
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.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()}.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\">This table is truncated. See the full table at ${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.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 srcLines = new File("${instruction.text()}").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 += "Missing\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
}
}