/
tool.go
62 lines (52 loc) · 1.57 KB
/
tool.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
// Copyright 2013 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author: polaris polaris@studygolang.com
package util
import (
"fmt"
"math"
"regexp"
"sander/global"
"strings"
"github.com/polaris1119/goutils"
)
// Gravatar 获取头像
func Gravatar(avatar string, emailI interface{}, size uint16, isHttps bool) string {
gravatarDomain := "http://gravatar.com"
if isHttps {
gravatarDomain = "https://secure.gravatar.com"
}
cdnDomain := global.App.CanonicalCDN(isHttps)
if avatar != "" {
if strings.HasPrefix(avatar, "http") {
return fmt.Sprintf("%s&s=%d", avatar, size)
}
return fmt.Sprintf("%savatar/%s?imageView2/2/w/%d", cdnDomain, avatar, size)
}
email, ok := emailI.(string)
if !ok {
return fmt.Sprintf("%savatar/gopher28.png?imageView2/2/w/%d", cdnDomain, size)
}
return fmt.Sprintf("%s/avatar/%s?s=%d", gravatarDomain, goutils.Md5(email), size)
}
// Max .
func Max(x, y int) int {
return int(math.Max(float64(x), float64(y)))
}
// UMin 最小值,但不会小于0
func UMin(x, y int) int {
if x < 0 || y < 0 {
return 0
}
return int(math.Min(float64(x), float64(y)))
}
// EmbedWide 内嵌 Wide iframe 版
func EmbedWide(content string) string {
if !strings.Contains(content, "<iframe") {
return content
}
reg := regexp.MustCompile(`<iframe.*src=.*(https://wide\.b3log\.org/playground.*\.go).*/iframe>`)
return reg.ReplaceAllString(content, `<iframe src="$1?embed=true" width="100%" height="600"></iframe>`)
}