-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmarkProcessor.go
66 lines (58 loc) · 1.69 KB
/
bookmarkProcessor.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
package gobookmarks
import (
"strings"
)
type BookmarkEntry struct {
Url string
Name string
}
type BookmarkCategory struct {
Name string
Entries []*BookmarkEntry
}
type BookmarkColumn struct {
Categories []*BookmarkCategory
}
func PreprocessBookmarks(bookmarks string) []*BookmarkColumn {
lines := strings.Split(bookmarks, "\n")
var result = []*BookmarkColumn{{}}
var currentCategory *BookmarkCategory
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.EqualFold(line, "column") {
if currentCategory != nil {
result[len(result)-1].Categories = append(result[len(result)-1].Categories, currentCategory)
currentCategory = nil
}
result = append(result, &BookmarkColumn{})
continue
}
parts := strings.Fields(line)
if len(parts) == 0 {
continue
}
if len(parts) > 0 && strings.EqualFold(parts[0], "Category:") {
categoryName := strings.Join(parts[1:], " ")
if currentCategory == nil {
currentCategory = &BookmarkCategory{Name: categoryName}
} else if currentCategory.Name != "" {
result[len(result)-1].Categories = append(result[len(result)-1].Categories, currentCategory)
currentCategory = &BookmarkCategory{Name: categoryName}
} else {
currentCategory.Name = categoryName
}
} else if len(parts) > 0 && currentCategory != nil {
var entry BookmarkEntry
entry.Url = parts[0]
entry.Name = parts[0]
if len(parts) > 1 {
entry.Name = strings.Join(parts[1:], " ")
}
currentCategory.Entries = append(currentCategory.Entries, &entry)
}
}
if currentCategory != nil && currentCategory.Name != "" {
result[len(result)-1].Categories = append(result[len(result)-1].Categories, currentCategory)
}
return result
}