You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
1.6 KiB

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package gosrc
  7. import (
  8. "errors"
  9. "net/http"
  10. "regexp"
  11. "strings"
  12. )
  13. var (
  14. golangBuildVersionRe = regexp.MustCompile(`Build version ([-+:. 0-9A-Za-z]+)`)
  15. golangFileRe = regexp.MustCompile(`<a href="([^"]+)"`)
  16. )
  17. func getStandardDir(client *http.Client, importPath string, savedEtag string) (*Directory, error) {
  18. c := &httpClient{client: client}
  19. browseURL := "https://golang.org/src/" + importPath + "/"
  20. p, err := c.getBytes(browseURL)
  21. if err != nil {
  22. return nil, err
  23. }
  24. var etag string
  25. m := golangBuildVersionRe.FindSubmatch(p)
  26. if m == nil {
  27. return nil, errors.New("Could not find revision for " + importPath)
  28. }
  29. etag = strings.Trim(string(m[1]), ". ")
  30. if etag == savedEtag {
  31. return nil, NotModifiedError{}
  32. }
  33. var files []*File
  34. var dataURLs []string
  35. for _, m := range golangFileRe.FindAllSubmatch(p, -1) {
  36. fname := string(m[1])
  37. if isDocFile(fname) {
  38. files = append(files, &File{Name: fname, BrowseURL: browseURL + fname})
  39. dataURLs = append(dataURLs, browseURL+fname+"?m=text")
  40. }
  41. }
  42. if err := c.getFiles(dataURLs, files); err != nil {
  43. return nil, err
  44. }
  45. return &Directory{
  46. BrowseURL: browseURL,
  47. Etag: etag,
  48. Files: files,
  49. ImportPath: importPath,
  50. LineFmt: "%s#L%d",
  51. ProjectName: "Go",
  52. ProjectRoot: "",
  53. ProjectURL: "https://golang.org/",
  54. ResolvedPath: importPath,
  55. }, nil
  56. }