aboutsummaryrefslogtreecommitdiff
path: root/modules/base/template.go
diff options
context:
space:
mode:
author无闻 <u@gogs.io>2014-12-22 05:30:49 -0500
committer无闻 <u@gogs.io>2014-12-22 05:30:49 -0500
commite193005c66e3c61593efd2d44005efbca0fab33a (patch)
treeb5e51343456338eeea89d09bb24d93e7142f5d99 /modules/base/template.go
parentebbe6177a91e9eb6b4001342728ff099cafd5d65 (diff)
parentfff8109567f8296f81954dbf6280eb3049bb3db8 (diff)
Merge pull request #773 from phsmit/golang_x_text_encoding
Golang x text encoding
Diffstat (limited to 'modules/base/template.go')
-rw-r--r--modules/base/template.go27
1 files changed, 19 insertions, 8 deletions
diff --git a/modules/base/template.go b/modules/base/template.go
index 462269aa..9107f3e1 100644
--- a/modules/base/template.go
+++ b/modules/base/template.go
@@ -7,14 +7,15 @@ package base
import (
"container/list"
"encoding/json"
- "errors"
"fmt"
"html/template"
"runtime"
"strings"
"time"
- "github.com/gogits/gogs/modules/mahonia"
+ "golang.org/x/net/html/charset"
+ "golang.org/x/text/transform"
+
"github.com/gogits/gogs/modules/setting"
"github.com/saintfish/chardet"
)
@@ -54,20 +55,30 @@ func DetectEncoding(content []byte) (string, error) {
}
func ToUtf8WithErr(content []byte) (error, string) {
- charset, err := DetectEncoding(content)
+ charsetLabel, err := DetectEncoding(content)
if err != nil {
return err, ""
}
- if charset == "utf8" {
+ if charsetLabel == "utf8" {
return nil, string(content)
}
- decoder := mahonia.NewDecoder(charset)
- if decoder != nil {
- return nil, decoder.ConvertString(string(content))
+ encoding, _ := charset.Lookup(charsetLabel)
+
+ if encoding == nil {
+ return fmt.Errorf("unknow char decoder %s", charsetLabel), string(content)
}
- return errors.New("unknow char decoder"), string(content)
+
+ result, n, err := transform.String(encoding.NewDecoder(), string(content))
+
+ // If there is an error, we concatenate the nicely decoded part and the
+ // original left over. This way we won't loose data.
+ if err != nil {
+ result = result + string(content[n:])
+ }
+
+ return err, result
}
func ToUtf8(content string) string {