You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gen.go 4.4KB

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