Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // +build ignore | 1 // +build ignore |
2 | 2 |
3 package main | |
4 | |
3 // Download http://encoding.spec.whatwg.org/encodings.json and use it to | 5 // Download http://encoding.spec.whatwg.org/encodings.json and use it to |
nigeltao
2013/11/18 04:16:18
I'd put this comment below the package clause, ins
| |
4 // generate encodinglist.go. | 6 // generate table.go. |
nigeltao
2013/11/18 04:16:18
I'd shorten encodinglist.go to table.go, similarly
| |
5 package main | |
6 | 7 |
7 import ( | 8 import ( |
8 "encoding/json" | 9 "encoding/json" |
9 "fmt" | 10 "fmt" |
10 "log" | 11 "log" |
11 "net/http" | 12 "net/http" |
12 "strings" | 13 "strings" |
13 ) | 14 ) |
14 | 15 |
15 type enc struct { | 16 type enc struct { |
16 Name string | 17 Name string |
17 Labels []string | 18 Labels []string |
18 } | 19 } |
19 | 20 |
20 type encGroup struct { | 21 type group struct { |
21 Encodings []enc | 22 Encodings []enc |
22 Heading string | 23 Heading string |
23 } | 24 } |
24 | 25 |
25 const specURL = "http://encoding.spec.whatwg.org/encodings.json" | 26 const specURL = "http://encoding.spec.whatwg.org/encodings.json" |
26 | 27 |
27 func main() { | 28 func main() { |
28 resp, err := http.Get(specURL) | 29 resp, err := http.Get(specURL) |
29 if err != nil { | 30 if err != nil { |
30 log.Fatalf("error fetching %s: %s", specURL, err) | 31 log.Fatalf("error fetching %s: %s", specURL, err) |
31 } | 32 } |
32 if resp.StatusCode != 200 { | 33 if resp.StatusCode != 200 { |
33 log.Fatalf("error fetching %s: HTTP status %s", specURL, resp.St atus) | 34 log.Fatalf("error fetching %s: HTTP status %s", specURL, resp.St atus) |
34 } | 35 } |
35 defer resp.Body.Close() | 36 defer resp.Body.Close() |
36 | 37 |
37 » var encodings []encGroup | 38 » var groups []group |
nigeltao
2013/11/18 04:16:18
It might read better if this was
var groups []grou
andybalholm
2013/11/18 17:01:28
Done.
| |
38 d := json.NewDecoder(resp.Body) | 39 d := json.NewDecoder(resp.Body) |
39 » err = d.Decode(&encodings) | 40 » err = d.Decode(&groups) |
40 if err != nil { | 41 if err != nil { |
41 log.Fatalf("error reading encodings.json: %s", err) | 42 log.Fatalf("error reading encodings.json: %s", err) |
42 } | 43 } |
43 | 44 |
44 » fmt.Println("// This file is automatically generated from", specURL) | 45 » fmt.Println("// generated by go run gen.go; DO NOT EDIT") |
nigeltao
2013/11/18 04:16:18
I'd just say
"// generated by go run gen.go; DO NO
andybalholm
2013/11/18 17:01:28
Done.
| |
45 » fmt.Println("// with the command go run gen.go | gofmt > encodinglist.go ") | |
46 fmt.Println() | 46 fmt.Println() |
47 fmt.Println("package charset") | 47 fmt.Println("package charset") |
48 fmt.Println() | 48 fmt.Println() |
49 | 49 |
50 fmt.Println("import (") | 50 fmt.Println("import (") |
51 fmt.Println(`"code.google.com/p/go.text/encoding"`) | 51 fmt.Println(`"code.google.com/p/go.text/encoding"`) |
52 for _, pkg := range []string{"charmap", "japanese", "korean", "simplifie dchinese", "traditionalchinese", "unicode"} { | 52 for _, pkg := range []string{"charmap", "japanese", "korean", "simplifie dchinese", "traditionalchinese", "unicode"} { |
53 fmt.Printf("\"code.google.com/p/go.text/encoding/%s\"\n", pkg) | 53 fmt.Printf("\"code.google.com/p/go.text/encoding/%s\"\n", pkg) |
54 } | 54 } |
55 fmt.Println(")") | 55 fmt.Println(")") |
56 fmt.Println() | 56 fmt.Println() |
57 | 57 |
58 fmt.Println("var encodings = map[string]encoding.Encoding{") | 58 fmt.Println("var encodings = map[string]encoding.Encoding{") |
59 » for _, grp := range encodings { | 59 » for _, g := range groups { |
nigeltao
2013/11/18 04:16:18
I'd write
for _, g := range groups {
andybalholm
2013/11/18 17:01:28
Done.
| |
60 » » for _, e := range grp.Encodings { | 60 » » for _, e := range g.Encodings { |
61 goName, ok := miscNames[e.Name] | 61 goName, ok := miscNames[e.Name] |
62 if !ok { | 62 if !ok { |
63 for k, v := range prefixes { | 63 for k, v := range prefixes { |
64 if strings.HasPrefix(e.Name, k) { | 64 if strings.HasPrefix(e.Name, k) { |
65 goName = v + e.Name[len(k):] | 65 goName = v + e.Name[len(k):] |
66 break | 66 break |
67 } | 67 } |
68 } | 68 } |
69 if goName == "" { | 69 if goName == "" { |
70 log.Fatalf("unrecognized encoding name: %s", e.Name) | 70 log.Fatalf("unrecognized encoding name: %s", e.Name) |
(...skipping 27 matching lines...) Expand all Loading... | |
98 "big5": "traditionalchinese.Big5", | 98 "big5": "traditionalchinese.Big5", |
99 "euc-jp": "japanese.EUCJP", | 99 "euc-jp": "japanese.EUCJP", |
100 "iso-2022-jp": "japanese.ISO2022JP", | 100 "iso-2022-jp": "japanese.ISO2022JP", |
101 "shift_jis": "japanese.ShiftJIS", | 101 "shift_jis": "japanese.ShiftJIS", |
102 "euc-kr": "korean.EUCKR", | 102 "euc-kr": "korean.EUCKR", |
103 "replacement": "replacementEncoding{}", | 103 "replacement": "replacementEncoding{}", |
104 "utf-16be": "unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)", | 104 "utf-16be": "unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)", |
105 "utf-16le": "unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM )", | 105 "utf-16le": "unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM )", |
106 "x-user-defined": "charmap.XUserDefined", | 106 "x-user-defined": "charmap.XUserDefined", |
107 } | 107 } |
LEFT | RIGHT |