gen.go 4.2 KB

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