-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
114 lines (106 loc) · 2.8 KB
/
parser.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func ParseFile(file *os.File) ([]ToDo, error) {
var todos []ToDo
var currentCategory string
startParsing := false
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if !startParsing {
if strings.HasPrefix(line, "###") {
startParsing = true
}
continue
}
if line == "" {
continue
}
if strings.HasPrefix(line, "@") {
currentCategory = strings.TrimPrefix(line, "@")
} else if strings.HasPrefix(line, "-") {
str := strings.TrimPrefix(line, "-")
parts := strings.Split(str, "$")
text := strings.TrimSpace(parts[0])
points, err := strconv.Atoi(parts[1])
if err != nil {
fmt.Println("Error not int")
return nil, nil
}
todo := ToDo{
isDone: false,
Text: strings.TrimSpace(text),
Points: points,
Category: currentCategory,
}
switch currentCategory {
case "Bonus España", "Planes por Madrid", "Bares", "Cafés", "Aventura", "Bonus Cerca de Madrid", "Bonus Europa":
todo.isHidden = true
}
todos = append(todos, todo)
} else if strings.HasPrefix(line, "F") {
parts := strings.Split(line, "/")
if len(parts) > 2 {
return nil, fmt.Errorf("invalid format for completed task: %s", line)
}
dateStr := strings.TrimSpace(parts[1])
subStr := strings.Split(strings.TrimPrefix(parts[0], "F"), "$")
text := strings.TrimSpace(subStr[0])
points, _ := strconv.Atoi(strings.TrimSpace(subStr[1]))
completationDate, err := time.Parse("2006-01-02", dateStr)
if err != nil {
return nil, fmt.Errorf("invalid date format: %s", dateStr)
}
todo := ToDo{
isDone: true,
Text: text,
Category: currentCategory,
CompletionDate: completationDate,
Points: points,
}
todos = append(todos, todo)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return todos, nil
}
func saveFile(filename string, todos []ToDo) {
file, err := os.Create(filename)
if err != nil {
fmt.Println("Failed to create file:", err)
return
}
defer file.Close()
file.WriteString("### LISTA DE PLANES EN MADRID" + "\n")
var currentCategory string
for _, todo := range todos {
if currentCategory == "" {
currentCategory = todo.Category
file.WriteString("@" + currentCategory + "\n")
}
if currentCategory != todo.Category {
currentCategory = todo.Category
file.WriteString("@" + currentCategory + "\n")
}
var str string
if todo.isDone {
str = "F " + todo.Text + " $" + strconv.Itoa(todo.Points) + " / " + todo.CompletionDate.Format("2006-01-02") + "\n"
} else {
str = "- " + todo.Text + " $" + strconv.Itoa(todo.Points) + "\n"
}
_, err := file.WriteString(str)
if err != nil {
fmt.Println("Failed to write string")
return
}
}
}