2013-07-18 22:02:03 -06:00
|
|
|
// 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 (
|
2013-07-29 22:23:23 -06:00
|
|
|
"log"
|
2013-07-18 22:02:03 -06:00
|
|
|
"net/http"
|
2013-07-29 22:23:23 -06:00
|
|
|
"text/template"
|
2013-07-18 22:02:03 -06:00
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/godoc"
|
|
|
|
"golang.org/x/tools/godoc/redirect"
|
|
|
|
"golang.org/x/tools/godoc/vfs"
|
2013-07-29 22:23:23 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pres *godoc.Presentation
|
|
|
|
fs = vfs.NameSpace{}
|
2013-07-18 22:02:03 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2014-01-30 05:07:12 -07:00
|
|
|
http.Handle("/pkg/C/", redirect.Handler("/cmd/cgo/"))
|
2013-10-01 21:13:34 -06:00
|
|
|
redirect.Register(nil)
|
2013-07-18 22:02:03 -06:00
|
|
|
}
|
2013-07-29 22:23:23 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-08-19 23:29:56 -06:00
|
|
|
func readTemplates(p *godoc.Presentation, html bool) {
|
2013-07-29 22:23:23 -06:00
|
|
|
p.PackageText = readTemplate("package.txt")
|
|
|
|
p.SearchText = readTemplate("search.txt")
|
2013-08-19 23:29:56 -06:00
|
|
|
|
2013-11-20 07:56:10 -07:00
|
|
|
if html || p.HTMLMode {
|
2013-08-19 23:29:56 -06:00
|
|
|
codewalkHTML = readTemplate("codewalk.html")
|
|
|
|
codewalkdirHTML = readTemplate("codewalkdir.html")
|
2014-03-14 16:58:22 -06:00
|
|
|
p.CallGraphHTML = readTemplate("callgraph.html")
|
2013-08-19 23:29:56 -06:00
|
|
|
p.DirlistHTML = readTemplate("dirlist.html")
|
|
|
|
p.ErrorHTML = readTemplate("error.html")
|
|
|
|
p.ExampleHTML = readTemplate("example.html")
|
|
|
|
p.GodocHTML = readTemplate("godoc.html")
|
2014-03-14 16:58:22 -06:00
|
|
|
p.ImplementsHTML = readTemplate("implements.html")
|
|
|
|
p.MethodSetHTML = readTemplate("methodset.html")
|
2013-08-19 23:29:56 -06:00
|
|
|
p.PackageHTML = readTemplate("package.html")
|
|
|
|
p.SearchHTML = readTemplate("search.html")
|
2014-01-06 07:51:01 -07:00
|
|
|
p.SearchDocHTML = readTemplate("searchdoc.html")
|
|
|
|
p.SearchCodeHTML = readTemplate("searchcode.html")
|
|
|
|
p.SearchTxtHTML = readTemplate("searchtxt.html")
|
2013-08-19 23:29:56 -06:00
|
|
|
p.SearchDescXML = readTemplate("opensearch.xml")
|
|
|
|
}
|
2013-07-29 22:23:23 -06:00
|
|
|
}
|