forked from sundy-li/html2article
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chadef.go
67 lines (60 loc) · 1.45 KB
/
chadef.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
package html2article
import (
"bytes"
"io/ioutil"
"net/http"
"regexp"
"strings"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
var charsetRe = regexp.MustCompile(`<meta\s.*charset=(.*)>`)
func DefCode(header http.Header, html string) string {
contentType := strings.ToLower(header.Get("Content-Type"))
if strings.Contains(contentType, "charset") {
return getCharset(contentType)
}
// 通过html判断
matches := charsetRe.FindStringSubmatch(html)
if len(matches) > 1 {
return getCharset(strings.Trim(matches[1], `"`))
}
return "utf-8"
}
func getCharset(contentType string) string {
contentType = strings.ToLower(contentType)
if strings.Contains(contentType, "utf-8") {
return "utf-8"
}
if strings.Contains(contentType, "gbk") {
return "gbk"
}
if strings.Contains(contentType, "gb-18030") {
return "gb-18030"
}
if strings.Contains(contentType, "gb") {
return "gbk"
}
return "utf-8"
}
func DecodeHtml(header http.Header, word, src string) (dst string) {
typ := DefCode(header, src)
var encoder encoding.Encoding
switch typ {
case "utf-8":
return word
case "gbk":
encoder = simplifiedchinese.GBK
case "gb-18030":
encoder = simplifiedchinese.GB18030
default:
encoder = simplifiedchinese.GBK
}
data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(word)), encoder.NewDecoder()))
if err == nil {
dst = string(data)
}
dst = string(data)
return
}