summaryrefslogtreecommitdiff
path: root/src/cmd/godoc/mapping.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-02-16 11:20:55 -0800
committerRobert Griesemer <gri@golang.org>2010-02-16 11:20:55 -0800
commiteaa18cf6078b210a8683fdf7c0533f1908f33d2b (patch)
tree696089ccc47c9bd9a84290641b86a6a72db76016 /src/cmd/godoc/mapping.go
parentf96c03e483aca8036176d436669ea99d25386e4a (diff)
downloadgolang-eaa18cf6078b210a8683fdf7c0533f1908f33d2b.tar.gz
godoc support for directories outside $GOROOT
Example use: godoc -path=/home/user1:/home/build/foo -http=:6666 will start a local godoc that maps urls starting with /pkg/user1 or /pkg/foo to the respective roots specified in the path. Missing: Handling of overlapping package directories, multiple packages per directory. R=rsc CC=golang-dev http://codereview.appspot.com/206078
Diffstat (limited to 'src/cmd/godoc/mapping.go')
-rw-r--r--src/cmd/godoc/mapping.go50
1 files changed, 45 insertions, 5 deletions
diff --git a/src/cmd/godoc/mapping.go b/src/cmd/godoc/mapping.go
index 1143a4bdc..ed2483d8b 100644
--- a/src/cmd/godoc/mapping.go
+++ b/src/cmd/godoc/mapping.go
@@ -27,13 +27,13 @@ import (
// until a valid mapping is found. For instance, for the mapping:
//
// user -> /home/user
-// public -> /home/user/public
+// public -> /home/user/public
// public -> /home/build/public
//
// the relative paths below are mapped to absolute paths as follows:
//
// user/foo -> /home/user/foo
-// public/net/rpc/file1.go -> /home/user/public/net/rpc/file1.go
+// public/net/rpc/file1.go -> /home/user/public/net/rpc/file1.go
//
// If there is no /home/user/public/net/rpc/file2.go, the next public
// mapping entry is used to map the relative path to:
@@ -43,7 +43,8 @@ import (
// (assuming that file exists).
//
type Mapping struct {
- list []mapping
+ list []mapping
+ prefixes []string
}
@@ -71,7 +72,7 @@ type mapping struct {
// leads to the following mapping:
//
// user -> /home/user
-// public -> /home/build/public
+// public -> /home/build/public
//
func (m *Mapping) Init(paths string) {
cwd, _ := os.Getwd() // ignore errors
@@ -105,7 +106,7 @@ func (m *Mapping) Init(paths string) {
// add mapping if it is new
if i >= n {
_, prefix := pathutil.Split(path)
- list[i] = mapping{prefix, path}
+ list[n] = mapping{prefix, path}
n++
}
}
@@ -118,6 +119,45 @@ func (m *Mapping) Init(paths string) {
func (m *Mapping) IsEmpty() bool { return len(m.list) == 0 }
+// PrefixList returns a list of all prefixes, with duplicates removed.
+// For instance, for the mapping:
+//
+// user -> /home/user
+// public -> /home/user/public
+// public -> /home/build/public
+//
+// the prefix list is:
+//
+// user, public
+//
+func (m *Mapping) PrefixList() []string {
+ // compute the list lazily
+ if m.prefixes == nil {
+ list := make([]string, len(m.list))
+ n := 0 // nuber of prefixes
+
+ for _, e := range m.list {
+ // check if prefix exists already
+ var i int
+ for i = 0; i < n; i++ {
+ if e.prefix == list[i] {
+ break
+ }
+ }
+
+ // add prefix if it is new
+ if i >= n {
+ list[n] = e.prefix
+ n++
+ }
+ }
+ m.prefixes = list[0:n]
+ }
+
+ return m.prefixes
+}
+
+
// Fprint prints the mapping.
func (m *Mapping) Fprint(w io.Writer) {
for _, e := range m.list {