Commit 5b5b42ea authored by Robert Griesemer's avatar Robert Griesemer

godoc: replace servePage's positional argument list

R=golang-dev, adg, bradfitz
CC=golang-dev
https://golang.org/cl/5869050
parent 373f1a95
...@@ -45,8 +45,8 @@ ...@@ -45,8 +45,8 @@
Do not delete this <div>. */}} Do not delete this <div>. */}}
<div id="nav"></div> <div id="nav"></div>
{{/* Content is HTML-escaped elsewhere */}} {{/* Body is HTML-escaped elsewhere */}}
{{printf "%s" .Content}} {{printf "%s" .Body}}
</div> </div>
......
...@@ -68,8 +68,11 @@ func codewalk(w http.ResponseWriter, r *http.Request) { ...@@ -68,8 +68,11 @@ func codewalk(w http.ResponseWriter, r *http.Request) {
return return
} }
b := applyTemplate(codewalkHTML, "codewalk", cw) servePage(w, Page{
servePage(w, cw.Title, "Codewalk: "+cw.Title, "", "", b) Title: "Codewalk: " + cw.Title,
Tabtitle: cw.Title,
Body: applyTemplate(codewalkHTML, "codewalk", cw),
})
} }
// A Codewalk represents a single codewalk read from an XML file. // A Codewalk represents a single codewalk read from an XML file.
...@@ -199,8 +202,10 @@ func codewalkDir(w http.ResponseWriter, r *http.Request, relpath, abspath string ...@@ -199,8 +202,10 @@ func codewalkDir(w http.ResponseWriter, r *http.Request, relpath, abspath string
} }
} }
b := applyTemplate(codewalkdirHTML, "codewalkdir", v) servePage(w, Page{
servePage(w, "", "Codewalks", "", "", b) Title: "Codewalks",
Body: applyTemplate(codewalkdirHTML, "codewalkdir", v),
})
} }
// codewalkFileprint serves requests with ?fileprint=f&lo=lo&hi=hi. // codewalkFileprint serves requests with ?fileprint=f&lo=lo&hi=hi.
......
...@@ -538,31 +538,26 @@ func readTemplates() { ...@@ -538,31 +538,26 @@ func readTemplates() {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Generic HTML wrapper // Generic HTML wrapper
func servePage(w http.ResponseWriter, tabtitle, title, subtitle, query string, content []byte) { // Page describes the contents of the top-level godoc webpage.
if tabtitle == "" { type Page struct {
tabtitle = title Title string
} Tabtitle string
d := struct { Subtitle string
Tabtitle string Query string
Title string Body []byte
Subtitle string
SearchBox bool // filled in by servePage
Query string SearchBox bool
Version string Version string
Menu []byte }
Content []byte
}{ func servePage(w http.ResponseWriter, page Page) {
tabtitle, if page.Tabtitle == "" {
title, page.Tabtitle = page.Title
subtitle, }
*indexEnabled, page.SearchBox = *indexEnabled
query, page.Version = runtime.Version()
runtime.Version(), if err := godocHTML.Execute(w, page); err != nil {
nil,
content,
}
if err := godocHTML.Execute(w, &d); err != nil {
log.Printf("godocHTML.Execute: %s", err) log.Printf("godocHTML.Execute: %s", err)
} }
} }
...@@ -627,7 +622,11 @@ func serveHTMLDoc(w http.ResponseWriter, r *http.Request, abspath, relpath strin ...@@ -627,7 +622,11 @@ func serveHTMLDoc(w http.ResponseWriter, r *http.Request, abspath, relpath strin
src = buf.Bytes() src = buf.Bytes()
} }
servePage(w, "", meta.Title, meta.Subtitle, "", src) servePage(w, Page{
Title: meta.Title,
Subtitle: meta.Subtitle,
Body: src,
})
} }
func applyTemplate(t *template.Template, name string, data interface{}) []byte { func applyTemplate(t *template.Template, name string, data interface{}) []byte {
...@@ -663,7 +662,11 @@ func serveTextFile(w http.ResponseWriter, r *http.Request, abspath, relpath, tit ...@@ -663,7 +662,11 @@ func serveTextFile(w http.ResponseWriter, r *http.Request, abspath, relpath, tit
FormatText(&buf, src, 1, pathpkg.Ext(abspath) == ".go", r.FormValue("h"), rangeSelection(r.FormValue("s"))) FormatText(&buf, src, 1, pathpkg.Ext(abspath) == ".go", r.FormValue("h"), rangeSelection(r.FormValue("s")))
buf.WriteString("</pre>") buf.WriteString("</pre>")
servePage(w, relpath, title+" "+relpath, "", "", buf.Bytes()) servePage(w, Page{
Title: title + " " + relpath,
Tabtitle: relpath,
Body: buf.Bytes(),
})
} }
func serveDirectory(w http.ResponseWriter, r *http.Request, abspath, relpath string) { func serveDirectory(w http.ResponseWriter, r *http.Request, abspath, relpath string) {
...@@ -677,8 +680,11 @@ func serveDirectory(w http.ResponseWriter, r *http.Request, abspath, relpath str ...@@ -677,8 +680,11 @@ func serveDirectory(w http.ResponseWriter, r *http.Request, abspath, relpath str
return return
} }
contents := applyTemplate(dirlistHTML, "dirlistHTML", list) servePage(w, Page{
servePage(w, relpath, "Directory "+relpath, "", "", contents) Title: "Directory " + relpath,
Tabtitle: relpath,
Body: applyTemplate(dirlistHTML, "dirlistHTML", list),
})
} }
func serveFile(w http.ResponseWriter, r *http.Request) { func serveFile(w http.ResponseWriter, r *http.Request) {
...@@ -1065,8 +1071,7 @@ func (h *docServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -1065,8 +1071,7 @@ func (h *docServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if mode&noHtml != 0 { if mode&noHtml != 0 {
contents := applyTemplate(packageText, "packageText", info) serveText(w, applyTemplate(packageText, "packageText", info))
serveText(w, contents)
return return
} }
...@@ -1103,8 +1108,12 @@ func (h *docServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -1103,8 +1108,12 @@ func (h *docServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tabtitle = "Commands" tabtitle = "Commands"
} }
contents := applyTemplate(packageHTML, "packageHTML", info) servePage(w, Page{
servePage(w, tabtitle, title, subtitle, "", contents) Title: title,
Tabtitle: tabtitle,
Subtitle: subtitle,
Body: applyTemplate(packageHTML, "packageHTML", info),
})
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -1181,8 +1190,7 @@ func search(w http.ResponseWriter, r *http.Request) { ...@@ -1181,8 +1190,7 @@ func search(w http.ResponseWriter, r *http.Request) {
result := lookup(query) result := lookup(query)
if getPageInfoMode(r)&noHtml != 0 { if getPageInfoMode(r)&noHtml != 0 {
contents := applyTemplate(searchText, "searchText", result) serveText(w, applyTemplate(searchText, "searchText", result))
serveText(w, contents)
return return
} }
...@@ -1193,8 +1201,12 @@ func search(w http.ResponseWriter, r *http.Request) { ...@@ -1193,8 +1201,12 @@ func search(w http.ResponseWriter, r *http.Request) {
title = fmt.Sprintf(`No results found for query %q`, query) title = fmt.Sprintf(`No results found for query %q`, query)
} }
contents := applyTemplate(searchHTML, "searchHTML", result) servePage(w, Page{
servePage(w, query, title, "", query, contents) Title: title,
Tabtitle: query,
Query: query,
Body: applyTemplate(searchHTML, "searchHTML", result),
})
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
......
...@@ -71,9 +71,12 @@ var ( ...@@ -71,9 +71,12 @@ var (
) )
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) { func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
servePage(w, relpath, "File "+relpath, "", "", contents) servePage(w, Page{
Title: "File " + relpath,
Subtitle: relpath,
Body: applyTemplate(errorHTML, "errorHTML", err), // err may contain an absolute path!
})
} }
func usage() { func usage() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment