@@ -19,6 +19,7 @@ import (
19
19
"log"
20
20
"os"
21
21
pathutil "path"
22
+ "regexp"
22
23
"runtime"
23
24
"strings"
24
25
"sync"
@@ -874,9 +875,10 @@ func readTemplates() {
874
875
// ----------------------------------------------------------------------------
875
876
// Generic HTML wrapper
876
877
877
- func servePage (c * http.Conn , title , query string , content []byte ) {
878
+ func servePage (c * http.Conn , title , subtitle , query string , content []byte ) {
878
879
type Data struct {
879
880
Title string
881
+ Subtitle string
880
882
PkgRoots []string
881
883
Timestamp uint64 // int64 to be compatible with os.Dir.Mtime_ns
882
884
Query string
@@ -888,6 +890,7 @@ func servePage(c *http.Conn, title, query string, content []byte) {
888
890
_ , ts := fsTree .get ()
889
891
d := Data {
890
892
Title : title ,
893
+ Subtitle : subtitle ,
891
894
PkgRoots : fsMap .PrefixList (),
892
895
Timestamp : uint64 (ts ) * 1e9 , // timestamp in ns
893
896
Query : query ,
@@ -912,16 +915,16 @@ func serveText(c *http.Conn, text []byte) {
912
915
// Files
913
916
914
917
var (
915
- tagBegin = []byte ("<!--" )
916
- tagEnd = []byte ("-->" )
918
+ titleRx = regexp .MustCompile (`<!-- title ([^\-]*)-->` )
919
+ subtitleRx = regexp .MustCompile (`<!-- subtitle ([^\-]*)-->` )
920
+ firstCommentRx = regexp .MustCompile (`<!--([^\-]*)-->` )
917
921
)
918
922
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 ]]))
925
928
}
926
929
return
927
930
}
@@ -950,8 +953,15 @@ func serveHTMLDoc(c *http.Conn, r *http.Request, abspath, relpath string) {
950
953
src = buf .Bytes ()
951
954
}
952
955
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 )
955
965
}
956
966
957
967
@@ -983,7 +993,7 @@ func serveGoSource(c *http.Conn, r *http.Request, abspath, relpath string) {
983
993
info := & SourceInfo {buf .Bytes (), styler .mapping ()}
984
994
985
995
contents := applyTemplate (sourceHTML , "sourceHTML" , info )
986
- servePage (c , "Source file " + relpath , "" , contents )
996
+ servePage (c , "Source file " + relpath , "" , "" , contents )
987
997
}
988
998
989
999
@@ -1056,7 +1066,7 @@ func serveTextFile(c *http.Conn, r *http.Request, abspath, relpath string) {
1056
1066
template .HTMLEscape (& buf , src )
1057
1067
fmt .Fprintln (& buf , "</pre>" )
1058
1068
1059
- servePage (c , "Text file " + relpath , "" , buf .Bytes ())
1069
+ servePage (c , "Text file " + relpath , "" , "" , buf .Bytes ())
1060
1070
}
1061
1071
1062
1072
@@ -1079,7 +1089,7 @@ func serveDirectory(c *http.Conn, r *http.Request, abspath, relpath string) {
1079
1089
}
1080
1090
1081
1091
contents := applyTemplate (dirlistHTML , "dirlistHTML" , list )
1082
- servePage (c , "Directory " + relpath , "" , contents )
1092
+ servePage (c , "Directory " + relpath , "" , "" , contents )
1083
1093
}
1084
1094
1085
1095
@@ -1326,7 +1336,7 @@ func (h *httpHandler) ServeHTTP(c *http.Conn, r *http.Request) {
1326
1336
}
1327
1337
1328
1338
contents := applyTemplate (packageHTML , "packageHTML" , info )
1329
- servePage (c , title , "" , contents )
1339
+ servePage (c , title , "" , "" , contents )
1330
1340
}
1331
1341
1332
1342
@@ -1373,7 +1383,7 @@ func search(c *http.Conn, r *http.Request) {
1373
1383
}
1374
1384
1375
1385
contents := applyTemplate (searchHTML , "searchHTML" , result )
1376
- servePage (c , title , query , contents )
1386
+ servePage (c , title , "" , query , contents )
1377
1387
}
1378
1388
1379
1389
0 commit comments