Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

98 строки
2.3 KiB

  1. // Copyright 2013 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 main
  7. import (
  8. "net/url"
  9. "path"
  10. "regexp"
  11. "strings"
  12. )
  13. func importPathFromGoogleBrowse(m []string) string {
  14. project := m[1]
  15. dir := m[2]
  16. if dir == "" {
  17. dir = "/"
  18. } else if dir[len(dir)-1] == '/' {
  19. dir = dir[:len(dir)-1]
  20. }
  21. subrepo := ""
  22. if len(m[3]) > 0 {
  23. v, _ := url.ParseQuery(m[3][1:])
  24. subrepo = v.Get("repo")
  25. if len(subrepo) > 0 {
  26. subrepo = "." + subrepo
  27. }
  28. }
  29. if strings.HasPrefix(m[4], "#hg%2F") {
  30. d, _ := url.QueryUnescape(m[4][len("#hg%2f"):])
  31. if i := strings.IndexRune(d, '%'); i >= 0 {
  32. d = d[:i]
  33. }
  34. dir = dir + "/" + d
  35. }
  36. return "code.google.com/p/" + project + subrepo + dir
  37. }
  38. var browsePatterns = []struct {
  39. pat *regexp.Regexp
  40. fn func([]string) string
  41. }{
  42. {
  43. // GitHub tree browser.
  44. regexp.MustCompile(`^https?://(github\.com/[^/]+/[^/]+)(?:/tree/[^/]+(/.*))?$`),
  45. func(m []string) string { return m[1] + m[2] },
  46. },
  47. {
  48. // GitHub file browser.
  49. regexp.MustCompile(`^https?://(github\.com/[^/]+/[^/]+)/blob/[^/]+/(.*)$`),
  50. func(m []string) string {
  51. d := path.Dir(m[2])
  52. if d == "." {
  53. return m[1]
  54. }
  55. return m[1] + "/" + d
  56. },
  57. },
  58. {
  59. // GitHub issues, pulls, etc.
  60. regexp.MustCompile(`^https?://(github\.com/[^/]+/[^/]+)(.*)$`),
  61. func(m []string) string { return m[1] },
  62. },
  63. {
  64. // Bitbucket source borwser.
  65. regexp.MustCompile(`^https?://(bitbucket\.org/[^/]+/[^/]+)(?:/src/[^/]+(/[^?]+)?)?`),
  66. func(m []string) string { return m[1] + m[2] },
  67. },
  68. {
  69. // Google Project Hosting source browser.
  70. regexp.MustCompile(`^http:/+code\.google\.com/p/([^/]+)/source/browse(/[^?#]*)?(\?[^#]*)?(#.*)?$`),
  71. importPathFromGoogleBrowse,
  72. },
  73. {
  74. // Launchpad source browser.
  75. regexp.MustCompile(`^https?:/+bazaar\.(launchpad\.net/.*)/files$`),
  76. func(m []string) string { return m[1] },
  77. },
  78. {
  79. regexp.MustCompile(`^https?://(.+)$`),
  80. func(m []string) string { return strings.Trim(m[1], "/") },
  81. },
  82. }
  83. // isBrowserURL returns importPath and true if URL looks like a URL for a VCS
  84. // source browser.
  85. func isBrowseURL(s string) (importPath string, ok bool) {
  86. for _, c := range browsePatterns {
  87. if m := c.pat.FindStringSubmatch(s); m != nil {
  88. return c.fn(m), true
  89. }
  90. }
  91. return "", false
  92. }