-
Notifications
You must be signed in to change notification settings - Fork 57
/
creeper.go
110 lines (91 loc) · 1.84 KB
/
creeper.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
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
package creeper
import "io/ioutil"
func Open(path string) *Creeper {
buf, _ := ioutil.ReadFile(path)
raw := string(buf)
return New(raw)
}
func New(raw string) *Creeper {
f := Formatting(raw)
return NewByFormatted(f)
}
func NewByFormatted(f *Formatted) *Creeper {
c := new(Creeper)
c.Nodes = f.Nodes
for _, n := range c.Nodes {
n.Creeper = c
}
c.Towns = f.Towns
for _, t := range c.Towns {
t.Creeper = c
}
cache := map[string]string{}
c.CacheGet = func(k string) (string, bool) {
v, e := cache[k]
return v, e
}
c.CacheSet = func(k string, v string) {
cache[k] = v
}
return c
}
type Creeper struct {
Nodes []*Node
Towns []*Town
CacheGet func(string) (string, bool)
CacheSet func(string, string)
Node *Node
}
func (c *Creeper) Array(key string) *Creeper {
if c.Node == nil {
c.Node = c.Nodes[0].SearchFlatScope(key)
} else {
c.Node = c.Node.FirstChildNode.SearchFlatScope(key)
}
return c
}
func (c *Creeper) String(key string) string {
v, _ := c.StringE(key)
return v
}
func (c *Creeper) StringE(key string) (string, error) {
return c.Node.FirstChildNode.SearchFlatScope(key).Value()
}
func (c *Creeper) Each(cle func(*Creeper)) {
fstNv := []string{}
repStor := []string{}
each:
for {
if c.Node.PrimaryNode() != nil {
v, err := c.Node.PrimaryNode().Value()
v = MD5(v)
if err != nil {
continue
}
if c.Node.Index == 0 {
for _, s := range fstNv {
if s == v {
break each
}
}
fstNv = append(fstNv, v)
}
for _, s := range repStor {
if s == v {
continue each
}
}
repStor = append(repStor, v)
}
cle(c)
//println(c.Node.Index + 1, c.Node.Fun.BundleSize, c.Node.Page.NextNoMore)
if c.Node.Index == c.Node.Fun.BundleSize - 1 && c.Node.Page.NextNoMore {
break each
}
c.Next()
}
}
func (c *Creeper) Next() *Creeper {
c.Node.Next()
return c
}