diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-12-09 02:18:25 -0500 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-12-09 02:18:25 -0500 |
commit | 9a1fe801e526ae1bc0a211d8a218aabcf985376d (patch) | |
tree | e40f92f1b34f81c60ffef084761b7d6cd4336aa2 /modules/base/template.go | |
parent | 6f71632e3ef6c0c818b3224e501fa66583108806 (diff) |
fix #711
Diffstat (limited to 'modules/base/template.go')
-rw-r--r-- | modules/base/template.go | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/modules/base/template.go b/modules/base/template.go index 446d01dd..462269aa 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -47,18 +47,23 @@ func ShortSha(sha1 string) string { return sha1 } -func ToUtf8WithErr(content []byte) (error, string) { +func DetectEncoding(content []byte) (string, error) { detector := chardet.NewTextDetector() result, err := detector.DetectBest(content) + return result.Charset, err +} + +func ToUtf8WithErr(content []byte) (error, string) { + charset, err := DetectEncoding(content) if err != nil { return err, "" } - if result.Charset == "utf8" { + if charset == "utf8" { return nil, string(content) } - decoder := mahonia.NewDecoder(result.Charset) + decoder := mahonia.NewDecoder(charset) if decoder != nil { return nil, decoder.ConvertString(string(content)) } |