-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbadges.go
More file actions
199 lines (163 loc) · 5.36 KB
/
badges.go
File metadata and controls
199 lines (163 loc) · 5.36 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"fmt"
"net/url"
"regexp"
"strings"
)
// processBadges converts shields.io badge images into text representations
// This allows badges to be displayed in the terminal similar to how Grip displays them
func processBadges(markdown string, config *Config) string {
// Match shields.io badge images in markdown format
// Pattern: [](optional-link)
// First, find standalone badges: 
// Second, find linked badges: [](link-url)
result := markdown
// Pattern for linked badges: [](link)
linkedBadgePattern := regexp.MustCompile(`\[!\[[^\]]*\]\((https://img\.shields\.io/[^)]+)\)\]\(([^)]+)\)`)
// Pattern for standalone badges: 
standaloneBadgePattern := regexp.MustCompile(`!\[[^\]]*\]\((https://img\.shields\.io/[^)]+)\)`)
// Process linked badges first (to avoid matching them as standalone)
linkedMatches := linkedBadgePattern.FindAllStringSubmatchIndex(result, -1)
// Process in reverse order to avoid position shifts
for i := len(linkedMatches) - 1; i >= 0; i-- {
match := linkedMatches[i]
matchStart := match[0]
matchEnd := match[1]
badgeURLStart := match[2]
badgeURLEnd := match[3]
linkURLStart := match[4]
linkURLEnd := match[5]
badgeURL := result[badgeURLStart:badgeURLEnd]
linkURL := result[linkURLStart:linkURLEnd]
// Parse the badge to extract label and message
label, message, color := parseShieldsBadge(badgeURL)
// Create a text representation as a clickable link
// Format: [label: message](link)
textBadge := fmt.Sprintf("[%s](%s)", formatBadgeText(label, message, color, config), linkURL)
result = result[:matchStart] + textBadge + result[matchEnd:]
}
// Process standalone badges
standaloneMatches := standaloneBadgePattern.FindAllStringSubmatchIndex(result, -1)
for i := len(standaloneMatches) - 1; i >= 0; i-- {
match := standaloneMatches[i]
matchStart := match[0]
matchEnd := match[1]
badgeURLStart := match[2]
badgeURLEnd := match[3]
badgeURL := result[badgeURLStart:badgeURLEnd]
// Parse the badge to extract label and message
label, message, color := parseShieldsBadge(badgeURL)
// Create a text representation
// Format: [label: message]
textBadge := formatBadgeText(label, message, color, config)
result = result[:matchStart] + textBadge + result[matchEnd:]
}
return result
}
// parseShieldsBadge extracts label, message, and color from a shields.io badge URL
func parseShieldsBadge(badgeURL string) (label, message, color string) {
// Parse URL
parsedURL, err := url.Parse(badgeURL)
if err != nil {
return "badge", "", ""
}
// Get path and query
path := strings.TrimPrefix(parsedURL.Path, "/")
query := parsedURL.Query()
// Different shields.io URL patterns:
// 1. /badge/<label>-<message>-<color>
// 2. /github/license/<user>/<repo>
// 3. /github/stars/<user>/<repo>
// Check if it's a GitHub-specific badge
if strings.HasPrefix(path, "github/license/") {
parts := strings.Split(path, "/")
if len(parts) >= 4 {
// GitHub license badge
user := parts[2]
repo := parts[3]
// Remove .svg extension if present
repo = strings.TrimSuffix(repo, ".svg")
label = "license"
message = fmt.Sprintf("%s/%s", user, repo)
color = query.Get("color")
if color == "" {
color = "blue"
}
return
}
}
if strings.HasPrefix(path, "github/stars/") {
parts := strings.Split(path, "/")
if len(parts) >= 4 {
// GitHub stars badge
user := parts[2]
repo := parts[3]
label = "stars"
message = fmt.Sprintf("%s/%s", user, repo)
color = query.Get("color")
if color == "" {
color = "yellow"
}
return
}
}
// Static badge pattern: /badge/<label>-<message>-<color>
if strings.HasPrefix(path, "badge/") {
badgeInfo := strings.TrimPrefix(path, "badge/")
// The format is label-message-color, but labels and messages can contain dashes
// We need to find the last dash (color) and second-to-last dash (message)
parts := strings.Split(badgeInfo, "-")
if len(parts) >= 3 {
// Last part is color
color = parts[len(parts)-1]
// Second to last is message
message = parts[len(parts)-2]
// Everything else is label
label = strings.Join(parts[:len(parts)-2], "-")
} else if len(parts) == 2 {
label = parts[0]
message = parts[1]
color = ""
} else if len(parts) == 1 {
label = parts[0]
message = ""
color = ""
}
// URL decode the parts
label = urlDecode(label)
message = urlDecode(message)
return
}
// Fallback: use the path as label
label = path
message = ""
color = ""
return
}
// urlDecode decodes URL-encoded strings, handling special characters
func urlDecode(s string) string {
// Replace URL-encoded characters
s = strings.ReplaceAll(s, "%20", " ")
s = strings.ReplaceAll(s, "%2F", "/")
s = strings.ReplaceAll(s, "%2D", "-")
s = strings.ReplaceAll(s, "%5F", "_")
decoded, err := url.QueryUnescape(s)
if err != nil {
return s
}
return decoded
}
// formatBadgeText formats the badge as colored text
func formatBadgeText(label, message, color string, config *Config) string {
// Format: [label: message] or just [label] if no message
var text string
if message != "" && label != "" {
text = fmt.Sprintf("[%s: %s]", label, message)
} else if label != "" {
text = fmt.Sprintf("[%s]", label)
} else {
text = "[badge]"
}
return text
}