Skip to content

Commit

Permalink
Refactor markup render system (#32589)
Browse files Browse the repository at this point in the history
This PR mainly moves some code and introduces `RenderContext.WithXxx`
functions
  • Loading branch information
wxiaoguang authored Nov 22, 2024
1 parent 81ac8d9 commit c4e27cb
Show file tree
Hide file tree
Showing 49 changed files with 486 additions and 626 deletions.
14 changes: 6 additions & 8 deletions models/issues/comment_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
}

var err error
if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Ctx: ctx,
Repo: issue.Repo,
Links: markup.Links{
Base: issue.Repo.Link(),
},
Metas: issue.Repo.ComposeMetas(ctx),
}, comment.Content); err != nil {
rctx := markup.NewRenderContext(ctx).
WithRepoFacade(issue.Repo).
WithLinks(markup.Links{Base: issue.Repo.Link()}).
WithMetas(issue.Repo.ComposeMetas(ctx))
if comment.RenderedContent, err = markdown.RenderString(rctx,
comment.Content); err != nil {
return nil, err
}
}
Expand Down
5 changes: 1 addition & 4 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,7 @@ func (repo *Repository) CanEnableEditor() bool {

// DescriptionHTML does special handles to description and return HTML string.
func (repo *Repository) DescriptionHTML(ctx context.Context) template.HTML {
desc, err := markup.RenderDescriptionHTML(&markup.RenderContext{
Ctx: ctx,
// Don't use Metas to speedup requests
}, repo.Description)
desc, err := markup.RenderDescriptionHTML(markup.NewRenderContext(ctx), repo.Description)
if err != nil {
log.Error("Failed to render description for %s (ID: %d): %v", repo.Name, repo.ID, err)
return template.HTML(markup.SanitizeDescription(repo.Description))
Expand Down
4 changes: 2 additions & 2 deletions modules/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
stdcsv "encoding/csv"
"io"
"path/filepath"
"path"
"regexp"
"strings"

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

var delimiter rune
Expand Down
7 changes: 2 additions & 5 deletions modules/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package csv

import (
"bytes"
"context"
"encoding/csv"
"io"
"strconv"
"strings"
"testing"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/translation"

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

for n, c := range cases {
delimiter := determineDelimiter(&markup.RenderContext{
Ctx: git.DefaultContext,
RelativePath: c.filename,
}, []byte(decodeSlashes(t, c.csv)))
delimiter := determineDelimiter(markup.NewRenderContext(context.Background()).WithRelativePath(c.filename), []byte(decodeSlashes(t, c.csv)))
assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter)
}
}
Expand Down
8 changes: 4 additions & 4 deletions modules/markup/asciicast/asciicast.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer) error {
rawURL := fmt.Sprintf("%s/%s/%s/raw/%s/%s",
setting.AppSubURL,
url.PathEscape(ctx.Metas["user"]),
url.PathEscape(ctx.Metas["repo"]),
ctx.Metas["BranchNameSubURL"],
url.PathEscape(ctx.RelativePath),
url.PathEscape(ctx.RenderOptions.Metas["user"]),
url.PathEscape(ctx.RenderOptions.Metas["repo"]),
ctx.RenderOptions.Metas["BranchNameSubURL"],
url.PathEscape(ctx.RenderOptions.RelativePath),
)
return ctx.RenderInternal.FormatWithSafeAttrs(output, `<div class="%s" %s="%s"></div>`, playerClassName, playerSrcAttr, rawURL)
}
5 changes: 2 additions & 3 deletions modules/markup/console/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package console

import (
"context"
"strings"
"testing"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"

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

err := render.Render(&markup.RenderContext{Ctx: git.DefaultContext},
strings.NewReader(k), &buf)
err := render.Render(markup.NewRenderContext(context.Background()), strings.NewReader(k), &buf)
assert.NoError(t, err)
assert.EqualValues(t, v, buf.String())
}
Expand Down
4 changes: 2 additions & 2 deletions modules/markup/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ func (r Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.W
// Check if maxRows or maxSize is reached, and if true, warn.
if (row >= maxRows && maxRows != 0) || (rd.InputOffset() >= maxSize && maxSize != 0) {
warn := `<table class="data-table"><tr><td>`
rawLink := ` <a href="` + ctx.Links.RawLink() + `/` + util.PathEscapeSegments(ctx.RelativePath) + `">`
rawLink := ` <a href="` + ctx.RenderOptions.Links.RawLink() + `/` + util.PathEscapeSegments(ctx.RenderOptions.RelativePath) + `">`

// Try to get the user translation
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
if locale, ok := ctx.Value(translation.ContextKey).(translation.Locale); ok {
warn += locale.TrString("repo.file_too_large")
rawLink += locale.TrString("repo.file_view_raw")
} else {
Expand Down
5 changes: 2 additions & 3 deletions modules/markup/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package markup

import (
"context"
"strings"
"testing"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"

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

for k, v := range kases {
var buf strings.Builder
err := render.Render(&markup.RenderContext{Ctx: git.DefaultContext},
strings.NewReader(k), &buf)
err := render.Render(markup.NewRenderContext(context.Background()), strings.NewReader(k), &buf)
assert.NoError(t, err)
assert.EqualValues(t, v, buf.String())
}
Expand Down
19 changes: 5 additions & 14 deletions modules/markup/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"runtime"
"strings"

"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/process"
Expand Down Expand Up @@ -80,8 +79,8 @@ func envMark(envName string) string {
func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
var (
command = strings.NewReplacer(
envMark("GITEA_PREFIX_SRC"), ctx.Links.SrcLink(),
envMark("GITEA_PREFIX_RAW"), ctx.Links.RawLink(),
envMark("GITEA_PREFIX_SRC"), ctx.RenderOptions.Links.SrcLink(),
envMark("GITEA_PREFIX_RAW"), ctx.RenderOptions.Links.RawLink(),
).Replace(p.Command)
commands = strings.Fields(command)
args = commands[1:]
Expand Down Expand Up @@ -113,22 +112,14 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
args = append(args, f.Name())
}

if ctx.Ctx == nil {
if !setting.IsProd || setting.IsInTesting {
panic("RenderContext did not provide context")
}
log.Warn("RenderContext did not provide context, defaulting to Shutdown context")
ctx.Ctx = graceful.GetManager().ShutdownContext()
}

processCtx, _, finished := process.GetManager().AddContext(ctx.Ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.Links.SrcLink()))
processCtx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.RenderOptions.Links.SrcLink()))
defer finished()

cmd := exec.CommandContext(processCtx, commands[0], args...)
cmd.Env = append(
os.Environ(),
"GITEA_PREFIX_SRC="+ctx.Links.SrcLink(),
"GITEA_PREFIX_RAW="+ctx.Links.RawLink(),
"GITEA_PREFIX_SRC="+ctx.RenderOptions.Links.SrcLink(),
"GITEA_PREFIX_RAW="+ctx.RenderOptions.Links.RawLink(),
)
if !p.IsInputFile {
cmd.Stdin = input
Expand Down
4 changes: 2 additions & 2 deletions modules/markup/html_codepreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosSt
CommitID: node.Data[m[6]:m[7]],
FilePath: node.Data[m[8]:m[9]],
}
if !httplib.IsCurrentGiteaSiteURL(ctx.Ctx, opts.FullURL) {
if !httplib.IsCurrentGiteaSiteURL(ctx, opts.FullURL) {
return 0, 0, "", nil
}
u, err := url.Parse(opts.FilePath)
Expand All @@ -51,7 +51,7 @@ func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosSt
lineStart, _ := strconv.Atoi(strings.TrimPrefix(lineStartStr, "L"))
lineStop, _ := strconv.Atoi(strings.TrimPrefix(lineStopStr, "L"))
opts.LineStart, opts.LineStop = lineStart, lineStop
h, err := DefaultProcessorHelper.RenderRepoFileCodePreview(ctx.Ctx, opts)
h, err := DefaultProcessorHelper.RenderRepoFileCodePreview(ctx, opts)
return m[0], m[1], h, err
}

Expand Down
6 changes: 1 addition & 5 deletions modules/markup/html_codepreview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"

Expand All @@ -23,10 +22,7 @@ func TestRenderCodePreview(t *testing.T) {
},
})
test := func(input, expected string) {
buffer, err := markup.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
MarkupType: markdown.MarkupName,
}, input)
buffer, err := markup.RenderString(markup.NewRenderContext(context.Background()).WithMarkupType(markdown.MarkupName), input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
Expand Down
26 changes: 13 additions & 13 deletions modules/markup/html_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func anyHashPatternExtract(s string) (ret anyHashPatternResult, ok bool) {

// fullHashPatternProcessor renders SHA containing URLs
func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil {
if ctx.RenderOptions.Metas == nil {
return
}
nodeStop := node.NextSibling
Expand All @@ -111,7 +111,7 @@ func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) {
}

func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil {
if ctx.RenderOptions.Metas == nil {
return
}
nodeStop := node.NextSibling
Expand Down Expand Up @@ -163,14 +163,14 @@ func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
// hashCurrentPatternProcessor renders SHA1 strings to corresponding links that
// are assumed to be in the same repository.
func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || (ctx.Repo == nil && ctx.GitRepo == nil) {
if ctx.RenderOptions.Metas == nil || ctx.RenderOptions.Metas["user"] == "" || ctx.RenderOptions.Metas["repo"] == "" || (ctx.RenderHelper.repoFacade == nil && ctx.RenderHelper.gitRepo == nil) {
return
}

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

// check cache first
exist, inCache := ctx.ShaExistCache[hash]
exist, inCache := ctx.RenderHelper.shaExistCache[hash]
if !inCache {
if ctx.GitRepo == nil {
if ctx.RenderHelper.gitRepo == nil {
var err error
var closer io.Closer
ctx.GitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx.Ctx, ctx.Repo)
ctx.RenderHelper.gitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx, ctx.RenderHelper.repoFacade)
if err != nil {
log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(ctx.Repo), err)
log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(ctx.RenderHelper.repoFacade), err)
return
}
ctx.AddCancel(func() {
_ = closer.Close()
ctx.GitRepo = nil
ctx.RenderHelper.gitRepo = nil
})
}

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

if !exist {
start = m[3]
continue
}

link := util.URLJoin(ctx.Links.Prefix(), ctx.Metas["user"], ctx.Metas["repo"], "commit", hash)
link := util.URLJoin(ctx.RenderOptions.Links.Prefix(), ctx.RenderOptions.Metas["user"], ctx.RenderOptions.Metas["repo"], "commit", hash)
replaceContent(node, m[2], m[3], createCodeLink(link, base.ShortSha(hash), "commit"))
start = 0
node = node.NextSibling.NextSibling
Expand Down
Loading

0 comments on commit c4e27cb

Please sign in to comment.