mirror of
https://github.com/golang/go
synced 2024-11-18 16:54:43 -07:00
a28efa5d8c
The display of search results can now be changed. A slice of functions for displaying the results can now be provided. By default, three functions are provided to display documentation, source code, and textual results. This makes it possible to replace them with equivalents that, say, obtain search results from alternative source code search engines. R=bradfitz, adg CC=golang-codereviews https://golang.org/cl/43470043
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
// Copyright 2010 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// The /doc/codewalk/ tree is synthesized from codewalk descriptions,
|
|
// files named $GOROOT/doc/codewalk/*.xml.
|
|
// For an example and a description of the format, see
|
|
// http://golang.org/doc/codewalk/codewalk or run godoc -http=:6060
|
|
// and see http://localhost:6060/doc/codewalk/codewalk .
|
|
// That page is itself a codewalk; the source code for it is
|
|
// $GOROOT/doc/codewalk/codewalk.xml.
|
|
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"text/template"
|
|
|
|
"code.google.com/p/go.tools/godoc"
|
|
"code.google.com/p/go.tools/godoc/redirect"
|
|
"code.google.com/p/go.tools/godoc/vfs"
|
|
)
|
|
|
|
var (
|
|
pres *godoc.Presentation
|
|
fs = vfs.NameSpace{}
|
|
)
|
|
|
|
func registerHandlers(pres *godoc.Presentation) {
|
|
if pres == nil {
|
|
panic("nil Presentation")
|
|
}
|
|
http.HandleFunc("/doc/codewalk/", codewalk)
|
|
http.Handle("/doc/play/", pres.FileServer())
|
|
http.Handle("/robots.txt", pres.FileServer())
|
|
http.Handle("/", pres)
|
|
redirect.Register(nil)
|
|
}
|
|
|
|
func readTemplate(name string) *template.Template {
|
|
if pres == nil {
|
|
panic("no global Presentation set yet")
|
|
}
|
|
path := "lib/godoc/" + name
|
|
|
|
// use underlying file system fs to read the template file
|
|
// (cannot use template ParseFile functions directly)
|
|
data, err := vfs.ReadFile(fs, path)
|
|
if err != nil {
|
|
log.Fatal("readTemplate: ", err)
|
|
}
|
|
// be explicit with errors (for app engine use)
|
|
t, err := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
|
|
if err != nil {
|
|
log.Fatal("readTemplate: ", err)
|
|
}
|
|
return t
|
|
}
|
|
|
|
func readTemplates(p *godoc.Presentation, html bool) {
|
|
p.PackageText = readTemplate("package.txt")
|
|
p.SearchText = readTemplate("search.txt")
|
|
|
|
if html || p.HTMLMode {
|
|
codewalkHTML = readTemplate("codewalk.html")
|
|
codewalkdirHTML = readTemplate("codewalkdir.html")
|
|
p.DirlistHTML = readTemplate("dirlist.html")
|
|
p.ErrorHTML = readTemplate("error.html")
|
|
p.ExampleHTML = readTemplate("example.html")
|
|
p.GodocHTML = readTemplate("godoc.html")
|
|
p.PackageHTML = readTemplate("package.html")
|
|
p.SearchHTML = readTemplate("search.html")
|
|
p.SearchDocHTML = readTemplate("searchdoc.html")
|
|
p.SearchCodeHTML = readTemplate("searchcode.html")
|
|
p.SearchTxtHTML = readTemplate("searchtxt.html")
|
|
p.SearchDescXML = readTemplate("opensearch.xml")
|
|
}
|
|
}
|