-
Notifications
You must be signed in to change notification settings - Fork 57
/
format.go
61 lines (51 loc) · 1.05 KB
/
format.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
package creeper
import (
"regexp"
"strings"
)
type Formatted struct {
Raw string
Towns []*Town
Nodes []*Node
}
var (
rx_isTown = regexp.MustCompile(`^\s*[a-zA-Z][a-zA-Z_-]{0,31}\s*(\(|=)`)
rx_isNode = regexp.MustCompile(`^\s*@?[a-zA-Z_-]+(\[]|\*)?:`)
)
func Formatting(s string) *Formatted {
ln := strings.Split(s, "\n")
ln = stringsFilter(ln, func(s string) bool {
l := strings.TrimSpace(s)
return len(l) > 0 && l[0] != '#'
})
ln = stringsMap(ln, func(s string) string {
return strings.Replace(s, "\r", "", -1)
})
ln = linesCombine(ln)
townLn := stringsMatch(ln, rx_isTown)
nodeLn := stringsMatch(ln, rx_isNode)
towns := ParseTown(townLn)
nodes := ParseNode(nodeLn)
return &Formatted{
Raw: s,
Towns: towns,
Nodes: nodes,
}
}
func linesCombine(l []string) []string {
nl := []string{}
for i, s := range l {
if strings.TrimSpace(s)[0] != '.' {
s := s
for i := i + 1; i < len(l); i++ {
ns := strings.TrimSpace(l[i])
if ns[0] != '.' {
break
}
s = s + ns
}
nl = append(nl, s)
}
}
return nl
}