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.

search.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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(ctx.Repo.Repository.ID, keyword, page, setting.UI.RepoSearchPagingNum)
  27. if err != nil {
  28. ctx.ServerError("SearchResults", err)
  29. return
  30. }
  31. ctx.Data["Keyword"] = keyword
  32. pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5)
  33. ctx.Data["Page"] = pager
  34. ctx.Data["SourcePath"] = setting.AppSubURL + "/" +
  35. path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name, "src", "branch", ctx.Repo.Repository.DefaultBranch)
  36. ctx.Data["SearchResults"] = searchResults
  37. ctx.Data["RequireHighlightJS"] = true
  38. ctx.Data["PageIsViewCode"] = true
  39. ctx.HTML(200, tplSearch)
  40. }