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
|
package todolist
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/jinzhu/now"
)
type Parser struct{}
func (p *Parser) Parse(input string) *Todo {
todo := NewTodo()
todo.Subject = p.Subject(input)
todo.Projects = p.Projects(input)
todo.Contexts = p.Contexts(input)
if p.hasDue(input) {
todo.FormattedDue = p.Due(input)
}
return todo
}
func (p *Parser) Subject(input string) string {
if strings.Contains(input, " due") {
index := strings.LastIndex(input, " due")
return input[0:index]
} else {
return input
}
}
func (p *Parser) Projects(input string) []string {
r, _ := regexp.Compile(`\+\w+`)
return p.matchWords(input, r)
}
func (p *Parser) Contexts(input string) []string {
r, err := regexp.Compile(`\@\w+`)
if err != nil {
fmt.Println("regex error", err)
}
return p.matchWords(input, r)
}
func (p *Parser) hasDue(input string) bool {
r, _ := regexp.Compile(`due \w+$`)
return r.MatchString(input)
}
func (p *Parser) Due(input string) time.Time {
r, _ := regexp.Compile(`due .*$`)
res := r.FindString(input)
res = res[4:len(res)]
switch {
case res == "today":
return now.BeginningOfDay()
case res == "tomorrow" || res == "tom":
return now.BeginningOfDay().AddDate(0, 0, 1)
case res == "monday" || res == "mon":
n := now.BeginningOfDay()
return now.New(n).Monday().AddDate(0, 0, 7)
case res == "tuesday" || res == "tue":
n := now.BeginningOfDay()
return now.New(n).Monday().AddDate(0, 0, 1)
case res == "wednesday" || res == "wed":
n := now.BeginningOfDay()
return now.New(n).Monday().AddDate(0, 0, 2)
case res == "next week":
n := now.BeginningOfDay()
return now.New(n).Monday().AddDate(0, 0, 7)
}
//return now.Parse(input)
return time.Now()
}
func (p *Parser) matchWords(input string, r *regexp.Regexp) []string {
results := r.FindAllString(input, -1)
ret := []string{}
for _, val := range results {
ret = append(ret, val[1:len(val)])
}
return ret
}
|