/vendor/golang.org/x/tools/godoc/static/makestatic.go

https://github.com/twitchyliquid64/subnet · Go · 122 lines · 100 code · 9 blank · 13 comment · 12 complexity · 43accf21a5316e488f8c02378a295415 MD5 · raw file

  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. // +build ignore
  5. // Command makestatic reads a set of files and writes a Go source file to "static.go"
  6. // that declares a map of string constants containing contents of the input files.
  7. // It is intended to be invoked via "go generate" (directive in "gen.go").
  8. package main
  9. import (
  10. "bytes"
  11. "fmt"
  12. "go/format"
  13. "io/ioutil"
  14. "os"
  15. "unicode/utf8"
  16. )
  17. var files = []string{
  18. "analysis/call3.png",
  19. "analysis/call-eg.png",
  20. "analysis/callers1.png",
  21. "analysis/callers2.png",
  22. "analysis/chan1.png",
  23. "analysis/chan2a.png",
  24. "analysis/chan2b.png",
  25. "analysis/error1.png",
  26. "analysis/help.html",
  27. "analysis/ident-def.png",
  28. "analysis/ident-field.png",
  29. "analysis/ident-func.png",
  30. "analysis/ipcg-func.png",
  31. "analysis/ipcg-pkg.png",
  32. "analysis/typeinfo-pkg.png",
  33. "analysis/typeinfo-src.png",
  34. "callgraph.html",
  35. "codewalk.html",
  36. "codewalkdir.html",
  37. "dirlist.html",
  38. "error.html",
  39. "example.html",
  40. "godoc.html",
  41. "godocs.js",
  42. "images/minus.gif",
  43. "images/plus.gif",
  44. "images/treeview-black-line.gif",
  45. "images/treeview-black.gif",
  46. "images/treeview-default-line.gif",
  47. "images/treeview-default.gif",
  48. "images/treeview-gray-line.gif",
  49. "images/treeview-gray.gif",
  50. "implements.html",
  51. "jquery.js",
  52. "jquery.treeview.css",
  53. "jquery.treeview.edit.js",
  54. "jquery.treeview.js",
  55. "methodset.html",
  56. "opensearch.xml",
  57. "package.html",
  58. "package.txt",
  59. "play.js",
  60. "playground.js",
  61. "search.html",
  62. "search.txt",
  63. "searchcode.html",
  64. "searchdoc.html",
  65. "searchtxt.html",
  66. "style.css",
  67. }
  68. func main() {
  69. if err := makestatic(); err != nil {
  70. fmt.Fprintln(os.Stderr, err)
  71. os.Exit(1)
  72. }
  73. }
  74. func makestatic() error {
  75. f, err := os.Create("static.go")
  76. if err != nil {
  77. return err
  78. }
  79. defer f.Close()
  80. buf := new(bytes.Buffer)
  81. fmt.Fprintf(buf, "%v\npackage static\n\n", warning)
  82. fmt.Fprintf(buf, "var Files = map[string]string{\n")
  83. for _, fn := range files {
  84. b, err := ioutil.ReadFile(fn)
  85. if err != nil {
  86. return err
  87. }
  88. fmt.Fprintf(buf, "\t%q: ", fn)
  89. if utf8.Valid(b) {
  90. fmt.Fprintf(buf, "`%s`", sanitize(b))
  91. } else {
  92. fmt.Fprintf(buf, "%q", b)
  93. }
  94. fmt.Fprintln(buf, ",\n")
  95. }
  96. fmt.Fprintln(buf, "}")
  97. fmtbuf, err := format.Source(buf.Bytes())
  98. if err != nil {
  99. return err
  100. }
  101. return ioutil.WriteFile("static.go", fmtbuf, 0666)
  102. }
  103. // sanitize prepares a valid UTF-8 string as a raw string constant.
  104. func sanitize(b []byte) []byte {
  105. // Replace ` with `+"`"+`
  106. b = bytes.Replace(b, []byte("`"), []byte("`+\"`\"+`"), -1)
  107. // Replace BOM with `+"\xEF\xBB\xBF"+`
  108. // (A BOM is valid UTF-8 but not permitted in Go source files.
  109. // I wouldn't bother handling this, but for some insane reason
  110. // jquery.js has a BOM somewhere in the middle.)
  111. return bytes.Replace(b, []byte("\xEF\xBB\xBF"), []byte("`+\"\\xEF\\xBB\\xBF\"+`"), -1)
  112. }
  113. const warning = "// Code generated by \"makestatic\"; DO NOT EDIT\n"