-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty.go
158 lines (153 loc) · 3.41 KB
/
pretty.go
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
package main
import (
"fmt"
"log"
"math"
"os"
"regexp"
"sort"
"strings"
)
// algo:
// search for patterns. if they are not found, we simply print back
// if patterns are available, put all name, and the location.
//
// -- sort by location to get unique patterns
// -- note all unique pattern means the previous line has (x) entry. x can be 0-9,A-Z,a-z
// -- then look for next such unique pattern
// -- repeat and see previous line for (x).
//
// once we have all unique-patterns, and the order, we then tab to print pretty
func prettySection(in string) string {
found := false
const max = math.MaxInt32
exprs := map[string]int{
`:\n\([a-z]-[0-9]\) `: max,
`:\n\([A-Z]\) `: max,
`:\n\([A-Z]-[0-9]\) `: max,
`:\n\([a-z])\) `: max,
`:\n\([0-9]\) `: max,
`:\n\([0-9]-[a-z]\) `: max,
`:\n\([0-9]-[A-Z]\) `: max,
`:\n\([i|x|v]+\) `: max,
}
for exp := range exprs {
b, e := regexp.MatchString(exp, in)
if e == nil {
if b {
smallestLoc := max
r, e := regexp.Compile(exp)
if e != nil {
fmt.Println("ERROR ", e)
continue
}
locations := r.FindIndex([]byte(in))
for _, loc := range locations {
if loc < smallestLoc {
smallestLoc = loc
}
}
exprs[exp] = smallestLoc
found = true
}
}
}
if !found {
fmt.Println(in)
} else {
newExpr := make(map[string]int)
locations := []int{}
for _, l := range exprs {
if l != max {
locations = append(locations, l)
}
}
sort.Slice(locations, func(i, j int) bool {
return locations[i] < locations[j]
})
/*
for _, l := range locations {
fmt.Println("debug:", l)
}
for xpr, vLoc := range exprs {
fmt.Println("debug:", xpr, vLoc)
}
*/
for k, lo := range locations {
for exp, vLoc := range exprs {
if vLoc == lo && exp != "" {
// fmt.Println("debug: ", exp, lo)
if arr := strings.Split(exp, `:\n`); len(arr) > 1 {
ss := arr[1]
newExpr[ss] = k + 1 // k is number of spaces that are needed
} else {
fmt.Println("error: ", arr)
}
}
}
}
/*
for xpr, v := range newExpr {
fmt.Println("found:", xpr, "space :", v)
}
*/
inp := strings.Split(in, "\n")
for _, l := range inp {
printed := false
for xpr, spc := range newExpr {
s := ""
for i := 0; i < spc; i++ {
s += " "
}
r, _ := regexp.Compile(xpr)
if r.Match([]byte(l)) {
fmt.Printf("%v%v\n", s, l)
printed = true
}
}
if !printed {
fmt.Println(l)
}
}
}
return in
}
func main() {
in := os.Getenv("IN")
if in == "" {
in = "a"
}
inf, e := os.ReadFile(in + ".txt")
if e != nil {
log.Fatal("failed to read file")
}
inps := string(inf)
chapters := strings.Split(inps, "SUBCHAPTER")
for _, chapContent := range chapters {
sections := strings.Split(chapContent, "Sec. 1101.")
for _, secCont := range sections {
sectionFull := "Sec. 1101."
// fmt.Printf("--( %v, %v)--\nSec.1101.", kChapNum, kSecNum)
fmt.Print("\n")
sectLines := strings.Split(secCont, "\n")
for _, line := range sectLines {
l := strings.TrimSpace(line)
if _, b := strings.CutPrefix(l, "Added by Acts 2"); b {
continue
}
if _, b := strings.CutPrefix(l, "Acts 2"); b {
continue
}
if _, b := strings.CutPrefix(l, "Amended by:"); b {
continue
}
if l == "" {
continue
}
m := l + "\n"
sectionFull += m
}
prettySection(sectionFull)
} // section complete
}
}