-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.regexp.match.replace.go
116 lines (110 loc) · 3.1 KB
/
url.regexp.match.replace.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
package util
import (
"bytes"
"fmt"
"html/template"
"regexp"
"strings"
)
const (
urlReg = `((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+`
)
func UrlRegMatchReplace(str string) (ret string) {
ret = str
compile := regexp.MustCompile(urlReg)
urlMap := make(map[string]string)
submatch := compile.FindAllSubmatch([]byte(ret), -1)
for _, m := range submatch {
url := string(m[0])
urlMap[url] = fmt.Sprintf("<a href=\"%s\">%s</a>", url, url)
}
// fmt.Println(urlMap)
for k, v := range urlMap {
ret = strings.ReplaceAll(ret, k, v)
}
// fmt.Println(ret)
return
}
func UrlRegMatchReplaceToMarkdown(str string) (ret string) {
ret = str
compile := regexp.MustCompile(urlReg)
urlMap := make(map[string]string)
submatch := compile.FindAllSubmatch([]byte(ret), -1)
for _, m := range submatch {
url := string(m[0])
urlMap[url] = fmt.Sprintf("[%s](%s)", url, url)
}
// fmt.Println(urlMap)
for k, v := range urlMap {
ret = strings.ReplaceAll(ret, k, v)
}
// fmt.Println(ret)
return
}
// 识别文案中链接,并转义,输出为TG支持的HTML格式文案
func UrlRegMatchReplaceToTGHTML(str string) (ret string, err error) {
ret = str
tplParseText := str
compile := regexp.MustCompile(urlReg)
urlMap := make(map[string]string)
submatch := compile.FindAllSubmatch([]byte(ret), -1)
for index, m := range submatch {
url := string(m[0])
urlKey := "url" + fmt.Sprintf("%d", index)
// fmt.Println("index", index, "url", url, "urlKey", urlKey)
urlMap[urlKey] = url
tplParseText = strings.ReplaceAll(tplParseText, url, fmt.Sprintf("<a href=\"{{ .%s }}\">{{ .%s }}</a>", urlKey, urlKey))
}
// fmt.Println("tplParseText", tplParseText)
var result bytes.Buffer
tpl, err := template.New("message").Parse(tplParseText)
if err != nil {
return ret, fmt.Errorf("template parsing failed: %w", err)
}
if err := tpl.Execute(&result, urlMap); err != nil {
return ret, fmt.Errorf("template execution failed: %w", err)
}
retHtml := result.String()
return retHtml, nil
}
// hanMap := make(map[string]string, 0)
// hanMap["汉//\\字"] = "http://han.com"
// hanMap["汉.df||字2"] = "http://han2.com"
// ret, err := util.UrlMapToTGHTML(hanMap)
//
// if err != nil {
// fmt.Println("出错了", err)
// } else {
//
// fmt.Println("ret", ret)
// }
//
// urlMap[title]url,转义,输出为TG支持的HTML格式文案
func UrlMapToTGHTML(urlMap map[string]string) (ret string, err error) {
ret = ""
tplParseText := ""
urlKeyMap := make(map[string]string)
for k, v := range urlMap {
vc := v
kc := k
kHash := "k" + MD5Hash(vc)
urlKeyMap[kHash] = kc
}
for k, v := range urlKeyMap {
vc := v
kc := k
tplParseText = tplParseText + fmt.Sprintf("<a href=\"%s\">{{ .%s }}</a>\n", urlMap[vc], kc)
}
tplParseText = strings.TrimSpace(tplParseText)
// fmt.Println("tplParseText", tplParseText)
var result bytes.Buffer
tpl, err := template.New("message").Parse(tplParseText)
if err != nil {
return ret, fmt.Errorf("template parsing failed: %w", err)
}
if err := tpl.Execute(&result, urlKeyMap); err != nil {
return ret, fmt.Errorf("template execution failed: %w", err)
}
retHtml := result.String()
return retHtml, nil
}