-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateMarkdown.groovy
67 lines (61 loc) · 2.19 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
// Copyright (c) 2018-2022 Egon Willighagen <egon.willighagen@gmail.com>
//
// 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])
}
references = new HashMap<String,String>();
bibList = "";
refCounter = 0;
context = input.substring(0, input.indexOf("."))
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.contains("<references/>")) {
println bibList
} else if (line.startsWith("%%%")) {
// ignore/remove this line
} 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 {
replacement = Integer.valueOf(references.get(cites))
}
line = line.substring(0, citeStart) + replacement + line.substring(citeEnd+7)
}
println line
}
}