camweb: Add /gw/ link; parse out titles from <h1>

* Make /gw/ a redirect to the following path in gitweb.
* Parse out the <title> from the first <h1>.  Little lame, but works.
This commit is contained in:
Brad Fitzpatrick 2011-01-28 11:17:08 -08:00
parent f80897a762
commit 8bb8371ba1
2 changed files with 16 additions and 5 deletions

View File

@ -26,12 +26,15 @@ import (
"log"
"os"
"path"
"regexp"
"strings"
"template"
)
const defaultAddr = ":31798" // default webserver address
var h1TitlePattern = regexp.MustCompile(`<h1>(.+)</h1>`)
var (
httpAddr = flag.String("http", defaultAddr, "HTTP service address (e.g., '"+defaultAddr+"')")
root = flag.String("root", "", "Website root (parent of 'static', 'content', and 'tmpl")
@ -146,6 +149,12 @@ func mainHandler(rw http.ResponseWriter, req *http.Request) {
return
}
if strings.HasPrefix(relPath, "gw/") {
path := relPath[3:]
http.Redirect(rw, req, "/code/?p=camlistore.git;f=" + path + ";hb=master", http.StatusFound)
return
}
absPath := path.Join(*root, "content", relPath)
fi, err := os.Lstat(absPath)
if err != nil {
@ -176,7 +185,13 @@ func serveFile(rw http.ResponseWriter, req *http.Request, relPath, absPath strin
serveError(rw, req, absPath, err)
return
}
servePage(rw, "", "", []byte(data))
title := ""
if m := h1TitlePattern.FindSubmatch(data); len(m) > 1 {
title = string(m[1])
}
servePage(rw, title, "", data)
}
type gitwebHandler struct {

View File

@ -24,10 +24,6 @@
<a href='http://code.google.com/p/camlistore/issues/list'>Bugs</a>
</div></div>
{.section Title}
<h1>{@|html-esc}</h1>
{.end}
<div class='content'>
<!-- Content is HTML-escaped elsewhere -->
{Content}