-
Notifications
You must be signed in to change notification settings - Fork 0
/
piyolog.go
222 lines (201 loc) Β· 4.67 KB
/
piyolog.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
package piyolog
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/text/language"
)
var piyoLoc, _ = time.LoadLocation("Asia/Tokyo")
// SetLocation sets the location.
func SetLocation(loc *time.Location) {
piyoLoc = loc
}
type section int
const (
sectionDate section = iota + 1
sectionBaby
sectionLogs
sectionResults
sectionJournal
sectionEnd
)
func (s *section) next() section {
switch *s {
case sectionDate:
*s = sectionBaby
case sectionBaby:
*s = sectionLogs
case sectionLogs:
*s = sectionResults
case sectionResults:
*s = sectionJournal
case sectionJournal:
*s = sectionEnd
}
return *s
}
func (s *section) end() {
*s = sectionEnd
}
const (
piyologJa = "γγ΄γγγ°γ"
piyologEn = "[PiyoLog]"
piyologSeparator = "----------"
)
type Data struct {
Tag language.Tag
Entries []Entry
}
type Entry struct {
section section
Date time.Time
Baby *Baby
Logs []Log
Results []string
Journal string
}
type Baby struct {
Name string
DateOfBirth time.Time
}
func newData(str string) (d Data) {
switch {
case strings.Contains(str, piyologJa):
d.Tag = language.Japanese
case strings.Contains(str, piyologEn):
d.Tag = language.English
}
return d
}
func (d Data) newEntry(str string) *Entry {
if str == "" {
return nil
}
var layout string
switch d.Tag {
case language.Japanese:
str, _, _ = strings.Cut(str, "(")
layout = "2006/1/2"
case language.English:
_, str, _ = strings.Cut(str, ", ")
layout = "Jan 2, 2006"
}
date, err := time.ParseInLocation(layout, str, piyoLoc)
if err != nil {
return nil
}
e := &Entry{
section: sectionDate,
Date: date,
}
return e
}
var reBaby = regexp.MustCompile(`^(.*) \(([0-9]+)(ζ³|y)([0-9]+)(γζ|m)([0-9]+)(ζ₯|d)\)$`)
// newBaby returns au Baby value retrieving from the given value.
func (e Entry) newBaby(str string) *Baby {
matches := reBaby.FindStringSubmatch(str)
y, _ := strconv.Atoi(matches[2])
m, _ := strconv.Atoi(matches[4])
d, _ := strconv.Atoi(matches[6])
return &Baby{
Name: matches[1],
DateOfBirth: e.Date.AddDate(-y, -m, -d),
}
}
var reLog = regexp.MustCompile(`^([0-9:]{5} ?(AM|PM)?)`)
func (e *Entry) apply(line string) {
switch e.section {
case sectionDate:
e.section.next()
e.apply(line)
case sectionBaby:
if line == "" {
return
}
if reBaby.MatchString(line) {
e.Baby = e.newBaby(line)
e.section.next()
return
}
// if text doesn't contain a certain baby infomation, move to the next section.
e.section = sectionLogs
e.apply(line)
case sectionLogs:
if line == "" && len(e.Logs) > 0 {
e.section.next()
return
}
if reLog.MatchString(line) {
l := NewLog(line, e.Date)
if l != nil {
e.Logs = append(e.Logs, l)
}
return
}
case sectionResults:
if line == "" && len(e.Results) > 0 {
e.section = sectionJournal
return
}
e.Results = append(e.Results, line)
case sectionJournal:
if e.Journal == "" {
e.Journal = line
} else {
e.Journal = fmt.Sprintf("%s\n%s", e.Journal, line)
}
}
}
// Parse returns the Data value represented by the string.
// It accepts only export data from PiyoLog. Any other value may return an error.
func Parse(str string) (*Data, error) {
// replace escape line breaks with unescaped line breaks to be able to scan line by line.
str = strings.Replace(str, `\n`, "\n", -1)
// add one separator with TWO new lines to the tail of the file to handle the string as monthly data.
exportData := fmt.Sprintf("%s\n\n%s\n", str, piyologSeparator)
// replace "\n\n" (TWO new liens) with the separator with "\n" (one new line) with the separator
// in order not to parse the new line before the separator.
exportData = strings.ReplaceAll(
exportData, fmt.Sprintf("\n\n%s", piyologSeparator), fmt.Sprintf("\n%s", piyologSeparator))
scanner := bufio.NewScanner(bytes.NewBufferString(exportData))
// first, parse the head of the file to detect its language.
if !scanner.Scan() {
return nil, io.EOF
}
head := strings.TrimSpace(scanner.Text())
data := newData(head)
switch data.Tag {
case language.Japanese:
head = strings.TrimLeft(head, piyologJa)
case language.English:
head = strings.TrimLeft(head, piyologEn)
}
// generate an entry with the head text.
entry := data.newEntry(head)
for scanner.Scan() {
// handling the file as if monthly data.
line := scanner.Text()
if strings.HasPrefix(line, piyologSeparator) {
if entry != nil {
entry.section.end()
data.Entries = append(data.Entries, *entry)
entry = nil
}
continue
}
if entry == nil {
entry = data.newEntry(line)
} else {
entry.apply(line)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return &data, nil
}