github.com/v2fly/tools@v0.100.0/godoc/static/gen.go (about)

     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  	"images/minus.gif",
    42  	"images/plus.gif",
    43  	"images/treeview-black-line.gif",
    44  	"images/treeview-black.gif",
    45  	"images/treeview-default-line.gif",
    46  	"images/treeview-default.gif",
    47  	"images/treeview-gray-line.gif",
    48  	"images/treeview-gray.gif",
    49  	"implements.html",
    50  	"jquery.js",
    51  	"jquery.treeview.css",
    52  	"jquery.treeview.edit.js",
    53  	"jquery.treeview.js",
    54  	"methodset.html",
    55  	"package.html",
    56  	"packageroot.html",
    57  	"play.js",
    58  	"playground.js",
    59  	"search.html",
    60  	"searchcode.html",
    61  	"searchdoc.html",
    62  	"searchtxt.html",
    63  	"style.css",
    64  }
    65  
    66  // Generate reads a set of files and returns a file buffer that declares
    67  // a map of string constants containing contents of the input files.
    68  func Generate() ([]byte, error) {
    69  	buf := new(bytes.Buffer)
    70  	fmt.Fprintf(buf, "%v\n\n%v\n\npackage static\n\n", license, warning)
    71  	fmt.Fprintf(buf, "var Files = map[string]string{\n")
    72  	for _, fn := range files {
    73  		b, err := ioutil.ReadFile(fn)
    74  		if err != nil {
    75  			return b, err
    76  		}
    77  		fmt.Fprintf(buf, "\t%q: ", fn)
    78  		appendQuote(buf, b)
    79  		fmt.Fprintf(buf, ",\n\n")
    80  	}
    81  	fmt.Fprintln(buf, "}")
    82  	return format.Source(buf.Bytes())
    83  }
    84  
    85  // appendQuote is like strconv.AppendQuote, but we avoid the latter
    86  // because it changes when Unicode evolves, breaking gen_test.go.
    87  func appendQuote(out *bytes.Buffer, data []byte) {
    88  	out.WriteByte('"')
    89  	for _, b := range data {
    90  		if b == '\\' || b == '"' {
    91  			out.WriteByte('\\')
    92  			out.WriteByte(b)
    93  		} else if b <= unicode.MaxASCII && unicode.IsPrint(rune(b)) && !unicode.IsSpace(rune(b)) {
    94  			out.WriteByte(b)
    95  		} else {
    96  			fmt.Fprintf(out, "\\x%02x", b)
    97  		}
    98  	}
    99  	out.WriteByte('"')
   100  }
   101  
   102  const warning = `// Code generated by "makestatic"; DO NOT EDIT.`
   103  
   104  const license = `// Copyright 2013 The Go Authors. All rights reserved.
   105  // Use of this source code is governed by a BSD-style
   106  // license that can be found in the LICENSE file.`