-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule-parser.go
68 lines (63 loc) · 1.53 KB
/
schedule-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
package file
import(
"os"
"bufio"
"strings"
"path/filepath"
"github.com/inajob/frontmatter"
)
type ScheduleParserResult struct {
Path string `json:"path"`
LineNo int `json:"lineNo"`
Text string `json:"text"`
Meta map[string]interface{} `json:"meta"`
}
func parseFile(path string) []ScheduleParserResult{
results := make([]ScheduleParserResult, 0)
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
// TODO: this requires memory
for i := 1; scanner.Scan(); i++ {
target := scanner.Text()
// 012345678901
// [2020-01-01]
if len(target) >= 12 && strings.HasPrefix(target, "[") {
sep1 := target[5]
sep2 := target[8]
if sep1 == '-' &&
sep2 == '-' {
r := ScheduleParserResult {
Path: path,
LineNo: i,
Text: scanner.Text(),
}
results = append(results, r)
}
}
}
if len(results) > 0 {
file.Seek(0, 0) // TODO: use above body data
meta, _, err := frontmatter.ParseFrontMatter(file) // TODO: slow
if err == nil {
for i := 0; i < len(results); i ++ {
results[i].Meta = meta
}
}
}
return results
}
func ParseSchedule(path string) []ScheduleParserResult {
results := make([]ScheduleParserResult, 0)
filepath.Walk(path, func(path string, file os.FileInfo, err error) error{
rs := parseFile(path)
if len(rs) > 0 {
results = append(results, rs...)
}
return nil
})
return results
}