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.

47 lines
1.3 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "path"
  7. "strings"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/search"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/Unknwon/paginater"
  13. )
  14. const tplSearch base.TplName = "repo/search"
  15. // Search render repository search page
  16. func Search(ctx *context.Context) {
  17. if !setting.Indexer.RepoIndexerEnabled {
  18. ctx.Redirect(ctx.Repo.RepoLink, 302)
  19. return
  20. }
  21. keyword := strings.TrimSpace(ctx.Query("q"))
  22. page := ctx.QueryInt("page")
  23. if page <= 0 {
  24. page = 1
  25. }
  26. total, searchResults, err := search.PerformSearch([]int64{ctx.Repo.Repository.ID},
  27. keyword, page, setting.UI.RepoSearchPagingNum)
  28. if err != nil {
  29. ctx.ServerError("SearchResults", err)
  30. return
  31. }
  32. ctx.Data["Keyword"] = keyword
  33. pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5)
  34. ctx.Data["Page"] = pager
  35. ctx.Data["SourcePath"] = setting.AppSubURL + "/" +
  36. path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name, "src", "branch", ctx.Repo.Repository.DefaultBranch)
  37. ctx.Data["SearchResults"] = searchResults
  38. ctx.Data["RequireHighlightJS"] = true
  39. ctx.Data["PageIsViewCode"] = true
  40. ctx.HTML(200, tplSearch)
  41. }