github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/html/charset/gen.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 package main 8 9 // Download http://encoding.spec.whatwg.org/encodings.json and use it to 10 // generate table.go. 11 12 import ( 13 "encoding/json" 14 "fmt" 15 "log" 16 "net/http" 17 "strings" 18 ) 19 20 type enc struct { 21 Name string 22 Labels []string 23 } 24 25 type group struct { 26 Encodings []enc 27 Heading string 28 } 29 30 const specURL = "http://encoding.spec.whatwg.org/encodings.json" 31 32 func main() { 33 resp, err := http.Get(specURL) 34 if err != nil { 35 log.Fatalf("error fetching %s: %s", specURL, err) 36 } 37 if resp.StatusCode != 200 { 38 log.Fatalf("error fetching %s: HTTP status %s", specURL, resp.Status) 39 } 40 defer resp.Body.Close() 41 42 var groups []group 43 d := json.NewDecoder(resp.Body) 44 err = d.Decode(&groups) 45 if err != nil { 46 log.Fatalf("error reading encodings.json: %s", err) 47 } 48 49 fmt.Println("// generated by go run gen.go; DO NOT EDIT") 50 fmt.Println() 51 fmt.Println("package charset") 52 fmt.Println() 53 54 fmt.Println("import (") 55 fmt.Println(`"golang.org/x/text/encoding"`) 56 for _, pkg := range []string{"charmap", "japanese", "korean", "simplifiedchinese", "traditionalchinese", "unicode"} { 57 fmt.Printf("\"golang.org/x/text/encoding/%s\"\n", pkg) 58 } 59 fmt.Println(")") 60 fmt.Println() 61 62 fmt.Println("var encodings = map[string]struct{e encoding.Encoding; name string} {") 63 for _, g := range groups { 64 for _, e := range g.Encodings { 65 goName, ok := miscNames[e.Name] 66 if !ok { 67 for k, v := range prefixes { 68 if strings.HasPrefix(e.Name, k) { 69 goName = v + e.Name[len(k):] 70 break 71 } 72 } 73 if goName == "" { 74 log.Fatalf("unrecognized encoding name: %s", e.Name) 75 } 76 } 77 78 for _, label := range e.Labels { 79 fmt.Printf("%q: {%s, %q},\n", label, goName, e.Name) 80 } 81 } 82 } 83 fmt.Println("}") 84 } 85 86 var prefixes = map[string]string{ 87 "iso-8859-": "charmap.ISO8859_", 88 "windows-": "charmap.Windows", 89 } 90 91 var miscNames = map[string]string{ 92 "utf-8": "encoding.Nop", 93 "ibm866": "charmap.CodePage866", 94 "iso-8859-8-i": "charmap.ISO8859_8", 95 "koi8-r": "charmap.KOI8R", 96 "koi8-u": "charmap.KOI8U", 97 "macintosh": "charmap.Macintosh", 98 "x-mac-cyrillic": "charmap.MacintoshCyrillic", 99 "gbk": "simplifiedchinese.GBK", 100 "gb18030": "simplifiedchinese.GB18030", 101 "hz-gb-2312": "simplifiedchinese.HZGB2312", 102 "big5": "traditionalchinese.Big5", 103 "euc-jp": "japanese.EUCJP", 104 "iso-2022-jp": "japanese.ISO2022JP", 105 "shift_jis": "japanese.ShiftJIS", 106 "euc-kr": "korean.EUCKR", 107 "replacement": "encoding.Replacement", 108 "utf-16be": "unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)", 109 "utf-16le": "unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)", 110 "x-user-defined": "charmap.XUserDefined", 111 }