2013-02-22 23:58:56 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2013 The Perkeep Authors.
|
2013-02-22 23:58:56 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-02-23 21:38:58 +00:00
|
|
|
// This is a hacked-up version of godoc.
|
|
|
|
|
2013-02-22 23:58:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-02-23 21:38:58 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2013-02-22 23:58:56 +00:00
|
|
|
"fmt"
|
2013-02-23 21:38:58 +00:00
|
|
|
"go/ast"
|
2013-02-22 23:58:56 +00:00
|
|
|
"go/build"
|
|
|
|
"go/doc"
|
|
|
|
"go/parser"
|
2013-02-23 21:38:58 +00:00
|
|
|
"go/printer"
|
2013-02-22 23:58:56 +00:00
|
|
|
"go/token"
|
2013-02-23 21:38:58 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-02-22 23:58:56 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2013-02-23 21:38:58 +00:00
|
|
|
pathpkg "path"
|
2013-02-22 23:58:56 +00:00
|
|
|
"path/filepath"
|
2013-02-23 21:38:58 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
2013-02-22 23:58:56 +00:00
|
|
|
)
|
|
|
|
|
2013-03-14 00:42:56 +00:00
|
|
|
const (
|
2013-03-16 00:16:40 +00:00
|
|
|
cmdPattern = "/cmd/"
|
|
|
|
fileembedPattern = "fileembed.go"
|
2018-01-12 19:21:18 +00:00
|
|
|
tabwidth = 4
|
2013-03-14 00:42:56 +00:00
|
|
|
)
|
2013-03-11 23:49:58 +00:00
|
|
|
|
2017-12-11 22:01:04 +00:00
|
|
|
var (
|
2018-01-05 23:54:26 +00:00
|
|
|
domainName string
|
2017-12-11 22:01:04 +00:00
|
|
|
)
|
2013-02-22 23:58:56 +00:00
|
|
|
|
2013-02-23 21:38:58 +00:00
|
|
|
type PageInfo struct {
|
|
|
|
Dirname string // directory containing the package
|
|
|
|
Err error // error or nil
|
|
|
|
|
|
|
|
// package info
|
|
|
|
FSet *token.FileSet // nil if no package documentation
|
2013-03-18 15:08:10 +00:00
|
|
|
PDoc *doc.Package // nil if no package documentation
|
2013-02-23 21:38:58 +00:00
|
|
|
Examples []*doc.Example // nil if no example code
|
|
|
|
PAst *ast.File // nil if no AST with package exports
|
2013-03-11 23:49:58 +00:00
|
|
|
IsPkg bool // true for pkg, false for cmd
|
2013-02-23 21:38:58 +00:00
|
|
|
|
|
|
|
// directory info
|
|
|
|
Dirs *DirList // nil if no directory information
|
|
|
|
DirTime time.Time // directory time stamp
|
|
|
|
DirFlat bool // if set, show directory in a flat (non-indented) manner
|
2013-03-11 23:49:58 +00:00
|
|
|
PList []string // list of package names found
|
2013-02-23 21:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// godocFmap describes the template functions installed with all godoc templates.
|
|
|
|
// Convention: template function names ending in "_html" or "_url" produce
|
|
|
|
// HTML- or URL-escaped strings; all other function results may
|
|
|
|
// require explicit escaping in the template.
|
|
|
|
var godocFmap = template.FuncMap{
|
|
|
|
// various helpers
|
|
|
|
"filename": filenameFunc,
|
|
|
|
"repeat": strings.Repeat,
|
|
|
|
|
|
|
|
// accss to FileInfos (directory listings)
|
|
|
|
"fileInfoName": fileInfoNameFunc,
|
|
|
|
"fileInfoTime": fileInfoTimeFunc,
|
|
|
|
|
|
|
|
// access to search result information
|
|
|
|
//"infoKind_html": infoKind_htmlFunc,
|
|
|
|
//"infoLine": infoLineFunc,
|
|
|
|
//"infoSnippet_html": infoSnippet_htmlFunc,
|
|
|
|
|
|
|
|
// formatting of AST nodes
|
|
|
|
"node": nodeFunc,
|
|
|
|
"node_html": node_htmlFunc,
|
|
|
|
"comment_html": comment_htmlFunc,
|
|
|
|
//"comment_text": comment_textFunc,
|
|
|
|
|
|
|
|
// support for URL attributes
|
|
|
|
"srcLink": srcLinkFunc,
|
|
|
|
"posLink_url": posLink_urlFunc,
|
|
|
|
|
|
|
|
// formatting of Examples
|
|
|
|
"example_html": example_htmlFunc,
|
|
|
|
"example_name": example_nameFunc,
|
|
|
|
"example_suffix": example_suffixFunc,
|
|
|
|
}
|
|
|
|
|
|
|
|
func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.FileSet) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func example_nameFunc(s string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func example_suffixFunc(name string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func filenameFunc(path string) string {
|
|
|
|
_, localname := pathpkg.Split(path)
|
|
|
|
return localname
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileInfoNameFunc(fi os.FileInfo) string {
|
|
|
|
name := fi.Name()
|
|
|
|
if fi.IsDir() {
|
|
|
|
name += "/"
|
2013-02-22 23:58:56 +00:00
|
|
|
}
|
2013-02-23 21:38:58 +00:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileInfoTimeFunc(fi os.FileInfo) string {
|
|
|
|
if t := fi.ModTime(); t.Unix() != 0 {
|
|
|
|
return t.Local().String()
|
|
|
|
}
|
|
|
|
return "" // don't return epoch if time is obviously not set
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write an AST node to w.
|
|
|
|
func writeNode(w io.Writer, fset *token.FileSet, x interface{}) {
|
|
|
|
// convert trailing tabs into spaces using a tconv filter
|
|
|
|
// to ensure a good outcome in most browsers (there may still
|
|
|
|
// be tabs in comments and strings, but converting those into
|
|
|
|
// the right number of spaces is much harder)
|
|
|
|
//
|
|
|
|
// TODO(gri) rethink printer flags - perhaps tconv can be eliminated
|
|
|
|
// with an another printer mode (which is more efficiently
|
|
|
|
// implemented in the printer than here with another layer)
|
|
|
|
mode := printer.TabIndent | printer.UseSpaces
|
|
|
|
err := (&printer.Config{Mode: mode, Tabwidth: tabwidth}).Fprint(&tconv{output: w}, fset, x)
|
2013-02-22 23:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2013-02-23 21:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeFunc(node interface{}, fset *token.FileSet) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
writeNode(&buf, fset, node)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func node_htmlFunc(node interface{}, fset *token.FileSet) string {
|
|
|
|
var buf1 bytes.Buffer
|
|
|
|
writeNode(&buf1, fset, node)
|
|
|
|
var buf2 bytes.Buffer
|
|
|
|
FormatText(&buf2, buf1.Bytes(), -1, true, "", nil)
|
|
|
|
return buf2.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func comment_htmlFunc(comment string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
// TODO(gri) Provide list of words (e.g. function parameters)
|
|
|
|
// to be emphasized by ToHTML.
|
|
|
|
doc.ToHTML(&buf, comment, nil) // does html-escaping
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func posLink_urlFunc(node ast.Node, fset *token.FileSet) string {
|
|
|
|
var relpath string
|
|
|
|
var line int
|
|
|
|
var low, high int // selection
|
|
|
|
|
|
|
|
if p := node.Pos(); p.IsValid() {
|
|
|
|
pos := fset.Position(p)
|
2018-01-05 23:54:26 +00:00
|
|
|
idx := strings.LastIndex(pos.Filename, domainName)
|
|
|
|
if idx == -1 {
|
|
|
|
log.Fatalf("No \"%s\" in path to file %s", domainName, pos.Filename)
|
2013-03-11 23:49:58 +00:00
|
|
|
}
|
|
|
|
relpath = pathpkg.Clean(pos.Filename[idx+len(domainName):])
|
2013-02-23 21:38:58 +00:00
|
|
|
line = pos.Line
|
|
|
|
low = pos.Offset
|
|
|
|
}
|
|
|
|
if p := node.End(); p.IsValid() {
|
|
|
|
high = fset.Position(p).Offset
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
template.HTMLEscape(&buf, []byte(relpath))
|
|
|
|
// selection ranges are of form "s=low:high"
|
|
|
|
if low < high {
|
|
|
|
fmt.Fprintf(&buf, "?s=%d:%d", low, high) // no need for URL escaping
|
|
|
|
// if we have a selection, position the page
|
|
|
|
// such that the selection is a bit below the top
|
|
|
|
line -= 10
|
|
|
|
if line < 1 {
|
|
|
|
line = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// line id's in html-printed source are of the
|
|
|
|
// form "L%d" where %d stands for the line number
|
|
|
|
if line > 0 {
|
|
|
|
fmt.Fprintf(&buf, "#L%d", line) // no need for URL escaping
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func srcLinkFunc(s string) string {
|
2018-01-05 23:54:26 +00:00
|
|
|
idx := strings.LastIndex(s, domainName)
|
|
|
|
if idx == -1 {
|
|
|
|
log.Fatalf("No \"%s\" in path to file %s", domainName, s)
|
2013-03-11 23:49:58 +00:00
|
|
|
}
|
|
|
|
return pathpkg.Clean(s[idx+len(domainName):])
|
2013-02-23 21:38:58 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 00:42:56 +00:00
|
|
|
func (pi *PageInfo) populateDirs(diskPath string, depth int) {
|
|
|
|
var dir *Directory
|
|
|
|
dir = newDirectory(diskPath, depth)
|
|
|
|
pi.Dirs = dir.listing(true)
|
|
|
|
pi.DirTime = time.Now()
|
|
|
|
}
|
|
|
|
|
2018-01-05 23:54:26 +00:00
|
|
|
func getPageInfo(pkgName, diskPath string) (pi PageInfo, err error) {
|
2013-02-23 21:38:58 +00:00
|
|
|
bpkg, err := build.ImportDir(diskPath, 0)
|
|
|
|
if err != nil {
|
2013-07-09 20:50:10 +00:00
|
|
|
if _, ok := err.(*build.NoGoError); ok {
|
|
|
|
pi.populateDirs(diskPath, -1)
|
|
|
|
return pi, nil
|
|
|
|
}
|
2013-02-22 23:58:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inSet := make(map[string]bool)
|
|
|
|
for _, name := range bpkg.GoFiles {
|
2013-03-16 00:16:40 +00:00
|
|
|
if name == fileembedPattern {
|
|
|
|
continue
|
|
|
|
}
|
2013-02-22 23:58:56 +00:00
|
|
|
inSet[filepath.Base(name)] = true
|
|
|
|
}
|
|
|
|
|
2013-02-23 21:38:58 +00:00
|
|
|
pi.FSet = token.NewFileSet()
|
2013-02-22 23:58:56 +00:00
|
|
|
filter := func(fi os.FileInfo) bool {
|
|
|
|
return inSet[fi.Name()]
|
|
|
|
}
|
2013-03-11 23:49:58 +00:00
|
|
|
aPkgMap, err := parser.ParseDir(pi.FSet, diskPath, filter, parser.ParseComments)
|
2013-02-22 23:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-02-23 21:38:58 +00:00
|
|
|
aPkg := aPkgMap[pathpkg.Base(pkgName)]
|
2013-02-22 23:58:56 +00:00
|
|
|
if aPkg == nil {
|
|
|
|
for _, v := range aPkgMap {
|
|
|
|
aPkg = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if aPkg == nil {
|
2013-02-23 21:38:58 +00:00
|
|
|
err = errors.New("no apkg found?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pi.Dirname = diskPath
|
|
|
|
pi.PDoc = doc.New(aPkg, pkgName, 0)
|
2018-01-06 00:41:39 +00:00
|
|
|
pi.IsPkg = pi.PDoc.Name != "main"
|
2013-03-14 00:42:56 +00:00
|
|
|
|
|
|
|
// get directory information
|
2013-07-09 20:50:10 +00:00
|
|
|
pi.populateDirs(diskPath, -1)
|
2013-02-23 21:38:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
indenting = iota
|
|
|
|
collecting
|
|
|
|
)
|
|
|
|
|
|
|
|
// A tconv is an io.Writer filter for converting leading tabs into spaces.
|
|
|
|
type tconv struct {
|
|
|
|
output io.Writer
|
|
|
|
state int // indenting or collecting
|
|
|
|
indent int // valid if state == indenting
|
|
|
|
}
|
|
|
|
|
|
|
|
var spaces = []byte(" ") // 32 spaces seems like a good number
|
|
|
|
|
|
|
|
func (p *tconv) writeIndent() (err error) {
|
|
|
|
i := p.indent
|
|
|
|
for i >= len(spaces) {
|
|
|
|
i -= len(spaces)
|
|
|
|
if _, err = p.output.Write(spaces); err != nil {
|
2013-02-22 23:58:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 21:38:58 +00:00
|
|
|
// i < len(spaces)
|
|
|
|
if i > 0 {
|
|
|
|
_, err = p.output.Write(spaces[0:i])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *tconv) Write(data []byte) (n int, err error) {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pos := 0 // valid if p.state == collecting
|
|
|
|
var b byte
|
|
|
|
for n, b = range data {
|
|
|
|
switch p.state {
|
|
|
|
case indenting:
|
|
|
|
switch b {
|
|
|
|
case '\t':
|
|
|
|
p.indent += tabwidth
|
|
|
|
case '\n':
|
|
|
|
p.indent = 0
|
|
|
|
if _, err = p.output.Write(data[n : n+1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case ' ':
|
|
|
|
p.indent++
|
|
|
|
default:
|
|
|
|
p.state = collecting
|
|
|
|
pos = n
|
|
|
|
if err = p.writeIndent(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case collecting:
|
|
|
|
if b == '\n' {
|
|
|
|
p.state = indenting
|
|
|
|
p.indent = 0
|
|
|
|
if _, err = p.output.Write(data[pos : n+1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n = len(data)
|
|
|
|
if pos < n && p.state == collecting {
|
|
|
|
_, err = p.output.Write(data[pos:])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-15 23:26:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-02-23 21:38:58 +00:00
|
|
|
func applyTextTemplate(t *template.Template, name string, data interface{}) []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := t.Execute(&buf, data); err != nil {
|
|
|
|
log.Printf("%s.Execute: %s", name, err)
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2013-03-11 23:49:58 +00:00
|
|
|
func serveTextFile(w http.ResponseWriter, r *http.Request, abspath, relpath, title string) {
|
|
|
|
src, err := ioutil.ReadFile(abspath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ReadFile: %s", err)
|
|
|
|
serveError(w, r, relpath, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("<pre>")
|
|
|
|
FormatText(&buf, src, 1, pathpkg.Ext(abspath) == ".go", r.FormValue("h"), rangeSelection(r.FormValue("s")))
|
|
|
|
buf.WriteString("</pre>")
|
2017-12-12 16:32:58 +00:00
|
|
|
servePage(w, r, pageParams{
|
2016-06-17 22:43:38 +00:00
|
|
|
title: title,
|
|
|
|
content: buf.Bytes(),
|
|
|
|
})
|
2013-03-11 23:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 00:41:39 +00:00
|
|
|
func serveGodoc(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
suffix := r.URL.Path
|
|
|
|
diskPath := filepath.Join(*root, "..", suffix)
|
2013-02-23 21:38:58 +00:00
|
|
|
|
2018-01-06 00:41:39 +00:00
|
|
|
fi, err := os.Stat(diskPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-02-23 21:38:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 00:41:39 +00:00
|
|
|
switch {
|
|
|
|
case isGoFile(fi):
|
2013-03-11 23:49:58 +00:00
|
|
|
serveTextFile(w, r, diskPath, suffix, "Source file")
|
2018-01-06 00:41:39 +00:00
|
|
|
return nil
|
|
|
|
case isPkgDir(fi):
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return os.ErrInvalid
|
2013-03-11 23:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 23:54:26 +00:00
|
|
|
pkgName := pathpkg.Join(domainName, suffix)
|
|
|
|
pi, err := getPageInfo(pkgName, diskPath)
|
2013-02-23 21:38:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2018-01-06 00:41:39 +00:00
|
|
|
return err
|
2013-02-23 21:38:58 +00:00
|
|
|
}
|
2013-02-22 23:58:56 +00:00
|
|
|
|
2013-03-15 18:26:24 +00:00
|
|
|
subtitle := pathpkg.Base(diskPath)
|
2018-01-05 23:54:26 +00:00
|
|
|
title := subtitle + " (" + pkgName + ")"
|
2017-12-12 16:32:58 +00:00
|
|
|
servePage(w, r, pageParams{
|
2016-06-17 22:43:38 +00:00
|
|
|
title: title,
|
|
|
|
subtitle: subtitle,
|
|
|
|
content: applyTextTemplate(packageHTML, "packageHTML", pi),
|
|
|
|
})
|
2018-01-06 00:41:39 +00:00
|
|
|
return nil
|
2013-02-22 23:58:56 +00:00
|
|
|
}
|