-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
405 lines (364 loc) · 13.3 KB
/
main.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"fmt"
"io"
"log"
"os"
"path"
"regexp"
"strings"
"text/template"
// I am using lookaheads in a regex so I need PCRE
// go regexp doesn't support any lookaround
"go.arsenm.dev/pcre"
)
// Chapter defines the regex expressions to search for that denote the start and end of a
// chapter (e.g. Street, Street Touring) of the rulebook.
type Chapter struct {
Name string
Number string
SubChapters []SubChapter
Reader *io.SectionReader
start *regexp.Regexp
end *regexp.Regexp
ChapterFillerText *regexp.Regexp
templateFile string
outputFile string
WeightInformation string
}
// SubChapter holds the name, number, and body of a subchapter of the rules (e.g. 13.2 Bodywork)
type SubChapter struct {
Name string
Number string
Reader *io.SectionReader
}
// getSubChapters returns an array of sub chapters (e.g. 13.1, 13.2) that exist for a given
// chapter
func getSubChapters(rules, chapterNumber string) []SubChapter {
SubChapters := []SubChapter{}
regexString := chapterNumber + `\.([0-9]+[.A-Z]*) ([^\.\n]*)\.+[\. ]([0-9]+)`
tableOfContents := regexp.MustCompile(regexString)
match := tableOfContents.FindAllStringSubmatch(rules, -1)
// SSC does not have its subchapters listed in the table of contents
// so we have to do things differently
if chapterNumber == "20" {
regexString = `(?m)^` + chapterNumber + `\.([0-9]+) (.+)$`
r := regexp.MustCompile(regexString)
match = r.FindAllStringSubmatch(rules, -1)
}
// This means there probably aren't any subchapters
if len(match) < 2 && chapterNumber != "20" {
return SubChapters
}
for i := range match {
SubChapters = append(SubChapters,
SubChapter{
Number: fmt.Sprintf("%s.%s", chapterNumber, match[i][1]),
Name: match[i][2],
},
)
}
return SubChapters
}
// readFile returns a Reader of a specific file
func readFile() *strings.Reader {
filePath := "rules.txt"
rules, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Encountered error reading file", filePath)
os.Exit(1)
}
rulesString := string(rules)
// Unsure why but the pdftotext output contains these characters
// perhaps due to incorrect parsing?
rulesString = strings.ReplaceAll(rulesString, "ff", "ff")
// standardize double quotes
rulesString = strings.ReplaceAll(rulesString, "“", `"`)
rulesString = strings.ReplaceAll(rulesString, "”", `"`)
remove := regexp.MustCompile(`\n\f`)
rulesString = string(remove.ReplaceAll([]byte(rulesString), []byte{}))
return strings.NewReader(rulesString)
}
// findSubChapterBody populates the reader field of each subchapter with the body
// of that subchapter
func findSubChapterBody(chapter Chapter, chapterText []byte) []SubChapter {
SubChapters := chapter.SubChapters
reader := strings.NewReader(string(chapterText))
for i, subChapter := range SubChapters {
reader.Seek(0, 0)
var length int
seekToEnd := false
if i == len(SubChapters)-1 {
seekToEnd = true
}
startRegexString := `(?i)` + regexp.QuoteMeta(subChapter.Number) + ` ` + regexp.QuoteMeta(subChapter.Name)
startRegex := regexp.MustCompile(startRegexString)
startMatch := startRegex.FindReaderIndex(reader)
reader.Seek(0, 0)
if startMatch != nil {
if seekToEnd == true {
length = reader.Len() // jump to end of reader, this is the last element
} else {
endRegexString := `(?i)` + regexp.QuoteMeta(SubChapters[i+1].Number) + ` ` + regexp.QuoteMeta(SubChapters[i+1].Name)
endRegex := regexp.MustCompile(endRegexString)
endMatch := endRegex.FindReaderIndex(reader)
reader.Seek(0, 0)
if endMatch != nil {
length = endMatch[0] - startMatch[0]
}
}
sectionReader := io.NewSectionReader(reader, int64(startMatch[0]), int64(length))
SubChapters[i].Reader = sectionReader
// uncomment to print for troubleshooting
// warning: it will put the section reader in a "Read" state and you'll
// have to seek to the beginning to be able to read from it again
// fmt.Println("here")
// subchapter, err := io.ReadAll(sectionReader)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(subChapter.Number + " " + subChapter.Name)
// fmt.Println(string(subchapter))
// fmt.Printf("%v\n", chapter)
}
}
return SubChapters
}
func getChapterReader(rules *strings.Reader, chapter Chapter) *io.SectionReader {
rules.Seek(0, 0)
startMatch := chapter.start.FindReaderIndex(rules)
rules.Seek(0, 0)
endMatch := chapter.end.FindReaderIndex(rules)
rules.Seek(0, 0)
length := endMatch[0] - startMatch[0]
return io.NewSectionReader(rules, int64(startMatch[0]), int64(length))
}
func ToMenuName(in string) string {
var result string
result = strings.Split(in, " ")[0]
result = strings.Split(result, "/")[0]
result = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(result, "")
return result
}
func ToVarName(in string) string {
var result string
result = regexp.MustCompile(`[^a-zA-Z0-9]+`).ReplaceAllString(in, "")
return strings.ToLower(result)
}
func stringEqual(a, b string) bool {
return a == b
}
func addOne(i int) int {
return i + 1
}
func formatChapterBody(in string) string {
var result string
result = regexp.MustCompile(`\n([A-Z]\.)`).ReplaceAllString(in, "</br></br>$1")
result = regexp.MustCompile(`\n([0-9]\.)`).ReplaceAllString(result, "</br>$1")
result = pcre.MustCompile(`(?s)(<\/br>[0-9]\..+?)(?=<\/br>)`).ReplaceAllString(result, "<div class=\"indent\">$1</div>")
result = regexp.MustCompile(`:`).ReplaceAllString(result, ":</br>")
result = regexp.MustCompile(`([^.]+\.{5,}.+\n)`).ReplaceAllString(result, "$1</br>")
return result
}
func subChapterText(r io.Reader, chapterText *regexp.Regexp) string {
var result string
resultBytes, err := io.ReadAll(r)
if err != nil {
return ""
}
resultBytes = chapterText.ReplaceAll(resultBytes, []byte{})
result = template.HTMLEscapeString(string(resultBytes))
result = formatChapterBody(result)
return result
}
func main() {
rules := readFile()
rulesBytes, err := io.ReadAll(rules)
if err != nil {
log.Fatal(err)
}
rules.Seek(0, 0)
allChapters := []Chapter{
{
Name: "Street",
Number: "13",
start: regexp.MustCompile(`\n13\. STREET CATEGORY\n`),
end: regexp.MustCompile(`\n14\. STREET TOURING® CATEGORY\n`),
ChapterFillerText: regexp.MustCompile(`13\. Street Category`),
templateFile: "./templates/a/s.html.tmpl",
outputFile: "./src/a/s.html",
},
{
Name: "Street Touring",
Number: "14",
start: regexp.MustCompile(`\n14\. STREET TOURING® CATEGORY\n`),
end: regexp.MustCompile(`\n15\. STREET PREPARED CATEGORY\n`),
ChapterFillerText: regexp.MustCompile(`14\. Street Touring®`),
templateFile: "./templates/a/st.html.tmpl",
outputFile: "./src/a/st.html",
},
{
Name: "Street Prepared",
Number: "15",
start: regexp.MustCompile(`\n15\. STREET PREPARED CATEGORY\n`),
end: regexp.MustCompile(`\n16\. STREET MODIFIED CATEGORY\n`),
ChapterFillerText: regexp.MustCompile(`15\. Street Prepared`),
templateFile: "./templates/a/sp.html.tmpl",
outputFile: "./src/a/sp.html",
},
{
Name: "Street Modified",
Number: "16",
start: regexp.MustCompile(`\n16\. STREET MODIFIED CATEGORY\n`),
end: regexp.MustCompile(`\n17\. PREPARED CATEGORY\n`),
ChapterFillerText: regexp.MustCompile(`16\. Street Modified`),
templateFile: "./templates/a/sm.html.tmpl",
outputFile: "./src/a/sm.html",
},
{
Name: "Prepared",
Number: "17",
start: regexp.MustCompile(`\n17\. PREPARED CATEGORY\n`),
end: regexp.MustCompile(`\n18\. MODIFIED CATEGORY\n`),
ChapterFillerText: regexp.MustCompile(`17\. Prepared`),
templateFile: "./templates/a/p.html.tmpl",
outputFile: "./src/a/p.html",
},
{
Name: "Modified",
Number: "18",
start: regexp.MustCompile(`\n18\. MODIFIED CATEGORY\n`),
end: regexp.MustCompile(`\n19\. KART CATEGORY\n`),
ChapterFillerText: regexp.MustCompile(`18\. Modified Category`),
templateFile: "./templates/a/m.html.tmpl",
outputFile: "./src/a/m.html",
},
{
Name: "Solo Spec Coupe",
Number: "20",
start: regexp.MustCompile(`\n20\. SOLO. SPEC COUPE \(SSC\)\n`),
end: regexp.MustCompile(`\n21\. PROSOLO® NATIONAL SERIES RULES\n`),
ChapterFillerText: regexp.MustCompile(`20\. Solo® Spec Coupe \(SSC\)`),
templateFile: "./templates/a/ssc.html.tmpl",
outputFile: "./src/a/ssc.html",
},
{
Name: "Xtreme Street",
Number: "n/a",
start: regexp.MustCompile(`\nClassic American Muscle \/ Xtreme Street Category\n`),
end: regexp.MustCompile(`\nElectrical Vehicle Experimental \(EVX\)\n`),
},
{
Name: "EVX",
Number: "n/a",
start: regexp.MustCompile(`\nElectrical Vehicle Experimental \(EVX\)\n`),
end: regexp.MustCompile(`\nAPPENDIX C - SOLO® ROLL BAR STANDARDS\n`),
},
}
toRemove := []*pcre.Regexp{
pcre.MustCompile(`(?s)20-40% MORE.+Section 14`),
pcre.MustCompile(`(?s)orders over .+15\. Street Prepared`),
pcre.MustCompile(`(?s)Own a vehicle.+Section 16`),
pcre.MustCompile(`(?s)MAKE EVERY.+©2022 Sunmarks, LLC. All Rights Reserved\.`),
pcre.MustCompile(`(?s)orders over .+Section 19`),
pcre.MustCompile(`\nSection 14\n`),
pcre.MustCompile(`\nSection 15\n`),
pcre.MustCompile(`\nSection 16\n`),
pcre.MustCompile(`\n17. Prepared\n`),
pcre.MustCompile(`\n18. Modified Category\n`),
pcre.MustCompile(`\n21. ProSolo® Series\n`),
}
SMWeights := []*pcre.Regexp{
pcre.MustCompile(`(?s)Super Street Modified class \(SSM\)\n.+(Minimum Weight Calculations without driver.+?)\nStreet Modified class \(SM\)\n`),
pcre.MustCompile(`(?s)Street Modified class \(SM\)\n.+(Minimum Weight Calculations without driver.+?)\nStreet Modified Front-Wheel-Drive class \(SMF\)\n`),
pcre.MustCompile(`(?s)Street Modified Front-Wheel-Drive class \(SMF\)\n.+(Minimum Weight Calculations without driver.+?)Prepared \(XP\) - Appendix A`),
}
// using two arrays here for SMWeights and SMClasses sinces maps aren't walked deterministically
SMClasses := []string{
"Super Street Modified (SSM)",
"Street Modified (SM)",
"Street Modified FWD (SMF)",
}
funcMap := template.FuncMap{
"subChapterText": subChapterText,
"menuName": ToMenuName,
"stringEqual": stringEqual,
"addOne": addOne,
"toVarName": ToVarName,
}
for i := range allChapters {
if allChapters[i].Number != "n/a" {
SubChapters := getSubChapters(string(rulesBytes), allChapters[i].Number)
allChapters[i].SubChapters = SubChapters
}
fmt.Printf("Currently processing: %s\n", allChapters[i].Name)
chapterReader := getChapterReader(rules, allChapters[i])
allChapters[i].Reader = chapterReader
chapterText, err := io.ReadAll(chapterReader)
if err != nil {
fmt.Println("error reading chapter text")
os.Exit(1)
}
// remove all form feed (i.e. ) chapter title lines
if allChapters[i].Number != "n/a" {
remove := regexp.MustCompile(`\n\f` + allChapters[i].Number + `\. .+\n`)
chapterText = remove.ReplaceAll(chapterText, []byte{})
}
// remove all page number text
remove := regexp.MustCompile(`(?i)([0-9]+ — )*\d{4} SCCA® NATIONAL SOLO® RULES( )*(— [0-9]+)*`)
chapterText = remove.ReplaceAll(chapterText, []byte{})
// remove certain text (ads, section markers)
for _, r := range toRemove {
chapterText = r.ReplaceAll(chapterText, []byte{})
}
if allChapters[i].Number != "n/a" && len(allChapters[i].SubChapters) > 0 {
allChapters[i].SubChapters = findSubChapterBody(allChapters[i], chapterText)
}
// grab minmum weights from appendix for SM
if allChapters[i].Name == "Street Modified" {
var weightInfo string
for i, regex := range SMWeights {
weightInfo = weightInfo + "</br></br>" + SMClasses[i] + ":</br>"
match := regex.FindAllStringSubmatch(string(rulesBytes), 1)
if len(match) > 0 {
weightInfo = weightInfo + match[0][1] + "\n"
}
}
weightInfo = remove.ReplaceAllString(weightInfo, "")
weightInfo = regexp.MustCompile(`(•)`).ReplaceAllString(weightInfo, "</br>$1")
allChapters[i].WeightInformation = weightInfo
}
if allChapters[i].templateFile != "" {
fmt.Println("Generating class specific page...")
classTemplate := template.New(path.Base(allChapters[i].templateFile)).Funcs(funcMap)
tpl, err := classTemplate.ParseFiles(allChapters[i].templateFile)
if err != nil {
log.Fatal("Could not parse template", err)
}
outFile, err := os.Create(allChapters[i].outputFile)
if err != nil {
log.Fatal("Could not create file", err)
}
err = tpl.Execute(outFile, allChapters[i])
if err != nil {
log.Fatal("Could not execute template", err)
}
outFile.Close()
}
}
fmt.Println("Generating common.js...")
commonJS := template.New("common.js.tmpl").Funcs(funcMap)
tpl, err := commonJS.ParseFiles("templates/common.js.tmpl")
if err != nil {
log.Fatal("Could not parse template", err)
}
outFile, err := os.Create("src/common.js")
if err != nil {
log.Fatal("Could not create file", err)
}
err = tpl.Execute(outFile, allChapters)
if err != nil {
log.Fatal("Could not execute template", err)
}
outFile.Close()
}