-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
167 lines (133 loc) · 3.27 KB
/
Copy pathparser.go
File metadata and controls
167 lines (133 loc) · 3.27 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
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
// SPDX-License-Identifier: MIT
package svgsequence
import (
"bufio"
"bytes"
"fmt"
"os"
"strconv"
"strings"
)
// GenerateFromCFG generates the sequence by parsing a config file.
func GenerateFromCFG(filename string) (string, error) {
data, err := os.ReadFile(filename)
if err != nil {
return "", fmt.Errorf("error reading file '%s': %w", filename, err)
}
r := bytes.NewReader(data)
scanner := bufio.NewScanner(r)
// Initialize the sequence
s := NewSequence()
lineNum := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
lineNum++
// Skip empty lines and comments
if line == "" || line[0] == '#' {
continue
}
// Sequence properties
key, val, ok := strings.Cut(line, "=")
key, val = strings.TrimSpace(key), strings.TrimSpace(val)
if ok {
switch key {
case "distance_between_actors":
s.SetDistance(parseIntDefault(val, defaultDistance))
case "step_height":
s.SetStepHeight(parseIntDefault(val, defaultStepHeight))
case "width":
s.SetWidth(val)
case "height":
s.SetHeight(val)
case "vertical_section_text":
s.SetVerticalSectionText(val == "1" || val == "true" || val == "True")
default:
}
}
parts := strings.Split(line, " ")
property := parts[0]
if property[0] != '@' {
continue
}
switch property {
case "@actors":
s.AddActors(parseProperty(line, property)...)
case "@start":
values := parseProperty(line, property)
var name, color string
bordered := true
switch len(values) {
case 0:
return "", fmt.Errorf("section needs a name at line %d", lineNum)
case 1:
name = values[0]
case 2:
name = values[0]
color = values[1]
default:
name = values[0]
color = values[1]
if values[2] == "false" {
bordered = false
}
}
s.OpenSection(name, &SectionConfig{Color: color, WithoutBorder: !bordered})
case "@end":
s.CloseSection()
case "@closeall":
s.CloseAllSections()
case "@step":
values := parseProperty(line, property)
var src, tgt, desc, color string
switch len(values) {
case 0, 1:
return "", fmt.Errorf("not enough values for step at line %d", lineNum)
case 2:
src = values[0]
tgt = values[1]
case 3:
src = values[0]
tgt = values[1]
desc = values[2]
default:
src = values[0]
tgt = values[1]
desc = values[2]
color = values[3]
}
s.AddStep(Step{
Text: desc,
Source: src,
Target: tgt,
Color: color,
})
default:
return "", fmt.Errorf(`unknown property: "%s" at line %d`, property, lineNum)
}
}
return s.Generate()
}
// parseIntDefault is a helper function to convert a string to int
// returns the default value if parsing fails.
func parseIntDefault(s string, def int) int {
n, err := strconv.Atoi(s)
if err != nil {
return def
}
return n
}
// parseProperty is a helper function to separate values from properties.
func parseProperty(line, property string) []string {
// remove the prefix (@actors, @start, ...)
rest := strings.TrimPrefix(line, property)
parts := strings.Split(rest, ",")
values := make([]string, 0, len(parts))
for _, p := range parts {
trimmed := strings.TrimSpace(p)
trimmed = strings.ReplaceAll(trimmed, `\n`, "\n")
if trimmed != "" {
values = append(values, trimmed)
}
}
return values
}