generated from probably-not/go-module-small
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
130 lines (112 loc) · 3.41 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
package slackparser
import (
"regexp"
"strconv"
"strings"
"github.com/probably-not/slackparser/sections"
)
var advFmtPattern = regexp.MustCompile(`<(.*?)>`)
func Parse(mrkdwn string) (*ParsedMessage, error) {
advFmts := advFmtPattern.FindAllStringSubmatchIndex(mrkdwn, -1)
if len(advFmts) == 0 {
// No advanced formatting sections (not including formatting markdown shit yet)
parsed := ParsedMessage{
originalText: mrkdwn,
sections: sections.Sections{
sections.NewTextSection(mrkdwn),
},
}
return &parsed, nil
}
messageSections := make(sections.Sections, 0)
startIndex := 0
for _, advMatch := range advFmts {
fullMatchIdxs := advMatch[0:2]
subMatchIdxs := advMatch[2:4]
// If the fullMatch index is greater than 0,
// then we need to create a simple text section
// between the startIndex (where the last match ended)
// and the full match index (where the current match starts).
if fullMatchIdxs[0] > 0 {
messageSections = append(messageSections, sections.NewTextSection(mrkdwn[startIndex:fullMatchIdxs[0]]))
}
section, err := parseAdvancedFormattingSection(mrkdwn[subMatchIdxs[0]:subMatchIdxs[1]])
if err != nil {
return nil, err
}
messageSections = append(messageSections, section)
startIndex = fullMatchIdxs[1]
}
if startIndex < len(mrkdwn) {
messageSections = append(messageSections, sections.NewTextSection(mrkdwn[startIndex:]))
}
parsed := ParsedMessage{
originalText: mrkdwn,
sections: messageSections,
}
return &parsed, nil
}
func parseAdvancedFormattingSection(extractedValue string) (sections.Section, error) {
if strings.HasPrefix(extractedValue, "#C") {
splitLabel := strings.Split(extractedValue, "|")
channelID := splitLabel[0][1:]
var label string
if len(splitLabel) == 2 {
label = splitLabel[1]
}
return sections.NewChannelLink(channelID, label), nil
}
if strings.HasPrefix(extractedValue, "@U") || strings.HasPrefix(extractedValue, "@W") {
splitLabel := strings.Split(extractedValue, "|")
userID := splitLabel[0][1:]
var label string
if len(splitLabel) == 2 {
label = splitLabel[1]
}
return sections.NewUserMention(userID, label), nil
}
if strings.HasPrefix(extractedValue, "!subteam^") {
splitLabel := strings.Split(extractedValue, "|")
userGroupID := splitLabel[0][9:]
var label string
if len(splitLabel) == 2 {
label = splitLabel[1]
}
return sections.NewUserGroupMention(userGroupID, label), nil
}
if strings.HasPrefix(extractedValue, "!date^") {
splitFallback := strings.Split(extractedValue, "|")
dateFmt := splitFallback[0][6:]
splitDateFmt := strings.Split(dateFmt, "^")
unix, err := strconv.ParseInt(splitDateFmt[0], 10, 64)
if err != nil {
return nil, err
}
format := splitDateFmt[1]
var optionalLink string
if len(splitDateFmt) == 3 {
optionalLink = splitDateFmt[2]
}
var fallback string
if len(splitFallback) == 2 {
fallback = splitFallback[1]
}
return sections.NewLocalizedDate(unix, format, optionalLink, fallback), nil
}
if strings.HasPrefix(extractedValue, "!") {
splitLabel := strings.Split(extractedValue, "|")
mention := splitLabel[0][1:]
var label string
if len(splitLabel) == 2 {
label = splitLabel[1]
}
return sections.NewSpecialMention(mention, label), nil
}
splitLabel := strings.Split(extractedValue, "|")
url := splitLabel[0]
var label string
if len(splitLabel) == 2 {
label = splitLabel[1]
}
return sections.NewUrlLink(url, label), nil
}