aboutsummaryrefslogtreecommitdiffstats
path: root/parser.go
blob: bb54b97fcb69d2c1a91e3651205eee8a16fe3602 (plain)
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
package todolist

import (
	"fmt"
	"regexp"
	"strings"
)

type Parser struct{}

func (p *Parser) Parse(input string) *Todo {
	todo := NewTodo()
	todo.Subject = p.Subject(input)
	todo.Projects = p.Projects(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, err := regexp.Compile(`\+\w+`)
	if err != nil {
		fmt.Printf("There was a problem with the regexp", err)
	}
	results := r.FindAllString(input, -1)
	ret := []string{}

	for _, val := range results {
		fmt.Println("Result is ", val)
		ret = append(ret, val[1:len(val)])
	}
	return ret
}