-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparser.go
More file actions
151 lines (137 loc) · 3.1 KB
/
Copy pathparser.go
File metadata and controls
151 lines (137 loc) · 3.1 KB
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
package component
import (
"io"
"sort"
"strings"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// Parser is an helper, creates a tree from the repo
type Parser struct {
index map[[2]string]int
categories []*Category
assets []*Asset
forms []*Form
}
// Parse executes the parsing on a repo
func (p *Parser) Parse(t *object.Tree) error {
p.index = make(map[[2]string]int)
p.categories = make([]*Category, 0)
if err := p.parse(t, filterCat); err != nil {
return err
}
if err := p.parse(t, filterRes); err != nil {
return err
}
sort.Sort(catSorter(p.categories))
for i := range p.categories {
sort.Sort(subSorter(p.categories[i].subcategories))
for j := range p.categories[i].subcategories {
for k := range p.categories[i].subcategories[j].difficulties {
sort.Sort(itemSorter(p.categories[i].subcategories[j].difficulties[k].items))
}
}
}
return nil
}
func (p *Parser) parse(t *object.Tree, fn func(name string) bool) error {
for iter := t.Files(); ; {
f, err := iter.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
if !fn(f.Name) {
continue
}
if err := p.parseFile(f); err != nil {
return err
}
}
return nil
}
func (p *Parser) parseFile(f *object.File) error {
contents, err := f.Contents()
if err != nil {
return parseError{f.Name, "read", err}
}
if ok, _ := f.IsBinary(); !ok {
contents = strings.Replace(strings.TrimSpace(contents), "\r\n", "\n", -1)
}
cmp, err := newCmp(f.Name)
if err != nil {
return parseError{f.Name, "cmp", err}
}
if err := p.setPath(f.Name, cmp); err != nil {
return err
}
if err := cmp.SetContents(contents); err != nil {
return parseError{f.Name, "contents", err}
}
return nil
}
func (p *Parser) setPath(name string, cmp Component) error {
if err := cmp.SetPath(name); err != nil {
return parseError{name, "path", err}
}
switch c := cmp.(type) {
case *Asset:
p.assets = append(p.assets, c)
return nil
case *Form:
p.forms = append(p.forms, c)
return nil
}
parts := strings.Split(name, "/")
idx := [2]string{parts[1], name[9:11]}
if cat, ok := cmp.(*Category); ok {
p.index[idx] = len(p.categories)
p.categories = append(p.categories, cat)
return nil
}
cat := p.categories[p.index[idx]]
if cat == nil {
return parseError{name, "path", "Invalid cat"}
}
if sub, ok := cmp.(*Subcategory); ok {
cat.Add(sub)
return nil
}
sub := cat.Sub(parts[2])
if sub == nil {
sub = &Subcategory{ID: parts[2]}
cat.Add(sub)
}
if dif, ok := cmp.(*Difficulty); ok {
sub.AddDifficulty(dif)
return nil
}
dif := sub.Difficulty(parts[3])
if dif == nil {
dif = &Difficulty{ID: parts[3]}
sub.AddDifficulty(dif)
}
switch c := cmp.(type) {
case *Item:
dif.AddItem(c)
case *Checklist:
dif.SetChecks(c)
default:
return parseError{name, "type", "Invalid Path"}
}
return nil
}
func (p *Parser) Categories() map[string][]*Category {
var res = make(map[string][]*Category)
for _, cat := range p.categories {
res[cat.Locale] = append(res[cat.Locale], cat)
}
return res
}
func (p *Parser) Assets() []*Asset {
return p.assets
}
func (p *Parser) Forms() []*Form {
return p.forms
}