~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/cmd/godoc/main.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
        "fmt"
37
37
        "go/ast"
38
38
        "go/build"
 
39
        "go/printer"
39
40
        "io"
40
41
        "log"
41
42
        "net/http"
73
74
)
74
75
 
75
76
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
76
 
        contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
77
77
        w.WriteHeader(http.StatusNotFound)
78
 
        servePage(w, relpath, "File "+relpath, "", "", contents)
 
78
        servePage(w, Page{
 
79
                Title:    "File " + relpath,
 
80
                Subtitle: relpath,
 
81
                Body:     applyTemplate(errorHTML, "errorHTML", err), // err may contain an absolute path!
 
82
        })
79
83
}
80
84
 
81
85
func usage() {
221
225
        // Print content that would be served at the URL *urlFlag.
222
226
        if *urlFlag != "" {
223
227
                registerPublicHandlers(http.DefaultServeMux)
 
228
                initFSTree()
 
229
                updateMetadata()
224
230
                // Try up to 10 fetches, following redirects.
225
231
                urlstr := *urlFlag
226
232
                for i := 0; i < 10; i++ {
278
284
                }
279
285
 
280
286
                registerPublicHandlers(http.DefaultServeMux)
281
 
 
282
 
                // Playground handlers are not available in local godoc.
283
 
                http.HandleFunc("/compile", disabledHandler)
284
 
                http.HandleFunc("/share", disabledHandler)
 
287
                registerPlaygroundHandlers(http.DefaultServeMux)
285
288
 
286
289
                // Initialize default directory tree with corresponding timestamp.
287
290
                // (Do it in a goroutine so that launch is quick.)
344
347
                fs.Bind(target, OS(path), "/", bindReplace)
345
348
                abspath = target
346
349
        } else if strings.HasPrefix(path, cmdPrefix) {
347
 
                path = path[len(cmdPrefix):]
 
350
                path = strings.TrimPrefix(path, cmdPrefix)
348
351
                forceCmd = true
349
352
        } else if bp, _ := build.Import(path, "", build.FindOnly); bp.Dir != "" && bp.ImportPath != "" {
350
353
                fs.Bind(target, OS(bp.Dir), "/", bindReplace)
369
372
                }
370
373
                mode |= showSource
371
374
        }
372
 
        // TODO(gri): Provide a mechanism (flag?) to select a package
373
 
        //            if there are multiple packages in a directory.
374
375
 
375
376
        // first, try as package unless forced as command
376
 
        var info PageInfo
 
377
        var info *PageInfo
377
378
        if !forceCmd {
378
 
                info = pkgHandler.getPageInfo(abspath, relpath, "", mode)
 
379
                info = pkgHandler.getPageInfo(abspath, relpath, mode)
379
380
        }
380
381
 
381
382
        // second, try as command unless the path is absolute
382
383
        // (the go command invokes godoc w/ absolute paths; don't override)
383
 
        var cinfo PageInfo
 
384
        var cinfo *PageInfo
384
385
        if !filepath.IsAbs(path) {
385
386
                abspath = pathpkg.Join(cmdHandler.fsRoot, path)
386
 
                cinfo = cmdHandler.getPageInfo(abspath, relpath, "", mode)
 
387
                cinfo = cmdHandler.getPageInfo(abspath, relpath, mode)
387
388
        }
388
389
 
389
390
        // determine what to use
390
 
        if info.IsEmpty() {
391
 
                if !cinfo.IsEmpty() {
 
391
        if info == nil || info.IsEmpty() {
 
392
                if cinfo != nil && !cinfo.IsEmpty() {
392
393
                        // only cinfo exists - switch to cinfo
393
394
                        info = cinfo
394
395
                }
395
 
        } else if !cinfo.IsEmpty() {
 
396
        } else if cinfo != nil && !cinfo.IsEmpty() {
396
397
                // both info and cinfo exist - use cinfo if info
397
398
                // contains only subdirectory information
398
399
                if info.PAst == nil && info.PDoc == nil {
402
403
                }
403
404
        }
404
405
 
 
406
        if info == nil {
 
407
                log.Fatalf("%s: no such directory or package", flag.Arg(0))
 
408
        }
405
409
        if info.Err != nil {
406
410
                log.Fatalf("%v", info.Err)
407
411
        }
 
412
 
408
413
        if info.PDoc != nil && info.PDoc.ImportPath == target {
409
414
                // Replace virtual /target with actual argument from command line.
410
415
                info.PDoc.ImportPath = flag.Arg(0)
411
416
        }
412
417
 
413
 
        // If we have more than one argument, use the remaining arguments for filtering
 
418
        // If we have more than one argument, use the remaining arguments for filtering.
414
419
        if flag.NArg() > 1 {
415
420
                args := flag.Args()[1:]
416
421
                rx := makeRx(args)
421
426
                filter := func(s string) bool { return rx.MatchString(s) }
422
427
                switch {
423
428
                case info.PAst != nil:
 
429
                        cmap := ast.NewCommentMap(info.FSet, info.PAst, info.PAst.Comments)
424
430
                        ast.FilterFile(info.PAst, filter)
425
431
                        // Special case: Don't use templates for printing
426
432
                        // so we only get the filtered declarations without
427
433
                        // package clause or extra whitespace.
428
434
                        for i, d := range info.PAst.Decls {
 
435
                                // determine the comments associated with d only
 
436
                                comments := cmap.Filter(d).Comments()
 
437
                                cn := &printer.CommentedNode{Node: d, Comments: comments}
429
438
                                if i > 0 {
430
439
                                        fmt.Println()
431
440
                                }
432
441
                                if *html {
433
442
                                        var buf bytes.Buffer
434
 
                                        writeNode(&buf, info.FSet, d)
 
443
                                        writeNode(&buf, info.FSet, cn)
435
444
                                        FormatText(os.Stdout, buf.Bytes(), -1, true, "", nil)
436
445
                                } else {
437
 
                                        writeNode(os.Stdout, info.FSet, d)
 
446
                                        writeNode(os.Stdout, info.FSet, cn)
438
447
                                }
439
448
                                fmt.Println()
440
449
                        }
459
468
 
460
469
func (w *httpWriter) Header() http.Header  { return w.h }
461
470
func (w *httpWriter) WriteHeader(code int) { w.code = code }
462
 
 
463
 
// disabledHandler serves a 501 "Not Implemented" response.
464
 
func disabledHandler(w http.ResponseWriter, r *http.Request) {
465
 
        w.WriteHeader(http.StatusNotImplemented)
466
 
        fmt.Fprint(w, "This functionality is not available via local godoc.")
467
 
}