website: use copy of package.html instead of directly golang's

Change-Id: I4ba47ade6c3307929aac489ff8735c5cf774074f
This commit is contained in:
mpl 2013-03-16 00:26:21 +01:00
parent 0e52cee398
commit 8f8373fa76
3 changed files with 241 additions and 22 deletions

View File

@ -33,6 +33,7 @@ import (
"path/filepath"
"regexp"
"strings"
txttemplate "text/template"
"time"
)
@ -55,6 +56,7 @@ var (
buildbotBackend = flag.String("buildbot_backend", "", "Build bot status backend URL")
buildbotHost = flag.String("buildbot_host", "", "Hostname to map to the buildbot_backend. If an HTTP request with this hostname is received, it proxies to buildbot_backend.")
pageHtml, errorHtml *template.Template
packageHTML *txttemplate.Template
)
var fmap = template.FuncMap{
@ -158,6 +160,8 @@ func readTemplate(name string) *template.Template {
func readTemplates() {
pageHtml = readTemplate("page.html")
errorHtml = readTemplate("error.html")
// TODO(mpl): see about not using text template anymore?
packageHTML = readTextTemplate("package.html")
}
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
@ -270,7 +274,6 @@ func fixupGitwebFiles() {
func main() {
flag.Parse()
readTemplates()
if *root == "" {
var err error
@ -279,7 +282,7 @@ func main() {
log.Fatalf("Failed to getwd: %v", err)
}
}
readTemplates()
fixupGitwebFiles()
latestGits := filepath.Join(*root, "latestgits")

View File

@ -36,7 +36,6 @@ import (
pathpkg "path"
"path/filepath"
"regexp"
"runtime"
"strings"
"text/template"
"time"
@ -223,25 +222,6 @@ func srcLinkFunc(s string) string {
return pathpkg.Clean(s[idx+len(domainName):])
}
var packageHTML = readGodocTemplate("package.html")
func readGodocTemplate(name string) *template.Template {
path := runtime.GOROOT() + "/lib/godoc/" + name
// use underlying file system fs to read the template file
// (cannot use template ParseFile functions directly)
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("readTemplate: ", err)
}
// be explicit with errors (for app engine use)
t, err := template.New(name).Funcs(godocFmap).Parse(string(data))
if err != nil {
log.Fatal("readTemplate: ", err)
}
return t
}
func (pi *PageInfo) populateDirs(diskPath string, depth int) {
var dir *Directory
dir = newDirectory(diskPath, depth)
@ -254,6 +234,8 @@ func getPageInfo(pkgName, diskPath string) (pi PageInfo, err error) {
pkgName == pathpkg.Join(domainName, cmdPattern) {
pi.Dirname = diskPath
pi.populateDirs(diskPath, 2)
// TODO(mpl): trim down our now local package.html to avoid that,
// among other things.
// hack; setting PDoc so that we can keep using directly
// $GOROOT/lib/godoc/package.html, while avoiding the
// missing gopher png and the "ad" for the go dashboard.
@ -370,6 +352,19 @@ func (p *tconv) Write(data []byte) (n int, err error) {
return
}
func readTextTemplate(name string) *template.Template {
fileName := filepath.Join(*root, "tmpl", name)
data, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatalf("ReadFile %s: %v", fileName, err)
}
t, err := template.New(name).Funcs(godocFmap).Parse(string(data))
if err != nil {
log.Fatalf("%s: %v", fileName, err)
}
return t
}
func applyTextTemplate(t *template.Template, name string, data interface{}) []byte {
var buf bytes.Buffer
if err := t.Execute(&buf, data); err != nil {

221
website/tmpl/package.html Normal file
View File

@ -0,0 +1,221 @@
<!--
Copyright 2009 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.
-->
<!--
Note: Static (i.e., not template-generated) href and id
attributes start with "pkg-" to make it impossible for
them to conflict with generated attributes (some of which
correspond to Go identifiers).
-->
{{with .PDoc}}
{{if $.IsPkg}}
<div id="short-nav">
<dl>
<dd><code>import "{{html .ImportPath}}"</code></dd>
</dl>
<dl>
<dd><a href="#pkg-overview" class="overviewLink">Overview</a></dd>
<dd><a href="#pkg-index">Index</a></dd>
{{if $.Examples}}
<dd><a href="#pkg-examples">Examples</a></dd>
{{end}}
{{if $.PList}}
<dd><a href="#pkg-other-packages">Other packages</a></dd>
{{end}}
{{if $.Dirs}}
<dd><a href="#pkg-subdirectories">Subdirectories</a></dd>
{{end}}
</dl>
</div>
<!-- The package's Name is printed as title by the top-level template -->
<div id="pkg-overview" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
{{comment_html .Doc}}
</div>
</div>
{{example_html "" $.Examples $.FSet}}
<h2 id="pkg-index">Index</h2>
<!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
<div id="manual-nav">
<dl>
{{if .Consts}}
<dd><a href="#pkg-constants">Constants</a></dd>
{{end}}
{{if .Vars}}
<dd><a href="#pkg-variables">Variables</a></dd>
{{end}}
{{range .Funcs}}
{{$name_html := html .Name}}
<dd><a href="#{{$name_html}}">{{node_html .Decl $.FSet}}</a></dd>
{{end}}
{{range .Types}}
{{$tname_html := html .Name}}
<dd><a href="#{{$tname_html}}">type {{$tname_html}}</a></dd>
{{range .Funcs}}
{{$name_html := html .Name}}
<dd>&nbsp; &nbsp; <a href="#{{$name_html}}">{{node_html .Decl $.FSet}}</a></dd>
{{end}}
{{range .Methods}}
{{$name_html := html .Name}}
<dd>&nbsp; &nbsp; <a href="#{{$tname_html}}.{{$name_html}}">{{node_html .Decl $.FSet}}</a></dd>
{{end}}
{{end}}
{{if .Bugs}}
<dd><a href="#pkg-bugs">Bugs</a></dd>
{{end}}
</dl>
{{if $.Examples}}
<h4 id="pkg-examples">Examples</h4>
<dl>
{{range $.Examples}}
<dd><a class="exampleLink" href="#example_{{.Name}}">{{example_name .Name}}</a></dd>
{{end}}
</dl>
{{end}}
{{with .Filenames}}
<h4>Package files</h4>
<p>
<span style="font-size:90%">
{{range .}}
<a href="{{.|srcLink|html}}">{{.|filename|html}}</a>
{{end}}
</span>
</p>
{{end}}
{{with .Consts}}
<h2 id="pkg-constants">Constants</h2>
{{range .}}
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{end}}
{{end}}
{{with .Vars}}
<h2 id="pkg-variables">Variables</h2>
{{range .}}
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{end}}
{{end}}
{{range .Funcs}}
{{/* Name is a string - no need for FSet */}}
{{$name_html := html .Name}}
<h2 id="{{$name_html}}">func <a href="{{posLink_url .Decl $.FSet}}">{{$name_html}}</a></h2>
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{example_html .Name $.Examples $.FSet}}
{{end}}
{{range .Types}}
{{$tname := .Name}}
{{$tname_html := html .Name}}
<h2 id="{{$tname_html}}">type <a href="{{posLink_url .Decl $.FSet}}">{{$tname_html}}</a></h2>
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{range .Consts}}
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{end}}
{{range .Vars}}
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{end}}
{{example_html $tname $.Examples $.FSet}}
{{range .Funcs}}
{{$name_html := html .Name}}
<h3 id="{{$name_html}}">func <a href="{{posLink_url .Decl $.FSet}}">{{$name_html}}</a></h3>
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{example_html .Name $.Examples $.FSet}}
{{end}}
{{range .Methods}}
{{$name_html := html .Name}}
<h3 id="{{$tname_html}}.{{$name_html}}">func ({{html .Recv}}) <a href="{{posLink_url .Decl $.FSet}}">{{$name_html}}</a></h3>
<pre>{{node_html .Decl $.FSet}}</pre>
{{comment_html .Doc}}
{{$name := printf "%s_%s" $tname .Name}}
{{example_html $name $.Examples $.FSet}}
{{end}}
{{end}}
</div>
{{else}} {{/* not a package; is a command */}}
{{comment_html .Doc}}
{{end}}
{{with .Bugs}}
<h2 id="pkg-bugs">Bugs</h2>
{{range .}}
{{comment_html .}}
{{end}}
{{end}}
{{end}}
{{with .PAst}}
<pre>{{node_html . $.FSet}}</pre>
{{end}}
{{with .PList}}
<h2 id="pkg-other-packages">Other packages</h2>
<p>
{{/* PList entries are strings - no need for FSet */}}
{{range .}}
<a href="?p={{urlquery .}}">{{html .}}</a><br />
{{end}}
</p>
{{end}}
{{with .Dirs}}
{{/* DirList entries are numbers and strings - no need for FSet */}}
{{if $.PDoc}}
<h2 id="pkg-subdirectories">Subdirectories</h2>
{{else}}
<div class="pkgGopher">
<img class="gopher" src="/doc/gopher/pkg.png"/>
</div>
{{end}}
<table class="dir">
<tr>
<th>Name</th>
<th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
<th style="text-align: left; width: auto">Synopsis</th>
</tr>
{{if not $.DirFlat}}
<tr>
<td><a href="..">..</a></td>
</tr>
{{end}}
{{range .List}}
{{if $.DirFlat}}
{{if .HasPkg}}
<tr>
<td class="name"><a href="{{html .Path}}/">{{html .Path}}</a></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td style="width: auto">{{html .Synopsis}}</td>
</tr>
{{end}}
{{else}}
<tr>
<td class="name">{{repeat `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` .Depth}}<a href="{{html .Path}}/">{{html .Name}}</a></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td style="width: auto">{{html .Synopsis}}</td>
</tr>
{{end}}
{{end}}
</table>
{{if $.PDoc}}{{else}}
<p>Need more packages? Take a look at the <a href="http://godashboard.appspot.com/">Go Project Dashboard</a>.</p>
{{end}}
{{end}}