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

     1  // Copyright 2009 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  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  )
    13  
    14  // Page describes the contents of the top-level godoc webpage.
    15  type Page struct {
    16  	Title    string
    17  	Tabtitle string
    18  	Subtitle string
    19  	SrcPath  string
    20  	Query    string
    21  	Body     []byte
    22  	TreeView bool // page needs to contain treeview related js and css
    23  
    24  	// filled in by ServePage
    25  	SearchBox       bool
    26  	Playground      bool
    27  	Version         string
    28  	GoogleAnalytics string
    29  }
    30  
    31  func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
    32  	if page.Tabtitle == "" {
    33  		page.Tabtitle = page.Title
    34  	}
    35  	page.SearchBox = p.Corpus.IndexEnabled
    36  	page.Playground = p.ShowPlayground
    37  	page.Version = runtime.Version()
    38  	page.GoogleAnalytics = p.GoogleAnalytics
    39  	applyTemplateToResponseWriter(w, p.GodocHTML, page)
    40  }
    41  
    42  func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
    43  	w.WriteHeader(http.StatusNotFound)
    44  	if perr, ok := err.(*os.PathError); ok {
    45  		rel, err := filepath.Rel(runtime.GOROOT(), perr.Path)
    46  		if err != nil {
    47  			perr.Path = "REDACTED"
    48  		} else {
    49  			perr.Path = filepath.Join("$GOROOT", rel)
    50  		}
    51  	}
    52  	p.ServePage(w, Page{
    53  		Title:           "File " + relpath,
    54  		Subtitle:        relpath,
    55  		Body:            applyTemplate(p.ErrorHTML, "errorHTML", err),
    56  		GoogleAnalytics: p.GoogleAnalytics,
    57  	})
    58  }