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.

processorhelper_codepreview.go 3.3KB

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