github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/encoding/htmlindex/gen.go (about) 1 // Copyright 2015 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 import ( 10 "bytes" 11 "encoding/json" 12 "fmt" 13 "log" 14 "strings" 15 16 "golang.org/x/text/internal/gen" 17 ) 18 19 type group struct { 20 Encodings []struct { 21 Labels []string 22 Name string 23 } 24 } 25 26 func main() { 27 gen.Init() 28 29 r := gen.Open("http://www.w3.org/TR", "w3", "encoding/indexes/encodings.json") 30 var groups []group 31 if err := json.NewDecoder(r).Decode(&groups); err != nil { 32 log.Fatalf("Error reading encodings.json: %v", err) 33 } 34 35 w := &bytes.Buffer{} 36 fmt.Fprintln(w, "type htmlEncoding byte") 37 fmt.Fprintln(w, "const (") 38 for i, g := range groups { 39 for _, e := range g.Encodings { 40 name := consts[e.Name] 41 if name == "" { 42 log.Fatalf("No const defined for %s.", e.Name) 43 } 44 if i == 0 { 45 fmt.Fprintf(w, "%s htmlEncoding = iota\n", name) 46 } else { 47 fmt.Fprintf(w, "%s\n", name) 48 } 49 } 50 } 51 fmt.Fprintln(w, "numEncodings") 52 fmt.Fprint(w, ")\n\n") 53 54 fmt.Fprintln(w, "var canonical = [numEncodings]string{") 55 for _, g := range groups { 56 for _, e := range g.Encodings { 57 fmt.Fprintf(w, "%q,\n", e.Name) 58 } 59 } 60 fmt.Fprint(w, "}\n\n") 61 62 fmt.Fprintln(w, "var nameMap = map[string]htmlEncoding{") 63 for _, g := range groups { 64 for _, e := range g.Encodings { 65 for _, l := range e.Labels { 66 fmt.Fprintf(w, "%q: %s,\n", l, consts[e.Name]) 67 } 68 } 69 } 70 fmt.Fprint(w, "}\n\n") 71 72 var tags []string 73 fmt.Fprintln(w, "var localeMap = []htmlEncoding{") 74 for _, loc := range locales { 75 tags = append(tags, loc.tag) 76 fmt.Fprintf(w, "%s, // %s \n", consts[loc.name], loc.tag) 77 } 78 fmt.Fprint(w, "}\n\n") 79 80 fmt.Fprintf(w, "const locales = %q\n", strings.Join(tags, " ")) 81 82 gen.WriteGoFile("tables.go", "htmlindex", w.Bytes()) 83 } 84 85 // consts maps canonical encoding name to internal constant. 86 var consts = map[string]string{ 87 "utf-8": "utf8", 88 "ibm866": "ibm866", 89 "iso-8859-2": "iso8859_2", 90 "iso-8859-3": "iso8859_3", 91 "iso-8859-4": "iso8859_4", 92 "iso-8859-5": "iso8859_5", 93 "iso-8859-6": "iso8859_6", 94 "iso-8859-7": "iso8859_7", 95 "iso-8859-8": "iso8859_8", 96 "iso-8859-8-i": "iso8859_8I", 97 "iso-8859-10": "iso8859_10", 98 "iso-8859-13": "iso8859_13", 99 "iso-8859-14": "iso8859_14", 100 "iso-8859-15": "iso8859_15", 101 "iso-8859-16": "iso8859_16", 102 "koi8-r": "koi8r", 103 "koi8-u": "koi8u", 104 "macintosh": "macintosh", 105 "windows-874": "windows874", 106 "windows-1250": "windows1250", 107 "windows-1251": "windows1251", 108 "windows-1252": "windows1252", 109 "windows-1253": "windows1253", 110 "windows-1254": "windows1254", 111 "windows-1255": "windows1255", 112 "windows-1256": "windows1256", 113 "windows-1257": "windows1257", 114 "windows-1258": "windows1258", 115 "x-mac-cyrillic": "macintoshCyrillic", 116 "gbk": "gbk", 117 "gb18030": "gb18030", 118 // "hz-gb-2312": "hzgb2312", // Was removed from WhatWG 119 "big5": "big5", 120 "euc-jp": "eucjp", 121 "iso-2022-jp": "iso2022jp", 122 "shift_jis": "shiftJIS", 123 "euc-kr": "euckr", 124 "replacement": "replacement", 125 "utf-16be": "utf16be", 126 "utf-16le": "utf16le", 127 "x-user-defined": "xUserDefined", 128 } 129 130 // locales is taken from 131 // https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm. 132 var locales = []struct{ tag, name string }{ 133 {"und", "windows-1252"}, // The default value. 134 {"ar", "windows-1256"}, 135 {"ba", "windows-1251"}, 136 {"be", "windows-1251"}, 137 {"bg", "windows-1251"}, 138 {"cs", "windows-1250"}, 139 {"el", "iso-8859-7"}, 140 {"et", "windows-1257"}, 141 {"fa", "windows-1256"}, 142 {"he", "windows-1255"}, 143 {"hr", "windows-1250"}, 144 {"hu", "iso-8859-2"}, 145 {"ja", "shift_jis"}, 146 {"kk", "windows-1251"}, 147 {"ko", "euc-kr"}, 148 {"ku", "windows-1254"}, 149 {"ky", "windows-1251"}, 150 {"lt", "windows-1257"}, 151 {"lv", "windows-1257"}, 152 {"mk", "windows-1251"}, 153 {"pl", "iso-8859-2"}, 154 {"ru", "windows-1251"}, 155 {"sah", "windows-1251"}, 156 {"sk", "windows-1250"}, 157 {"sl", "iso-8859-2"}, 158 {"sr", "windows-1251"}, 159 {"tg", "windows-1251"}, 160 {"th", "windows-874"}, 161 {"tr", "windows-1254"}, 162 {"tt", "windows-1251"}, 163 {"uk", "windows-1251"}, 164 {"vi", "windows-1258"}, 165 {"zh-hans", "gb18030"}, 166 {"zh-hant", "big5"}, 167 }