Skip to content

Commit e8e4987

Browse files
committed
godoc: support for title and subtitle headers when serving .html docs
and use it to show version (date) of go spec Fixes #68. R=rsc CC=golang-dev, r https://golang.org/cl/848042
1 parent 7ecefdc commit e8e4987

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

doc/go_spec.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<!-- The Go Programming Language Specification -->
1+
<!-- title The Go Programming Language Specification -->
2+
<!-- subtitle Version of March 25, 2010 -->
23

34
<!--
45
Todo

doc/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ span.highlight {
198198
background-color: #ffffa0;
199199
}
200200

201+
span.subtitle {
202+
font-weight: bold;
203+
font-size: medium;
204+
}
205+
201206
/* same style as for gettingStarted */
202207
#menu {
203208
margin-top: 1.5em;

lib/godoc/godoc.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,18 @@
133133
</div>
134134
{.end}
135135

136-
<h1 id="generatedHeader">{Title|html-esc}</h1>
136+
{.section Title}
137+
<h1 id="generatedHeader">{@|html-esc}</h1>
138+
{.end}
139+
{.section Subtitle}
140+
<span class="subtitle">{@|html-esc}</span>
141+
{.end}
137142

143+
<p>
138144
<!-- The Table of Contents is automatically inserted in this <div>.
139145
Do not delete this <div>. -->
140146
<div id="nav"></div>
147+
</p>
141148

142149
<!-- Content is HTML-escaped elsewhere -->
143150
{Content}

src/cmd/godoc/godoc.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"log"
2020
"os"
2121
pathutil "path"
22+
"regexp"
2223
"runtime"
2324
"strings"
2425
"sync"
@@ -874,9 +875,10 @@ func readTemplates() {
874875
// ----------------------------------------------------------------------------
875876
// Generic HTML wrapper
876877

877-
func servePage(c *http.Conn, title, query string, content []byte) {
878+
func servePage(c *http.Conn, title, subtitle, query string, content []byte) {
878879
type Data struct {
879880
Title string
881+
Subtitle string
880882
PkgRoots []string
881883
Timestamp uint64 // int64 to be compatible with os.Dir.Mtime_ns
882884
Query string
@@ -888,6 +890,7 @@ func servePage(c *http.Conn, title, query string, content []byte) {
888890
_, ts := fsTree.get()
889891
d := Data{
890892
Title: title,
893+
Subtitle: subtitle,
891894
PkgRoots: fsMap.PrefixList(),
892895
Timestamp: uint64(ts) * 1e9, // timestamp in ns
893896
Query: query,
@@ -912,16 +915,16 @@ func serveText(c *http.Conn, text []byte) {
912915
// Files
913916

914917
var (
915-
tagBegin = []byte("<!--")
916-
tagEnd = []byte("-->")
918+
titleRx = regexp.MustCompile(`<!-- title ([^\-]*)-->`)
919+
subtitleRx = regexp.MustCompile(`<!-- subtitle ([^\-]*)-->`)
920+
firstCommentRx = regexp.MustCompile(`<!--([^\-]*)-->`)
917921
)
918922

919-
// commentText returns the text of the first HTML comment in src.
920-
func commentText(src []byte) (text string) {
921-
i := bytes.Index(src, tagBegin)
922-
j := bytes.Index(src, tagEnd)
923-
if i >= 0 && j >= i+len(tagBegin) {
924-
text = string(bytes.TrimSpace(src[i+len(tagBegin) : j]))
923+
924+
func extractString(src []byte, rx *regexp.Regexp) (s string) {
925+
m := rx.Execute(src)
926+
if len(m) >= 4 {
927+
s = strings.TrimSpace(string(src[m[2]:m[3]]))
925928
}
926929
return
927930
}
@@ -950,8 +953,15 @@ func serveHTMLDoc(c *http.Conn, r *http.Request, abspath, relpath string) {
950953
src = buf.Bytes()
951954
}
952955

953-
title := commentText(src)
954-
servePage(c, title, "", src)
956+
// get title and subtitle, if any
957+
title := extractString(src, titleRx)
958+
if title == "" {
959+
// no title found; try first comment for backward-compatibility
960+
title = extractString(src, firstCommentRx)
961+
}
962+
subtitle := extractString(src, subtitleRx)
963+
964+
servePage(c, title, subtitle, "", src)
955965
}
956966

957967

@@ -983,7 +993,7 @@ func serveGoSource(c *http.Conn, r *http.Request, abspath, relpath string) {
983993
info := &SourceInfo{buf.Bytes(), styler.mapping()}
984994

985995
contents := applyTemplate(sourceHTML, "sourceHTML", info)
986-
servePage(c, "Source file "+relpath, "", contents)
996+
servePage(c, "Source file "+relpath, "", "", contents)
987997
}
988998

989999

@@ -1056,7 +1066,7 @@ func serveTextFile(c *http.Conn, r *http.Request, abspath, relpath string) {
10561066
template.HTMLEscape(&buf, src)
10571067
fmt.Fprintln(&buf, "</pre>")
10581068

1059-
servePage(c, "Text file "+relpath, "", buf.Bytes())
1069+
servePage(c, "Text file "+relpath, "", "", buf.Bytes())
10601070
}
10611071

10621072

@@ -1079,7 +1089,7 @@ func serveDirectory(c *http.Conn, r *http.Request, abspath, relpath string) {
10791089
}
10801090

10811091
contents := applyTemplate(dirlistHTML, "dirlistHTML", list)
1082-
servePage(c, "Directory "+relpath, "", contents)
1092+
servePage(c, "Directory "+relpath, "", "", contents)
10831093
}
10841094

10851095

@@ -1326,7 +1336,7 @@ func (h *httpHandler) ServeHTTP(c *http.Conn, r *http.Request) {
13261336
}
13271337

13281338
contents := applyTemplate(packageHTML, "packageHTML", info)
1329-
servePage(c, title, "", contents)
1339+
servePage(c, title, "", "", contents)
13301340
}
13311341

13321342

@@ -1373,7 +1383,7 @@ func search(c *http.Conn, r *http.Request) {
13731383
}
13741384

13751385
contents := applyTemplate(searchHTML, "searchHTML", result)
1376-
servePage(c, title, query, contents)
1386+
servePage(c, title, "", query, contents)
13771387
}
13781388

13791389

src/cmd/godoc/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var (
6666

6767
func serveError(c *http.Conn, r *http.Request, relpath string, err os.Error) {
6868
contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
69-
servePage(c, "File "+relpath, "", contents)
69+
servePage(c, "File "+relpath, "", "", contents)
7070
}
7171

7272

0 commit comments

Comments
 (0)