예제 #1
0
파일: view.go 프로젝트: felipelovato/gogs
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))
}
예제 #2
0
func main() {
	flag.Parse()

	var r io.Reader = os.Stdin
	var w io.Writer = os.Stdout

	if *from != "utf-8" {
		decode := mahonia.NewDecoder(*from)
		if decode == nil {
			log.Fatalf("Could not create decoder for %s", *from)
		}
		r = decode.NewReader(r)
	}

	if *to != "utf-8" {
		encode := mahonia.NewEncoder(*to)
		if encode == nil {
			log.Fatalf("Could not create decoder for %s", *to)
		}
		w = encode.NewWriter(w)
	}

	io.Copy(w, r)
}