diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-08-23 20:24:31 +0800 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-08-23 20:24:31 +0800 |
commit | e0a2d23baf5b51041a19773909d1cb2aa209eac6 (patch) | |
tree | f117a5805b40a3e103eb1352f78ba63b9b513334 /routers | |
parent | fa60502a70de62bb83c2ba083de923d5d08bdbf9 (diff) | |
parent | 97fb62f51ece9353d620780fd13cb9c520413fb8 (diff) | |
download | gitea-e0a2d23baf5b51041a19773909d1cb2aa209eac6.tar.gz gitea-e0a2d23baf5b51041a19773909d1cb2aa209eac6.zip |
Merge branch 'dev' of github.com:gogits/gogs into dev
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/view.go | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/routers/repo/view.go b/routers/repo/view.go index 9562bb785b..bd1dcb8f99 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -6,6 +6,7 @@ package repo import ( "bytes" + "fmt" "io/ioutil" "path" "path/filepath" @@ -14,12 +15,30 @@ import ( "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/middleware" + + "code.google.com/p/mahonia" + "github.com/saintfish/chardet" ) const ( HOME base.TplName = "repo/home" ) +func toUtf8(content []byte) (error, string) { + detector := chardet.NewTextDetector() + result, err := detector.DetectBest(content) + if err != nil { + return err, "" + } + + if result.Charset == "utf8" { + return nil, string(content) + } + + decoder := mahonia.NewDecoder(result.Charset) + return nil, decoder.ConvertString(string(content)) +} + func Home(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Repo.Repository.Name @@ -98,7 +117,12 @@ func Home(ctx *middleware.Context) { if readmeExist { ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, "")) } else { - ctx.Data["FileContent"] = string(buf) + if err, content := toUtf8(buf); err != nil { + fmt.Println("transfer encode error:", err) + ctx.Data["FileContent"] = string(buf) + } else { + ctx.Data["FileContent"] = content + } } } } |