gitea源码

renderhelper_codepreview.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "bufio"
  6. "context"
  7. "errors"
  8. "html/template"
  9. "strings"
  10. "code.gitea.io/gitea/models/perm/access"
  11. "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unit"
  13. "code.gitea.io/gitea/modules/charset"
  14. "code.gitea.io/gitea/modules/git/languagestats"
  15. "code.gitea.io/gitea/modules/gitrepo"
  16. "code.gitea.io/gitea/modules/indexer/code"
  17. "code.gitea.io/gitea/modules/markup"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/modules/util"
  20. gitea_context "code.gitea.io/gitea/services/context"
  21. )
  22. func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePreviewOptions) (template.HTML, error) {
  23. opts.LineStop = max(opts.LineStop, opts.LineStart)
  24. lineCount := opts.LineStop - opts.LineStart + 1
  25. if lineCount <= 0 || lineCount > 140 /* GitHub at most show 140 lines */ {
  26. lineCount = 10
  27. opts.LineStop = opts.LineStart + lineCount
  28. }
  29. dbRepo, err := repo.GetRepositoryByOwnerAndName(ctx, opts.OwnerName, opts.RepoName)
  30. if err != nil {
  31. return "", err
  32. }
  33. webCtx := gitea_context.GetWebContext(ctx)
  34. if webCtx == nil {
  35. return "", errors.New("context is not a web context")
  36. }
  37. doer := webCtx.Doer
  38. perms, err := access.GetUserRepoPermission(ctx, dbRepo, doer)
  39. if err != nil {
  40. return "", err
  41. }
  42. if !perms.CanRead(unit.TypeCode) {
  43. return "", util.ErrPermissionDenied
  44. }
  45. gitRepo, err := gitrepo.OpenRepository(ctx, dbRepo)
  46. if err != nil {
  47. return "", err
  48. }
  49. defer gitRepo.Close()
  50. commit, err := gitRepo.GetCommit(opts.CommitID)
  51. if err != nil {
  52. return "", err
  53. }
  54. language, _ := languagestats.GetFileLanguage(ctx, gitRepo, opts.CommitID, opts.FilePath)
  55. blob, err := commit.GetBlobByPath(opts.FilePath)
  56. if err != nil {
  57. return "", err
  58. }
  59. if blob.Size() > setting.UI.MaxDisplayFileSize {
  60. return "", errors.New("file is too large")
  61. }
  62. dataRc, err := blob.DataAsync()
  63. if err != nil {
  64. return "", err
  65. }
  66. defer dataRc.Close()
  67. reader := bufio.NewReader(dataRc)
  68. for i := 1; i < opts.LineStart; i++ {
  69. if _, err = reader.ReadBytes('\n'); err != nil {
  70. return "", err
  71. }
  72. }
  73. lineNums := make([]int, 0, lineCount)
  74. lineCodes := make([]string, 0, lineCount)
  75. for i := opts.LineStart; i <= opts.LineStop; i++ {
  76. line, err := reader.ReadString('\n')
  77. if err != nil && line == "" {
  78. break
  79. }
  80. lineNums = append(lineNums, i)
  81. lineCodes = append(lineCodes, line)
  82. }
  83. realLineStop := max(opts.LineStart, opts.LineStart+len(lineNums)-1)
  84. highlightLines := code.HighlightSearchResultCode(opts.FilePath, language, lineNums, strings.Join(lineCodes, ""))
  85. escapeStatus := &charset.EscapeStatus{}
  86. lineEscapeStatus := make([]*charset.EscapeStatus, len(highlightLines))
  87. for i, hl := range highlightLines {
  88. lineEscapeStatus[i], hl.FormattedContent = charset.EscapeControlHTML(hl.FormattedContent, webCtx.Base.Locale, charset.RuneNBSP)
  89. escapeStatus = escapeStatus.Or(lineEscapeStatus[i])
  90. }
  91. return webCtx.RenderToHTML("base/markup_codepreview", map[string]any{
  92. "FullURL": opts.FullURL,
  93. "FilePath": opts.FilePath,
  94. "LineStart": opts.LineStart,
  95. "LineStop": realLineStop,
  96. "RepoLink": dbRepo.Link(),
  97. "CommitID": opts.CommitID,
  98. "HighlightLines": highlightLines,
  99. "EscapeStatus": escapeStatus,
  100. "LineEscapeStatus": lineEscapeStatus,
  101. })
  102. }