Skip to content

Commit c4e27cb

Browse files
authored
Refactor markup render system (#32589)
This PR mainly moves some code and introduces `RenderContext.WithXxx` functions
1 parent 81ac8d9 commit c4e27cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+486
-626
lines changed

models/issues/comment_code.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,12 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
112112
}
113113

114114
var err error
115-
if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
116-
Ctx: ctx,
117-
Repo: issue.Repo,
118-
Links: markup.Links{
119-
Base: issue.Repo.Link(),
120-
},
121-
Metas: issue.Repo.ComposeMetas(ctx),
122-
}, comment.Content); err != nil {
115+
rctx := markup.NewRenderContext(ctx).
116+
WithRepoFacade(issue.Repo).
117+
WithLinks(markup.Links{Base: issue.Repo.Link()}).
118+
WithMetas(issue.Repo.ComposeMetas(ctx))
119+
if comment.RenderedContent, err = markdown.RenderString(rctx,
120+
comment.Content); err != nil {
123121
return nil, err
124122
}
125123
}

models/repo/repo.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,7 @@ func (repo *Repository) CanEnableEditor() bool {
617617

618618
// DescriptionHTML does special handles to description and return HTML string.
619619
func (repo *Repository) DescriptionHTML(ctx context.Context) template.HTML {
620-
desc, err := markup.RenderDescriptionHTML(&markup.RenderContext{
621-
Ctx: ctx,
622-
// Don't use Metas to speedup requests
623-
}, repo.Description)
620+
desc, err := markup.RenderDescriptionHTML(markup.NewRenderContext(ctx), repo.Description)
624621
if err != nil {
625622
log.Error("Failed to render description for %s (ID: %d): %v", repo.Name, repo.ID, err)
626623
return template.HTML(markup.SanitizeDescription(repo.Description))

modules/csv/csv.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"bytes"
88
stdcsv "encoding/csv"
99
"io"
10-
"path/filepath"
10+
"path"
1111
"regexp"
1212
"strings"
1313

@@ -53,7 +53,7 @@ func CreateReaderAndDetermineDelimiter(ctx *markup.RenderContext, rd io.Reader)
5353
func determineDelimiter(ctx *markup.RenderContext, data []byte) rune {
5454
extension := ".csv"
5555
if ctx != nil {
56-
extension = strings.ToLower(filepath.Ext(ctx.RelativePath))
56+
extension = strings.ToLower(path.Ext(ctx.RenderOptions.RelativePath))
5757
}
5858

5959
var delimiter rune

modules/csv/csv_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ package csv
55

66
import (
77
"bytes"
8+
"context"
89
"encoding/csv"
910
"io"
1011
"strconv"
1112
"strings"
1213
"testing"
1314

14-
"code.gitea.io/gitea/modules/git"
1515
"code.gitea.io/gitea/modules/markup"
1616
"code.gitea.io/gitea/modules/translation"
1717

@@ -231,10 +231,7 @@ John Doe [email protected] This,note,had,a,lot,of,commas,to,test,delimiters`,
231231
}
232232

233233
for n, c := range cases {
234-
delimiter := determineDelimiter(&markup.RenderContext{
235-
Ctx: git.DefaultContext,
236-
RelativePath: c.filename,
237-
}, []byte(decodeSlashes(t, c.csv)))
234+
delimiter := determineDelimiter(markup.NewRenderContext(context.Background()).WithRelativePath(c.filename), []byte(decodeSlashes(t, c.csv)))
238235
assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter)
239236
}
240237
}

modules/markup/asciicast/asciicast.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
4444
func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer) error {
4545
rawURL := fmt.Sprintf("%s/%s/%s/raw/%s/%s",
4646
setting.AppSubURL,
47-
url.PathEscape(ctx.Metas["user"]),
48-
url.PathEscape(ctx.Metas["repo"]),
49-
ctx.Metas["BranchNameSubURL"],
50-
url.PathEscape(ctx.RelativePath),
47+
url.PathEscape(ctx.RenderOptions.Metas["user"]),
48+
url.PathEscape(ctx.RenderOptions.Metas["repo"]),
49+
ctx.RenderOptions.Metas["BranchNameSubURL"],
50+
url.PathEscape(ctx.RenderOptions.RelativePath),
5151
)
5252
return ctx.RenderInternal.FormatWithSafeAttrs(output, `<div class="%s" %s="%s"></div>`, playerClassName, playerSrcAttr, rawURL)
5353
}

modules/markup/console/console_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package console
55

66
import (
7+
"context"
78
"strings"
89
"testing"
910

10-
"code.gitea.io/gitea/modules/git"
1111
"code.gitea.io/gitea/modules/markup"
1212

1313
"github.com/stretchr/testify/assert"
@@ -24,8 +24,7 @@ func TestRenderConsole(t *testing.T) {
2424
canRender := render.CanRender("test", strings.NewReader(k))
2525
assert.True(t, canRender)
2626

27-
err := render.Render(&markup.RenderContext{Ctx: git.DefaultContext},
28-
strings.NewReader(k), &buf)
27+
err := render.Render(markup.NewRenderContext(context.Background()), strings.NewReader(k), &buf)
2928
assert.NoError(t, err)
3029
assert.EqualValues(t, v, buf.String())
3130
}

modules/markup/csv/csv.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ func (r Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.W
133133
// Check if maxRows or maxSize is reached, and if true, warn.
134134
if (row >= maxRows && maxRows != 0) || (rd.InputOffset() >= maxSize && maxSize != 0) {
135135
warn := `<table class="data-table"><tr><td>`
136-
rawLink := ` <a href="` + ctx.Links.RawLink() + `/` + util.PathEscapeSegments(ctx.RelativePath) + `">`
136+
rawLink := ` <a href="` + ctx.RenderOptions.Links.RawLink() + `/` + util.PathEscapeSegments(ctx.RenderOptions.RelativePath) + `">`
137137

138138
// Try to get the user translation
139-
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
139+
if locale, ok := ctx.Value(translation.ContextKey).(translation.Locale); ok {
140140
warn += locale.TrString("repo.file_too_large")
141141
rawLink += locale.TrString("repo.file_view_raw")
142142
} else {

modules/markup/csv/csv_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package markup
55

66
import (
7+
"context"
78
"strings"
89
"testing"
910

10-
"code.gitea.io/gitea/modules/git"
1111
"code.gitea.io/gitea/modules/markup"
1212

1313
"github.com/stretchr/testify/assert"
@@ -24,8 +24,7 @@ func TestRenderCSV(t *testing.T) {
2424

2525
for k, v := range kases {
2626
var buf strings.Builder
27-
err := render.Render(&markup.RenderContext{Ctx: git.DefaultContext},
28-
strings.NewReader(k), &buf)
27+
err := render.Render(markup.NewRenderContext(context.Background()), strings.NewReader(k), &buf)
2928
assert.NoError(t, err)
3029
assert.EqualValues(t, v, buf.String())
3130
}

modules/markup/external/external.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"runtime"
1313
"strings"
1414

15-
"code.gitea.io/gitea/modules/graceful"
1615
"code.gitea.io/gitea/modules/log"
1716
"code.gitea.io/gitea/modules/markup"
1817
"code.gitea.io/gitea/modules/process"
@@ -80,8 +79,8 @@ func envMark(envName string) string {
8079
func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
8180
var (
8281
command = strings.NewReplacer(
83-
envMark("GITEA_PREFIX_SRC"), ctx.Links.SrcLink(),
84-
envMark("GITEA_PREFIX_RAW"), ctx.Links.RawLink(),
82+
envMark("GITEA_PREFIX_SRC"), ctx.RenderOptions.Links.SrcLink(),
83+
envMark("GITEA_PREFIX_RAW"), ctx.RenderOptions.Links.RawLink(),
8584
).Replace(p.Command)
8685
commands = strings.Fields(command)
8786
args = commands[1:]
@@ -113,22 +112,14 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
113112
args = append(args, f.Name())
114113
}
115114

116-
if ctx.Ctx == nil {
117-
if !setting.IsProd || setting.IsInTesting {
118-
panic("RenderContext did not provide context")
119-
}
120-
log.Warn("RenderContext did not provide context, defaulting to Shutdown context")
121-
ctx.Ctx = graceful.GetManager().ShutdownContext()
122-
}
123-
124-
processCtx, _, finished := process.GetManager().AddContext(ctx.Ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.Links.SrcLink()))
115+
processCtx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.RenderOptions.Links.SrcLink()))
125116
defer finished()
126117

127118
cmd := exec.CommandContext(processCtx, commands[0], args...)
128119
cmd.Env = append(
129120
os.Environ(),
130-
"GITEA_PREFIX_SRC="+ctx.Links.SrcLink(),
131-
"GITEA_PREFIX_RAW="+ctx.Links.RawLink(),
121+
"GITEA_PREFIX_SRC="+ctx.RenderOptions.Links.SrcLink(),
122+
"GITEA_PREFIX_RAW="+ctx.RenderOptions.Links.RawLink(),
132123
)
133124
if !p.IsInputFile {
134125
cmd.Stdin = input

modules/markup/html_codepreview.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosSt
3838
CommitID: node.Data[m[6]:m[7]],
3939
FilePath: node.Data[m[8]:m[9]],
4040
}
41-
if !httplib.IsCurrentGiteaSiteURL(ctx.Ctx, opts.FullURL) {
41+
if !httplib.IsCurrentGiteaSiteURL(ctx, opts.FullURL) {
4242
return 0, 0, "", nil
4343
}
4444
u, err := url.Parse(opts.FilePath)
@@ -51,7 +51,7 @@ func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosSt
5151
lineStart, _ := strconv.Atoi(strings.TrimPrefix(lineStartStr, "L"))
5252
lineStop, _ := strconv.Atoi(strings.TrimPrefix(lineStopStr, "L"))
5353
opts.LineStart, opts.LineStop = lineStart, lineStop
54-
h, err := DefaultProcessorHelper.RenderRepoFileCodePreview(ctx.Ctx, opts)
54+
h, err := DefaultProcessorHelper.RenderRepoFileCodePreview(ctx, opts)
5555
return m[0], m[1], h, err
5656
}
5757

modules/markup/html_codepreview_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"testing"
1111

12-
"code.gitea.io/gitea/modules/git"
1312
"code.gitea.io/gitea/modules/markup"
1413
"code.gitea.io/gitea/modules/markup/markdown"
1514

@@ -23,10 +22,7 @@ func TestRenderCodePreview(t *testing.T) {
2322
},
2423
})
2524
test := func(input, expected string) {
26-
buffer, err := markup.RenderString(&markup.RenderContext{
27-
Ctx: git.DefaultContext,
28-
MarkupType: markdown.MarkupName,
29-
}, input)
25+
buffer, err := markup.RenderString(markup.NewRenderContext(context.Background()).WithMarkupType(markdown.MarkupName), input)
3026
assert.NoError(t, err)
3127
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
3228
}

modules/markup/html_commit.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func anyHashPatternExtract(s string) (ret anyHashPatternResult, ok bool) {
8484

8585
// fullHashPatternProcessor renders SHA containing URLs
8686
func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) {
87-
if ctx.Metas == nil {
87+
if ctx.RenderOptions.Metas == nil {
8888
return
8989
}
9090
nodeStop := node.NextSibling
@@ -111,7 +111,7 @@ func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) {
111111
}
112112

113113
func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
114-
if ctx.Metas == nil {
114+
if ctx.RenderOptions.Metas == nil {
115115
return
116116
}
117117
nodeStop := node.NextSibling
@@ -163,14 +163,14 @@ func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
163163
// hashCurrentPatternProcessor renders SHA1 strings to corresponding links that
164164
// are assumed to be in the same repository.
165165
func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
166-
if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || (ctx.Repo == nil && ctx.GitRepo == nil) {
166+
if ctx.RenderOptions.Metas == nil || ctx.RenderOptions.Metas["user"] == "" || ctx.RenderOptions.Metas["repo"] == "" || (ctx.RenderHelper.repoFacade == nil && ctx.RenderHelper.gitRepo == nil) {
167167
return
168168
}
169169

170170
start := 0
171171
next := node.NextSibling
172-
if ctx.ShaExistCache == nil {
173-
ctx.ShaExistCache = make(map[string]bool)
172+
if ctx.RenderHelper.shaExistCache == nil {
173+
ctx.RenderHelper.shaExistCache = make(map[string]bool)
174174
}
175175
for node != nil && node != next && start < len(node.Data) {
176176
m := globalVars().hashCurrentPattern.FindStringSubmatchIndex(node.Data[start:])
@@ -191,33 +191,33 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
191191
// a commit in the repository before making it a link.
192192

193193
// check cache first
194-
exist, inCache := ctx.ShaExistCache[hash]
194+
exist, inCache := ctx.RenderHelper.shaExistCache[hash]
195195
if !inCache {
196-
if ctx.GitRepo == nil {
196+
if ctx.RenderHelper.gitRepo == nil {
197197
var err error
198198
var closer io.Closer
199-
ctx.GitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx.Ctx, ctx.Repo)
199+
ctx.RenderHelper.gitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx, ctx.RenderHelper.repoFacade)
200200
if err != nil {
201-
log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(ctx.Repo), err)
201+
log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(ctx.RenderHelper.repoFacade), err)
202202
return
203203
}
204204
ctx.AddCancel(func() {
205205
_ = closer.Close()
206-
ctx.GitRepo = nil
206+
ctx.RenderHelper.gitRepo = nil
207207
})
208208
}
209209

210210
// Don't use IsObjectExist since it doesn't support short hashs with gogit edition.
211-
exist = ctx.GitRepo.IsReferenceExist(hash)
212-
ctx.ShaExistCache[hash] = exist
211+
exist = ctx.RenderHelper.gitRepo.IsReferenceExist(hash)
212+
ctx.RenderHelper.shaExistCache[hash] = exist
213213
}
214214

215215
if !exist {
216216
start = m[3]
217217
continue
218218
}
219219

220-
link := util.URLJoin(ctx.Links.Prefix(), ctx.Metas["user"], ctx.Metas["repo"], "commit", hash)
220+
link := util.URLJoin(ctx.RenderOptions.Links.Prefix(), ctx.RenderOptions.Metas["user"], ctx.RenderOptions.Metas["repo"], "commit", hash)
221221
replaceContent(node, m[2], m[3], createCodeLink(link, base.ShortSha(hash), "commit"))
222222
start = 0
223223
node = node.NextSibling.NextSibling

0 commit comments

Comments
 (0)