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.

52 lines
1.5 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_indexer "code.gitea.io/gitea/modules/indexer/code"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. const tplSearch base.TplName = "repo/search"
  14. // Search render repository search page
  15. func Search(ctx *context.Context) {
  16. if !setting.Indexer.RepoIndexerEnabled {
  17. ctx.Redirect(ctx.Repo.RepoLink, 302)
  18. return
  19. }
  20. language := strings.TrimSpace(ctx.Query("l"))
  21. keyword := strings.TrimSpace(ctx.Query("q"))
  22. page := ctx.QueryInt("page")
  23. if page <= 0 {
  24. page = 1
  25. }
  26. total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch([]int64{ctx.Repo.Repository.ID},
  27. language, keyword, page, setting.UI.RepoSearchPagingNum)
  28. if err != nil {
  29. ctx.ServerError("SearchResults", err)
  30. return
  31. }
  32. ctx.Data["Keyword"] = keyword
  33. ctx.Data["Language"] = language
  34. ctx.Data["SourcePath"] = setting.AppSubURL + "/" +
  35. path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name)
  36. ctx.Data["SearchResults"] = searchResults
  37. ctx.Data["SearchResultLanguages"] = searchResultLanguages
  38. ctx.Data["RequireHighlightJS"] = true
  39. ctx.Data["PageIsViewCode"] = true
  40. pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
  41. pager.SetDefaultParams(ctx)
  42. pager.AddParam(ctx, "l", "Language")
  43. ctx.Data["Page"] = pager
  44. ctx.HTML(200, tplSearch)
  45. }