-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
181 lines (142 loc) · 3.45 KB
/
parse.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"io"
"sort"
"strings"
"github.com/pkg/errors"
"golang.org/x/net/html"
)
func parse(r io.Reader) ([]*EmojiGroup, error) {
doc, err := html.Parse(r)
if err != nil {
return nil, errors.Wrap(err, "failed to parse HTML")
}
contentDiv := findContentDiv(doc)
if contentDiv == nil {
return nil, errors.New(`did not find <div id="content">`)
}
var groups []*EmojiGroup
for child := contentDiv.FirstChild; child != nil; child = child.NextSibling {
if !isEmojiList(child) {
continue
}
id, ok := getAttr(child, "id")
if !ok {
return nil, errors.Errorf("found emoji list without id attribute")
}
group, err := parseEmojiList(child)
if err != nil {
return nil, errors.Wrapf(err, "emoji list id=%q", id)
}
groups = append(groups, group)
}
return groups, nil
}
func findContentDiv(n *html.Node) *html.Node {
if n.Type == html.ElementNode && n.Data == "div" {
for _, attr := range n.Attr {
if attr.Key == "id" && attr.Val == "content" {
return n
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
content := findContentDiv(c)
if content != nil {
return content
}
}
return nil
}
func isEmojiList(n *html.Node) bool {
if n.Type != html.ElementNode || n.Data != "ul" {
return false
}
return hasClass(n, "emojis")
}
func parseEmojiList(n *html.Node) (*EmojiGroup, error) {
category, err := getEmojiListCategory(n)
if err != nil {
return nil, err
}
group := &EmojiGroup{Name: category}
for child := n.FirstChild; child != nil; child = child.NextSibling {
if child.Type != html.ElementNode || child.Data != "li" {
continue
}
emoji, err := parseEmojiListItem(child)
if err != nil {
return nil, err
}
group.Emojis = append(group.Emojis, emoji)
}
sort.Slice(group.Emojis, func(i, j int) bool {
return group.Emojis[i].Name < group.Emojis[j].Name
})
return group, nil
}
func getEmojiListCategory(n *html.Node) (string, error) {
for prev := n.PrevSibling; prev != nil; prev = prev.PrevSibling {
if prev.Type != html.ElementNode || prev.Data != "h2" {
continue
}
return prev.FirstChild.Data, nil
}
return "", errors.New("did not find h2 before emoji list")
}
func parseEmojiListItem(n *html.Node) (*Emoji, error) {
if n.Type != html.ElementNode || n.Data != "li" {
return nil, errors.New("expected emoji to be in an <li> element")
}
var div *html.Node
for child := n.FirstChild; child != nil; child = child.NextSibling {
if child.Type != html.ElementNode {
continue
}
div = child
break
}
if div == nil {
return nil, errors.New("expected div element")
}
return parseEmojiDiv(div)
}
func parseEmojiDiv(n *html.Node) (*Emoji, error) {
if n.Data != "div" {
return nil, errors.New("expected emoji to contain a <div> element")
}
var emoji *Emoji
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode || c.Data != "span" {
continue
}
if !hasClass(c, "name") {
continue
}
emoji = &Emoji{Name: c.FirstChild.Data}
}
if emoji == nil {
return nil, errors.New("did not find emoji span")
}
return emoji, nil
}
func getAttr(n *html.Node, name string) (string, bool) {
for _, attr := range n.Attr {
if attr.Key == name {
return attr.Val, true
}
}
return "", false
}
func hasClass(n *html.Node, wanted string) bool {
classes, ok := getAttr(n, "class")
if !ok {
return false
}
for _, actual := range strings.Split(classes, " ") {
if actual == wanted {
return true
}
}
return false
}