-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
79 lines (71 loc) · 1.75 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
package emoji
import (
"bytes"
"fmt"
"github.com/gomarkdown/markdown/ast"
)
// Node is a node containing an emoji
type Node struct {
ast.Leaf
}
var seen map[string]bool
func init() {
seen = make(map[string]bool)
}
// Parser is a Markdown parser hook to parse emojis.
// This operates as a pre-parser, because gomarkdown
// does not allow inline parser extensions.
func Parser(data []byte) (ast.Node, []byte, int) {
if seen[string(data)] {
// Already processed
return nil, nil, 0
}
if bytes.Index(data, []byte("class=\"emoji\"")) != -1 {
// Already processed
return nil, nil, 0
}
dataLen := len(data)
if dataLen <= 1 {
// Not long enough to be an emoji
return nil, nil, 0
}
if bytes.IndexByte(data, ':') == -1 {
// No emoji delimieters
return nil, nil, 0
}
// Translate emojis to HTML
resData := make([]byte, 0)
startIndex := bytes.IndexByte(data, ':')
resData = append(resData, data[0:startIndex]...)
for {
if startIndex >= len(data) {
// Done
break
}
endIndex := bytes.IndexByte(data[startIndex+1:], ':') + startIndex + 1
if endIndex > startIndex {
name := string(data[startIndex+1 : endIndex])
if isValidEmoji([]byte(name)) {
startIndex = endIndex + 1
url := fmt.Sprintf(`<img class="emoji" src="%s" alt=":%s:"></img>`, generateURL(name), name)
resData = append(resData, []byte(url)...)
} else {
resData = append(resData, data[startIndex:endIndex]...)
startIndex = endIndex
}
if startIndex == dataLen {
break
}
} else {
break
}
}
if startIndex < dataLen {
resData = append(resData, data[startIndex:]...)
}
if bytes.Index(resData, []byte("class=\"emoji\"")) == -1 {
// Processed with no changes
seen[string(resData)] = true
}
return &ast.Softbreak{}, resData, dataLen
}