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.
13 "golang.org/x/tools/godoc/vfs/httpfs"
16 // SearchResultFunc functions return an HTML body for displaying search results.
17 type SearchResultFunc func(p *Presentation, result SearchResult) []byte
19 // Presentation generates output from a corpus.
20 type Presentation struct {
24 fileServer http.Handler
25 cmdHandler handlerServer
26 pkgHandler handlerServer
41 SearchDescXML *template.Template // If not nil, register a /opensearch.xml handler with this template.
43 // TabWidth optionally specifies the tab width.
50 // NotesRx optionally specifies a regexp to match
51 // notes to render in the output.
52 NotesRx *regexp.Regexp
54 // AdjustPageInfoMode optionally specifies a function to
55 // modify the PageInfoMode of a request. The default chosen
57 AdjustPageInfoMode func(req *http.Request, mode PageInfoMode) PageInfoMode
59 // URLForSrc optionally specifies a function that takes a source file and
60 // returns a URL for it.
61 // The source file argument has the form /src/<path>/<filename>.
62 URLForSrc func(src string) string
64 // URLForSrcPos optionally specifies a function to create a URL given a
65 // source file, a line from the source file (1-based), and low & high offset
66 // positions (0-based, bytes from beginning of file). Ideally, the returned
67 // URL will be for the specified line of the file, while the high & low
68 // positions will be used to highlight a section of the file.
69 // The source file argument has the form /src/<path>/<filename>.
70 URLForSrcPos func(src string, line, low, high int) string
72 // URLForSrcQuery optionally specifies a function to create a URL given a
73 // source file, a query string, and a line from the source file (1-based).
74 // The source file argument has the form /src/<path>/<filename>.
75 // The query argument will be escaped for the purposes of embedding in a URL
77 // Ideally, the returned URL will be for the specified line of the file with
78 // the query string highlighted.
79 URLForSrcQuery func(src, query string, line int) string
81 // SearchResults optionally specifies a list of functions returning an HTML
82 // body for displaying search results.
83 SearchResults []SearchResultFunc
85 // GoogleAnalytics optionally adds Google Analytics via the provided
86 // tracking ID to each page.
87 GoogleAnalytics string
89 initFuncMapOnce sync.Once
90 funcMap template.FuncMap
91 templateFuncs template.FuncMap
94 // NewPresentation returns a new Presentation from a corpus.
95 // It sets SearchResults to:
96 // [SearchResultDoc SearchResultCode SearchResultTxt].
97 func NewPresentation(c *Corpus) *Presentation {
103 mux: http.NewServeMux(),
104 fileServer: http.FileServer(httpfs.New(c.fs)),
108 SearchResults: []SearchResultFunc{
109 (*Presentation).SearchResultDoc,
110 (*Presentation).SearchResultCode,
111 (*Presentation).SearchResultTxt,
114 p.cmdHandler = handlerServer{
120 p.pkgHandler = handlerServer{
126 exclude: []string{"/src/cmd"},
128 p.cmdHandler.registerWithMux(p.mux)
129 p.pkgHandler.registerWithMux(p.mux)
130 p.mux.HandleFunc("/", p.ServeFile)
131 p.mux.HandleFunc("/search", p.HandleSearch)
132 if p.SearchDescXML != nil {
133 p.mux.HandleFunc("/opensearch.xml", p.serveSearchDesc)
138 func (p *Presentation) FileServer() http.Handler {
142 func (p *Presentation) ServeHTTP(w http.ResponseWriter, r *http.Request) {
143 p.mux.ServeHTTP(w, r)
146 func (p *Presentation) PkgFSRoot() string {
147 return p.pkgHandler.fsRoot
150 func (p *Presentation) CmdFSRoot() string {
151 return p.cmdHandler.fsRoot
154 // TODO(bradfitz): move this to be a method on Corpus. Just moving code around for now,
155 // but this doesn't feel right.
156 func (p *Presentation) GetPkgPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo {
157 return p.pkgHandler.GetPageInfo(abspath, relpath, mode, "", "")
160 // TODO(bradfitz): move this to be a method on Corpus. Just moving code around for now,
161 // but this doesn't feel right.
162 func (p *Presentation) GetCmdPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo {
163 return p.cmdHandler.GetPageInfo(abspath, relpath, mode, "", "")