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