aboutsummaryrefslogtreecommitdiffstats
path: root/entry.go
blob: 189014faf76f4f0e19c36dc595007a249568aa5e (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
	"os"
	"path/filepath"
	"regexp"
	"strconv"
        "fmt"
)

/*
 * This file contains all the code related to reading and processing entries
 */
type entry struct {
	name     string
	rawName  string
	fullPath string
}

type logEntry struct {
	baseEntry entry
	day       int
	month     int
	year      int
}

func findEntries(root string) (entries []Renderer) {
	baseEntryRegex := regexp.MustCompile("([A-z\\-]+)\\.md")
	logEntryRegex := regexp.MustCompile("(20[0-9]{2})-([0-9]{2})-([0-9]{2})-([A-z\\-]+)\\.md")

        fmt.Println(root)
	err := filepath.Walk(root, func(p string, f os.FileInfo, e error) error {
		if !f.IsDir() && baseEntryRegex.MatchString(f.Name()) {
			baseCaptures := baseEntryRegex.FindStringSubmatch(f.Name())
			newBaseEntry := entry{name: nameify(baseCaptures[1]),
				rawName:  baseCaptures[1],
				fullPath: p}

			if logEntryRegex.MatchString(f.Name()) {
				logCaptures := logEntryRegex.FindStringSubmatch(f.Name())
				y, _ := strconv.Atoi(logCaptures[1])
				m, _ := strconv.Atoi(logCaptures[2])
				d, _ := strconv.Atoi(logCaptures[3])

				newLogEntry := logEntry{
					baseEntry: newBaseEntry,
					day:       d,
					month:     m,
					year:      y}
				entries = append(entries, newLogEntry)
			} else {
				entries = append(entries, newBaseEntry)
			}
		}
		return nil
	})
	if err != nil {
		panic(err)
	}

	return
}