-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
256 lines (209 loc) · 5.88 KB
/
Copy pathparser.go
File metadata and controls
256 lines (209 loc) · 5.88 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
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
// SPDX-License-Identifier: MIT
package svgtimeline
import (
"bufio"
"bytes"
"fmt"
"os"
"strconv"
"strings"
"time"
)
// GenerateFromCFG generates the timeline by parsing a config file with an optional css style.
func GenerateFromCFG(filename string, cssFilename string) (string, error) {
var cssStyle string
if cssFilename != "" {
css, err := os.ReadFile(cssFilename)
if err != nil {
return "", fmt.Errorf("error reading file '%s': %w", cssFilename, err)
}
cssStyle = string(css)
}
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 timeline
tl := NewTimeline()
margins := [4]int{0, 0, 0, 0} // top , right , bottom , left
setMargins := false
var currentEvent *Event
currentSection := ""
lineNum := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
lineNum++
// Skip empty lines and comments
if line == "" || line[0] == '#' {
continue
}
parts := strings.Fields(line)
switch line[0] {
case '@':
if currentEvent != nil {
row := tl.GetLastRow()
if row == nil {
return "", fmt.Errorf("error at line %d, cannot add an event without creating a row first", lineNum)
}
row.AddEvent(*currentEvent)
currentEvent = nil
}
currentSection = parts[0] // @timeline, @row, @task, @era
switch currentSection {
case "@row":
height := parseIntDefault(parts, 1, 30)
separator := parseIntDefault(parts, 2, 5)
tl.AddRow(height, separator)
case "@era":
currentEvent = &Event{Type: EventTypeEra}
case "@task":
currentEvent = &Event{Type: EventTypeTask}
default:
}
default:
key, val, ok := strings.Cut(line, "=")
if !ok {
return "", fmt.Errorf("unknown value at line %d", lineNum)
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
switch currentSection {
case "@timeline":
switch key {
// Single digit properties
case "content_width", "min_event_width", "num_ticks", "tick_height", "margin_top", "margin_bottom", "margin_left", "margin_right":
x, err2 := strconv.Atoi(val)
if err2 != nil {
return "", fmt.Errorf("error at line %d: %w", lineNum, err2)
}
switch key {
case "content_width":
tl.SetContentWidth(x)
case "min_event_width":
tl.SetMinEventWidth(x)
case "num_ticks":
tl.SetNumTicks(x)
case "tick_height":
tl.SetTickHeight(x)
case "margin_top":
setMargins = true
margins[0] = x
case "margin_right":
setMargins = true
margins[1] = x
case "margin_bottom":
setMargins = true
margins[2] = x
case "margin_left":
setMargins = true
margins[3] = x
default:
}
case "id":
tl.SetID(val)
case "width":
tl.SetWidth(val)
case "height":
tl.SetHeight(val)
default:
return "", fmt.Errorf("unknown property '%s' at line %d", key, lineNum)
}
case "@row":
return "", fmt.Errorf("error at line %d, row has no configuration options", lineNum)
case "@task", "@era":
switch key {
case "id":
currentEvent.ID = val
case "class":
currentEvent.Class = val
case "text":
currentEvent.Text = val
case "title":
currentEvent.Title = val
case "duration":
dur, err2 := time.ParseDuration(val)
if err2 != nil {
return "", fmt.Errorf("error at line %d while parsing duration of event, %w", lineNum, err2)
}
currentEvent.Duration = dur
case "time":
t, err2 := parseTime(val)
if err2 != nil {
return "", err2
}
currentEvent.Time = t
default:
return "", fmt.Errorf("unknown event property '%s' at line %d", key, lineNum)
}
default:
return "", fmt.Errorf("unknown section: %s", currentSection)
}
}
}
err = scanner.Err()
if err != nil {
return "", fmt.Errorf("scanner error: %w", err)
}
// Last event
if currentEvent != nil {
row := tl.GetLastRow()
if row == nil {
return "", fmt.Errorf("error at line %d, cannot add an event without creating a row first", lineNum)
}
row.AddEvent(*currentEvent)
}
if setMargins {
tl.SetMargins(margins[0], margins[1], margins[2], margins[3])
}
if cssStyle != "" {
tl.SetStyle(cssStyle)
}
return tl.Generate()
}
// parseIntDefault is a helper function to convert a string to int
// returns the default value if parsing fails.
func parseIntDefault(parts []string, i, def int) int {
if len(parts) <= i {
return def
}
n, err := strconv.Atoi(parts[i])
if err != nil {
return def
}
return n
}
// parseTime tries to parse time strings in common formats.
func parseTime(input string) (time.Time, error) {
formats := []string{
"2006-01-02T15:04:05.99Z", // UTC with nanosecond precision
time.UnixDate, // Mon Jan _2 15:04:05 MST 2006
time.ANSIC, // Mon Jan _2 15:04:05 2006
time.RFC3339, // 2006-01-02T15:04:05Z07:00
time.RFC1123, // Mon, 02 Jan 2006 15:04:05 MST
time.RFC822, // 02 Jan 06 15:04 MST
time.RFC850, // Monday, 02-Jan-06 15:04:05 MST
time.DateTime, // 2006-01-02 15:04:05
"2006/01/02 15:04:05", // Common slash style
"02/01/2006 15:04:05", // European style
time.DateOnly, // 2006-01-02
"02/01/2006", // DD/MM/YYYY
"02 Jan 2006", // Human style
"02-Jan-2006", // Human with dashes
"15:04:05.99", // With nanosecond precision
"15:04:05", // Only time
"15:04", // Hour and minute only
}
var (
t time.Time
err error
)
for _, layout := range formats {
t, err = time.Parse(layout, input)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unrecognized time format: %s\nyou might use one of the following formats: %v", input, formats)
}