1 | // Copyright 2014 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 | package static |
6 | |
7 | //go:generate go run makestatic.go |
8 | |
9 | import ( |
10 | "bytes" |
11 | "fmt" |
12 | "go/format" |
13 | "io/ioutil" |
14 | "unicode" |
15 | ) |
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 | "dirlist.html", |
36 | "error.html", |
37 | "example.html", |
38 | "favicon.ico", |
39 | "godoc.html", |
40 | "godocs.js", |
41 | "gopher/pkg.png", |
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 | "package.html", |
57 | "packageroot.html", |
58 | "play.js", |
59 | "playground.js", |
60 | "search.html", |
61 | "searchcode.html", |
62 | "searchdoc.html", |
63 | "searchtxt.html", |
64 | "style.css", |
65 | } |
66 | |
67 | // Generate reads a set of files and returns a file buffer that declares |
68 | // a map of string constants containing contents of the input files. |
69 | func Generate() ([]byte, error) { |
70 | buf := new(bytes.Buffer) |
71 | fmt.Fprintf(buf, "%v\n\n%v\n\npackage static\n\n", license, warning) |
72 | fmt.Fprintf(buf, "var Files = map[string]string{\n") |
73 | for _, fn := range files { |
74 | b, err := ioutil.ReadFile(fn) |
75 | if err != nil { |
76 | return b, err |
77 | } |
78 | fmt.Fprintf(buf, "\t%q: ", fn) |
79 | appendQuote(buf, b) |
80 | fmt.Fprintf(buf, ",\n\n") |
81 | } |
82 | fmt.Fprintln(buf, "}") |
83 | return format.Source(buf.Bytes()) |
84 | } |
85 | |
86 | // appendQuote is like strconv.AppendQuote, but we avoid the latter |
87 | // because it changes when Unicode evolves, breaking gen_test.go. |
88 | func appendQuote(out *bytes.Buffer, data []byte) { |
89 | out.WriteByte('"') |
90 | for _, b := range data { |
91 | if b == '\\' || b == '"' { |
92 | out.WriteByte('\\') |
93 | out.WriteByte(b) |
94 | } else if b <= unicode.MaxASCII && unicode.IsPrint(rune(b)) && !unicode.IsSpace(rune(b)) { |
95 | out.WriteByte(b) |
96 | } else { |
97 | fmt.Fprintf(out, "\\x%02x", b) |
98 | } |
99 | } |
100 | out.WriteByte('"') |
101 | } |
102 | |
103 | const warning = `// Code generated by "makestatic"; DO NOT EDIT.` |
104 | |
105 | const license = `// Copyright 2013 The Go Authors. All rights reserved. |
106 | // Use of this source code is governed by a BSD-style |
107 | // license that can be found in the LICENSE file.` |
108 |
Members