-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.go
141 lines (117 loc) · 2.76 KB
/
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
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
package crumbs
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/teris-io/shortid"
)
// ParseLines parses a slice of text lines and builds the tree.
func ParseLines(lines []string, imagesPath, imagesSuffix string) (*Entry, error) {
mkID := idGenerator()
checkIcon := lookForIcon(imagesPath, imagesSuffix)
// generate a short id for the root node
rootID, err := mkID()
if err != nil {
return nil, err
}
// create the root node
root := newEmptyNote(rootID)
node := root
nodeDepth := 0
for _, el := range lines {
// skip empty lines
if strings.TrimSpace(el) == "" {
continue
}
// count depth
childDepth := depth(el)
// case: no leading 'stars' (skip line)
if childDepth == 0 {
continue
}
// trim leading 'stars', then the spaces
text := el[childDepth:]
text = strings.TrimSpace(text)
// create the child
childID, err := mkID()
if err != nil {
return nil, err
}
child := newNote(childID, childDepth, text)
// check if has an icon
checkIcon(child)
// case: the current 'node' is the parent
if childDepth > nodeDepth {
// update tree
child.parent = node
node.childrens = append(node.childrens, child)
// update loop state
node = child
nodeDepth++
// case: the current 'node' is not the parent of our child
} else if childDepth <= nodeDepth {
// adjust 'node' until it's correct
for childDepth <= nodeDepth {
node = node.parent
nodeDepth--
}
// update tree
child.parent = node
node.childrens = append(node.childrens, child)
// update loop state
node = child
nodeDepth++
}
}
return root, nil
}
// depth space-counting helper (probably done in a dumb way, dunno)
func depth(line string) int {
i := 0
for line[i] == '*' {
i++
}
return i
}
// newNote creates a new note element
func newNote(id string, lvl int, txt string) *Entry {
f := new(Entry)
f.id = id
f.text = txt
f.level = lvl
return f
}
// newNote creates a new note element
func newEmptyNote(id string) *Entry {
f := new(Entry)
f.id = id
f.level = -1
return f
}
// idGenerator generates a new short id at each invocation.
func idGenerator() func() (string, error) {
sid, err := shortid.New(1, shortid.DefaultABC, 2342)
if err != nil {
return func() (string, error) {
return "", err
}
}
return func() (string, error) {
return sid.Generate()
}
}
func lookForIcon(imagesPath, imagesSuffix string) func(note *Entry) {
re := regexp.MustCompile(`^\[{2}(.*?)\]{2}`)
return func(note *Entry) {
str := note.text
res := re.FindStringSubmatch(str)
if len(res) > 0 {
note.icon = filepath.Join(imagesPath, strings.TrimSpace(res[1]))
if len(imagesSuffix) > 0 {
note.icon = fmt.Sprintf("%s.%s", note.icon, imagesSuffix)
}
note.text = re.ReplaceAllString(str, "")
}
}
}