-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.go
More file actions
88 lines (71 loc) · 2.76 KB
/
preprocessor.go
File metadata and controls
88 lines (71 loc) · 2.76 KB
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
package preprocessor
import (
"strings"
)
// The preprocessor is responsible for cleaning up the input Yarnball code.
// There are a lot of little things that make Yarnball code look more like crochet,
// but are not, in fact, needed for the code to work at all (as of right now).
type Preprocessor struct{}
func New() *Preprocessor {
return &Preprocessor{}
}
func (p *Preprocessor) Process(input string) (string, error) {
lines := strings.Split(input, "\n")
var processedLines []string
// Remove everything before and including the line that says "STITCH GUIDE:" (case-insensitive).
var startIdx int
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.EqualFold(trimmed, "STITCH GUIDE:") {
startIdx = i + 1 // Start processing from the next line
break
}
if strings.EqualFold(trimmed, "INSTRUCTIONS:") {
startIdx = i + 1 // Start processing from the next line
break
}
}
for _, line := range lines[startIdx:] {
// Commas are just to make things look nice, currently serve no other purpose---ignore them.
line = strings.ReplaceAll(line, ",", "")
line := strings.TrimSpace(line)
// If the line is empty, a comment, or exactly "INSTRUCTIONS:" leave an empty line to preserve line numbers.
if line == "" || strings.HasPrefix(line, "#") || strings.EqualFold(line, "INSTRUCTIONS:") {
processedLines = append(processedLines, "")
continue
}
if strings.TrimSpace(line) == "" || strings.HasPrefix(strings.TrimSpace(line), "#") {
continue // Skip empty lines and comment lines
}
// If the line says "INSTRUCTIONS:", skip it --- that's just our label to separate the stitch guide (functions) from the actual instructions.
if strings.EqualFold(strings.TrimSpace(line), "INSTRUCTIONS:") {
continue
}
// Remove any comments from the line
line = p.removeComment(line)
// Remove "Row #:" or "Round #:" prefixes if present
line = p.RemoveRowRoundPrefix(line)
// convert to lowercase
line = strings.ToLower(line)
processedLines = append(processedLines, line)
}
return strings.Join(processedLines, "\n"), nil
}
func (p *Preprocessor) removeComment(line string) string {
// Check if the line contains a comment
if idx := strings.Index(line, "#"); idx != -1 {
return strings.TrimSpace(line[:idx]) // Return the line up to the comment
}
return line
}
// RemoveRowRoundPrefix removes the "Row N:" or "Round N:" prefix from a line if it exists.
// We don't need those, they just add a little crochet-inspired flair to the code.
func (p *Preprocessor) RemoveRowRoundPrefix(line string) string {
if strings.HasPrefix(line, "Row ") || strings.HasPrefix(line, "Round ") {
parts := strings.SplitN(line, ":", 2)
if len(parts) > 1 {
return strings.TrimSpace(parts[1]) // Return the part after the colon
}
}
return line
}