github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/_vendor/src/golang.org/x/tools/godoc/pres.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  package godoc
     6  
     7  import (
     8  	"net/http"
     9  	"regexp"
    10  	"sync"
    11  	"text/template"
    12  
    13  	"golang.org/x/tools/godoc/vfs/httpfs"
    14  )
    15  
    16  // SearchResultFunc functions return an HTML body for displaying search results.
    17  type SearchResultFunc func(p *Presentation, result SearchResult) []byte
    18  
    19  // Presentation generates output from a corpus.
    20  type Presentation struct {
    21  	Corpus *Corpus
    22  
    23  	mux        *http.ServeMux
    24  	fileServer http.Handler
    25  	cmdHandler handlerServer
    26  	pkgHandler handlerServer
    27  
    28  	CallGraphHTML,
    29  	DirlistHTML,
    30  	ErrorHTML,
    31  	ExampleHTML,
    32  	GodocHTML,
    33  	ImplementsHTML,
    34  	MethodSetHTML,
    35  	PackageHTML,
    36  	PackageRootHTML,
    37  	PackageText,
    38  	SearchHTML,
    39  	SearchDocHTML,
    40  	SearchCodeHTML,
    41  	SearchTxtHTML,
    42  	SearchText,
    43  	SearchDescXML *template.Template
    44  
    45  	// TabWidth optionally specifies the tab width.
    46  	TabWidth int
    47  
    48  	ShowTimestamps bool
    49  	ShowPlayground bool
    50  	ShowExamples   bool
    51  	DeclLinks      bool
    52  
    53  	// SrcMode outputs source code instead of documentation in command-line mode.
    54  	SrcMode bool
    55  	// HTMLMode outputs HTML instead of plain text in command-line mode.
    56  	HTMLMode bool
    57  	// AllMode includes unexported identifiers in the output in command-line mode.
    58  	AllMode bool
    59  
    60  	// NotesRx optionally specifies a regexp to match
    61  	// notes to render in the output.
    62  	NotesRx *regexp.Regexp
    63  
    64  	// AdjustPageInfoMode optionally specifies a function to
    65  	// modify the PageInfoMode of a request. The default chosen
    66  	// value is provided.
    67  	AdjustPageInfoMode func(req *http.Request, mode PageInfoMode) PageInfoMode
    68  
    69  	// URLForSrc optionally specifies a function that takes a source file and
    70  	// returns a URL for it.
    71  	// The source file argument has the form /src/<path>/<filename>.
    72  	URLForSrc func(src string) string
    73  
    74  	// URLForSrcPos optionally specifies a function to create a URL given a
    75  	// source file, a line from the source file (1-based), and low & high offset
    76  	// positions (0-based, bytes from beginning of file). Ideally, the returned
    77  	// URL will be for the specified line of the file, while the high & low
    78  	// positions will be used to highlight a section of the file.
    79  	// The source file argument has the form /src/<path>/<filename>.
    80  	URLForSrcPos func(src string, line, low, high int) string
    81  
    82  	// URLForSrcQuery optionally specifies a function to create a URL given a
    83  	// source file, a query string, and a line from the source file (1-based).
    84  	// The source file argument has the form /src/<path>/<filename>.
    85  	// The query argument will be escaped for the purposes of embedding in a URL
    86  	// query parameter.
    87  	// Ideally, the returned URL will be for the specified line of the file with
    88  	// the query string highlighted.
    89  	URLForSrcQuery func(src, query string, line int) string
    90  
    91  	// SearchResults optionally specifies a list of functions returning an HTML
    92  	// body for displaying search results.
    93  	SearchResults []SearchResultFunc
    94  
    95  	initFuncMapOnce sync.Once
    96  	funcMap         template.FuncMap
    97  	templateFuncs   template.FuncMap
    98  }
    99  
   100  // NewPresentation returns a new Presentation from a corpus.
   101  // It sets SearchResults to:
   102  // [SearchResultDoc SearchResultCode SearchResultTxt].
   103  func NewPresentation(c *Corpus) *Presentation {
   104  	if c == nil {
   105  		panic("nil Corpus")
   106  	}
   107  	p := &Presentation{
   108  		Corpus:     c,
   109  		mux:        http.NewServeMux(),
   110  		fileServer: http.FileServer(httpfs.New(c.fs)),
   111  
   112  		TabWidth:     4,
   113  		ShowExamples: true,
   114  		DeclLinks:    true,
   115  		SearchResults: []SearchResultFunc{
   116  			(*Presentation).SearchResultDoc,
   117  			(*Presentation).SearchResultCode,
   118  			(*Presentation).SearchResultTxt,
   119  		},
   120  	}
   121  	p.cmdHandler = handlerServer{
   122  		p:       p,
   123  		c:       c,
   124  		pattern: "/cmd/",
   125  		fsRoot:  "/src",
   126  	}
   127  	p.pkgHandler = handlerServer{
   128  		p:           p,
   129  		c:           c,
   130  		pattern:     "/pkg/",
   131  		stripPrefix: "pkg/",
   132  		fsRoot:      "/src",
   133  		exclude:     []string{"/src/cmd"},
   134  	}
   135  	p.cmdHandler.registerWithMux(p.mux)
   136  	p.pkgHandler.registerWithMux(p.mux)
   137  	p.mux.HandleFunc("/", p.ServeFile)
   138  	p.mux.HandleFunc("/search", p.HandleSearch)
   139  	p.mux.HandleFunc("/opensearch.xml", p.serveSearchDesc)
   140  	return p
   141  }
   142  
   143  func (p *Presentation) FileServer() http.Handler {
   144  	return p.fileServer
   145  }
   146  
   147  func (p *Presentation) ServeHTTP(w http.ResponseWriter, r *http.Request) {
   148  	p.mux.ServeHTTP(w, r)
   149  }
   150  
   151  func (p *Presentation) PkgFSRoot() string {
   152  	return p.pkgHandler.fsRoot
   153  }
   154  
   155  func (p *Presentation) CmdFSRoot() string {
   156  	return p.cmdHandler.fsRoot
   157  }
   158  
   159  // TODO(bradfitz): move this to be a method on Corpus. Just moving code around for now,
   160  // but this doesn't feel right.
   161  func (p *Presentation) GetPkgPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo {
   162  	return p.pkgHandler.GetPageInfo(abspath, relpath, mode, "", "")
   163  }
   164  
   165  // TODO(bradfitz): move this to be a method on Corpus. Just moving code around for now,
   166  // but this doesn't feel right.
   167  func (p *Presentation) GetCmdPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo {
   168  	return p.cmdHandler.GetPageInfo(abspath, relpath, mode, "", "")
   169  }