github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/_vendor/src/golang.org/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  	"bytes"
    14  	"fmt"
    15  	"go/format"
    16  	"io/ioutil"
    17  	"os"
    18  	"time"
    19  	"unicode/utf8"
    20  )
    21  
    22  var files = []string{
    23  	"analysis/call3.png",
    24  	"analysis/call-eg.png",
    25  	"analysis/callers1.png",
    26  	"analysis/callers2.png",
    27  	"analysis/chan1.png",
    28  	"analysis/chan2a.png",
    29  	"analysis/chan2b.png",
    30  	"analysis/error1.png",
    31  	"analysis/help.html",
    32  	"analysis/ident-def.png",
    33  	"analysis/ident-field.png",
    34  	"analysis/ident-func.png",
    35  	"analysis/ipcg-func.png",
    36  	"analysis/ipcg-pkg.png",
    37  	"analysis/typeinfo-pkg.png",
    38  	"analysis/typeinfo-src.png",
    39  	"callgraph.html",
    40  	"codewalk.html",
    41  	"codewalkdir.html",
    42  	"dirlist.html",
    43  	"error.html",
    44  	"example.html",
    45  	"godoc.html",
    46  	"godocs.js",
    47  	"images/minus.gif",
    48  	"images/plus.gif",
    49  	"images/treeview-black-line.gif",
    50  	"images/treeview-black.gif",
    51  	"images/treeview-default-line.gif",
    52  	"images/treeview-default.gif",
    53  	"images/treeview-gray-line.gif",
    54  	"images/treeview-gray.gif",
    55  	"implements.html",
    56  	"jquery.js",
    57  	"jquery.treeview.css",
    58  	"jquery.treeview.edit.js",
    59  	"jquery.treeview.js",
    60  	"methodset.html",
    61  	"opensearch.xml",
    62  	"package.html",
    63  	"packageroot.html",
    64  	"package.txt",
    65  	"play.js",
    66  	"playground.js",
    67  	"search.html",
    68  	"search.txt",
    69  	"searchcode.html",
    70  	"searchdoc.html",
    71  	"searchtxt.html",
    72  	"style.css",
    73  }
    74  
    75  func main() {
    76  	if err := makestatic(); err != nil {
    77  		fmt.Fprintln(os.Stderr, err)
    78  		os.Exit(1)
    79  	}
    80  }
    81  
    82  func makestatic() error {
    83  	f, err := os.Create("static.go")
    84  	if err != nil {
    85  		return err
    86  	}
    87  	defer f.Close()
    88  	buf := new(bytes.Buffer)
    89  	fmt.Fprintf(buf, "%v\n\n%v\n\npackage static\n\n", license, warning)
    90  	fmt.Fprintf(buf, "var Files = map[string]string{\n")
    91  	for _, fn := range files {
    92  		b, err := ioutil.ReadFile(fn)
    93  		if err != nil {
    94  			return err
    95  		}
    96  		fmt.Fprintf(buf, "\t%q: ", fn)
    97  		if utf8.Valid(b) {
    98  			fmt.Fprintf(buf, "`%s`", sanitize(b))
    99  		} else {
   100  			fmt.Fprintf(buf, "%q", b)
   101  		}
   102  		fmt.Fprintln(buf, ",\n")
   103  	}
   104  	fmt.Fprintln(buf, "}")
   105  	fmtbuf, err := format.Source(buf.Bytes())
   106  	if err != nil {
   107  		return err
   108  	}
   109  	return ioutil.WriteFile("static.go", fmtbuf, 0666)
   110  }
   111  
   112  // sanitize prepares a valid UTF-8 string as a raw string constant.
   113  func sanitize(b []byte) []byte {
   114  	// Replace ` with `+"`"+`
   115  	b = bytes.Replace(b, []byte("`"), []byte("`+\"`\"+`"), -1)
   116  
   117  	// Replace BOM with `+"\xEF\xBB\xBF"+`
   118  	// (A BOM is valid UTF-8 but not permitted in Go source files.
   119  	// I wouldn't bother handling this, but for some insane reason
   120  	// jquery.js has a BOM somewhere in the middle.)
   121  	return bytes.Replace(b, []byte("\xEF\xBB\xBF"), []byte("`+\"\\xEF\\xBB\\xBF\"+`"), -1)
   122  }
   123  
   124  const warning = `// Code generated by "makestatic"; DO NOT EDIT.`
   125  
   126  var license = fmt.Sprintf(`// Copyright %d The Go Authors. All rights reserved.
   127  // Use of this source code is governed by a BSD-style
   128  // license that can be found in the LICENSE file.`, time.Now().UTC().Year())