-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfindBrokenQuotes.scala
52 lines (47 loc) · 1.21 KB
/
findBrokenQuotes.scala
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
import scala.io.Source
sealed abstract class State
case object Normal extends State
case object InListing extends State
case object InQuotes extends State
var state: State = Normal
var insetDepth = 0
for (file <- args) {
val outputFile = new java.io.FileWriter(file + "-fixed")
for ((line,index) <- Source.fromFile(file).getLines.zipWithIndex) {
//print (file,index,line)
var output = line
state match {
case Normal if line.matches("""(?s)^\\begin_inset listings.*""") => {
//printf("Found listing at %s : %d\n", file, index + 1)
state = InListing
insetDepth = 1
}
case InListing => {
if (line.matches("""(?s)\\begin_inset Quotes e[lr]d.*""")) {
printf("Bad quotes at %s : %d\n", file, index + 1)
output = "\"\n"
state = InQuotes
} else {
if (line.matches("""(?s)\\begin_inset.*""")) {
insetDepth += 1
}
if (line.matches("""(?s)\\end_inset.*""")) {
insetDepth -= 1
}
if (insetDepth == 0) {
state = Normal
}
}
}
case InQuotes if line.matches("""(?s)\\end_inset.*""") => {
output = ""
state = InListing
}
case InQuotes => output = ""
case Normal =>
// ok
}
outputFile.write(output)
}
outputFile.close()
}