2024-03-18 08:21:35 +03:00
|
|
|
// Copyright The Forgejo Authors.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2024-03-16 10:09:49 +03:00
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
2024-03-25 18:05:01 +03:00
|
|
|
"bufio"
|
2024-03-16 10:09:49 +03:00
|
|
|
"bytes"
|
|
|
|
"html/template"
|
|
|
|
"regexp"
|
|
|
|
"slices"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2024-03-25 18:05:01 +03:00
|
|
|
"code.gitea.io/gitea/modules/highlight"
|
2024-03-16 10:18:47 +03:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-16 10:09:49 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2024-03-18 08:23:12 +03:00
|
|
|
|
2024-03-16 10:09:49 +03:00
|
|
|
"golang.org/x/net/html"
|
|
|
|
"golang.org/x/net/html/atom"
|
|
|
|
)
|
|
|
|
|
2024-03-16 10:18:47 +03:00
|
|
|
// filePreviewPattern matches "http://domain/org/repo/src/commit/COMMIT/filepath#L1-L2"
|
|
|
|
var filePreviewPattern = regexp.MustCompile(`https?://((?:\S+/){3})src/commit/([0-9a-f]{4,64})/(\S+)#(L\d+(?:-L\d+)?)`)
|
2024-03-16 10:09:49 +03:00
|
|
|
|
|
|
|
type FilePreview struct {
|
|
|
|
fileContent []template.HTML
|
2024-04-13 01:30:20 +03:00
|
|
|
title template.HTML
|
2024-03-16 10:09:49 +03:00
|
|
|
subTitle template.HTML
|
|
|
|
lineOffset int
|
|
|
|
start int
|
|
|
|
end int
|
2024-03-25 18:05:01 +03:00
|
|
|
isTruncated bool
|
2024-03-16 10:09:49 +03:00
|
|
|
}
|
|
|
|
|
2024-04-19 19:21:21 +03:00
|
|
|
func NewFilePreviews(ctx *RenderContext, node *html.Node, locale translation.Locale) []*FilePreview {
|
2024-03-27 20:36:12 +03:00
|
|
|
if setting.FilePreviewMaxLines == 0 {
|
2024-03-25 18:05:01 +03:00
|
|
|
// Feature is disabled
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 19:21:21 +03:00
|
|
|
mAll := filePreviewPattern.FindAllStringSubmatchIndex(node.Data, -1)
|
|
|
|
if mAll == nil {
|
2024-03-16 10:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 19:21:21 +03:00
|
|
|
result := make([]*FilePreview, 0)
|
|
|
|
|
|
|
|
for _, m := range mAll {
|
|
|
|
if slices.Contains(m, -1) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
preview := newFilePreview(ctx, node, locale, m)
|
|
|
|
if preview != nil {
|
|
|
|
result = append(result, preview)
|
|
|
|
}
|
2024-03-16 10:09:49 +03:00
|
|
|
}
|
|
|
|
|
2024-04-19 19:21:21 +03:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Locale, m []int) *FilePreview {
|
|
|
|
preview := &FilePreview{}
|
|
|
|
|
2024-04-13 01:30:20 +03:00
|
|
|
urlFull := node.Data[m[0]:m[1]]
|
2024-03-16 10:09:49 +03:00
|
|
|
|
|
|
|
// Ensure that we only use links to local repositories
|
2024-04-17 13:44:17 +03:00
|
|
|
if !strings.HasPrefix(urlFull, setting.AppURL) {
|
2024-03-16 10:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-17 13:44:17 +03:00
|
|
|
projPath := strings.TrimPrefix(strings.TrimSuffix(node.Data[m[0]:m[3]], "/"), setting.AppURL)
|
2024-03-16 10:09:49 +03:00
|
|
|
|
|
|
|
commitSha := node.Data[m[4]:m[5]]
|
2024-04-13 01:30:20 +03:00
|
|
|
filePath := node.Data[m[6]:m[7]]
|
2024-03-16 10:09:49 +03:00
|
|
|
hash := node.Data[m[8]:m[9]]
|
|
|
|
|
|
|
|
preview.start = m[0]
|
|
|
|
preview.end = m[1]
|
|
|
|
|
|
|
|
projPathSegments := strings.Split(projPath, "/")
|
2024-04-17 13:44:17 +03:00
|
|
|
if len(projPathSegments) != 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-13 01:30:20 +03:00
|
|
|
ownerName := projPathSegments[len(projPathSegments)-2]
|
|
|
|
repoName := projPathSegments[len(projPathSegments)-1]
|
|
|
|
|
2024-03-25 18:05:01 +03:00
|
|
|
var language string
|
|
|
|
fileBlob, err := DefaultProcessorHelper.GetRepoFileBlob(
|
2024-03-16 10:09:49 +03:00
|
|
|
ctx.Ctx,
|
2024-04-13 01:30:20 +03:00
|
|
|
ownerName,
|
|
|
|
repoName,
|
|
|
|
commitSha, filePath,
|
2024-03-25 18:05:01 +03:00
|
|
|
&language,
|
2024-03-16 10:09:49 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-13 01:30:20 +03:00
|
|
|
titleBuffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
isExternRef := ownerName != ctx.Metas["user"] || repoName != ctx.Metas["repo"]
|
|
|
|
if isExternRef {
|
|
|
|
err = html.Render(titleBuffer, createLink(node.Data[m[0]:m[3]], ownerName+"/"+repoName, ""))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to render repoLink: %v", err)
|
|
|
|
}
|
|
|
|
titleBuffer.WriteString(" – ")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = html.Render(titleBuffer, createLink(urlFull, filePath, "muted"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to render filepathLink: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
preview.title = template.HTML(titleBuffer.String())
|
|
|
|
|
2024-03-16 10:09:49 +03:00
|
|
|
lineSpecs := strings.Split(hash, "-")
|
|
|
|
|
|
|
|
commitLinkBuffer := new(bytes.Buffer)
|
2024-04-13 01:30:20 +03:00
|
|
|
commitLinkText := commitSha[0:7]
|
|
|
|
if isExternRef {
|
|
|
|
commitLinkText = ownerName + "/" + repoName + "@" + commitLinkText
|
|
|
|
}
|
|
|
|
|
|
|
|
err = html.Render(commitLinkBuffer, createLink(node.Data[m[0]:m[5]], commitLinkText, "text black"))
|
2024-03-16 10:18:47 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to render commitLink: %v", err)
|
|
|
|
}
|
2024-03-16 10:09:49 +03:00
|
|
|
|
2024-03-25 18:05:01 +03:00
|
|
|
var startLine, endLine int
|
|
|
|
|
2024-03-16 10:09:49 +03:00
|
|
|
if len(lineSpecs) == 1 {
|
2024-03-25 18:05:01 +03:00
|
|
|
startLine, _ = strconv.Atoi(strings.TrimPrefix(lineSpecs[0], "L"))
|
|
|
|
endLine = startLine
|
2024-03-16 10:09:49 +03:00
|
|
|
preview.subTitle = locale.Tr(
|
2024-03-25 18:05:01 +03:00
|
|
|
"markup.filepreview.line", startLine,
|
2024-03-16 10:09:49 +03:00
|
|
|
template.HTML(commitLinkBuffer.String()),
|
|
|
|
)
|
|
|
|
|
2024-03-25 18:05:01 +03:00
|
|
|
preview.lineOffset = startLine - 1
|
2024-03-16 10:09:49 +03:00
|
|
|
} else {
|
2024-03-25 18:05:01 +03:00
|
|
|
startLine, _ = strconv.Atoi(strings.TrimPrefix(lineSpecs[0], "L"))
|
|
|
|
endLine, _ = strconv.Atoi(strings.TrimPrefix(lineSpecs[1], "L"))
|
2024-03-16 10:09:49 +03:00
|
|
|
preview.subTitle = locale.Tr(
|
|
|
|
"markup.filepreview.lines", startLine, endLine,
|
|
|
|
template.HTML(commitLinkBuffer.String()),
|
|
|
|
)
|
|
|
|
|
|
|
|
preview.lineOffset = startLine - 1
|
|
|
|
}
|
|
|
|
|
2024-03-27 20:36:12 +03:00
|
|
|
lineCount := endLine - (startLine - 1)
|
2024-03-25 18:05:01 +03:00
|
|
|
if startLine < 1 || endLine < 1 || lineCount < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.FilePreviewMaxLines > 0 && lineCount > setting.FilePreviewMaxLines {
|
|
|
|
preview.isTruncated = true
|
|
|
|
lineCount = setting.FilePreviewMaxLines
|
|
|
|
}
|
|
|
|
|
|
|
|
dataRc, err := fileBlob.DataAsync()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer dataRc.Close()
|
|
|
|
|
|
|
|
reader := bufio.NewReader(dataRc)
|
|
|
|
|
|
|
|
// skip all lines until we find our startLine
|
|
|
|
for i := 1; i < startLine; i++ {
|
|
|
|
_, err := reader.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture the lines we're interested in
|
|
|
|
lineBuffer := new(bytes.Buffer)
|
|
|
|
for i := 0; i < lineCount; i++ {
|
|
|
|
buf, err := reader.ReadBytes('\n')
|
|
|
|
if err != nil {
|
2024-03-27 20:36:12 +03:00
|
|
|
break
|
2024-03-25 18:05:01 +03:00
|
|
|
}
|
|
|
|
lineBuffer.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// highlight the file...
|
|
|
|
fileContent, _, err := highlight.File(fileBlob.Name(), language, lineBuffer.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("highlight.File failed, fallback to plain text: %v", err)
|
|
|
|
fileContent = highlight.PlainText(lineBuffer.Bytes())
|
|
|
|
}
|
|
|
|
preview.fileContent = fileContent
|
|
|
|
|
2024-03-16 10:09:49 +03:00
|
|
|
return preview
|
|
|
|
}
|
|
|
|
|
2024-03-16 10:18:47 +03:00
|
|
|
func (p *FilePreview) CreateHTML(locale translation.Locale) *html.Node {
|
2024-03-16 10:09:49 +03:00
|
|
|
table := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Table.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "class", Val: "file-preview"}},
|
|
|
|
}
|
|
|
|
tbody := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Tbody.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
status := &charset.EscapeStatus{}
|
|
|
|
statuses := make([]*charset.EscapeStatus, len(p.fileContent))
|
|
|
|
for i, line := range p.fileContent {
|
|
|
|
statuses[i], p.fileContent[i] = charset.EscapeControlHTML(line, locale, charset.FileviewContext)
|
|
|
|
status = status.Or(statuses[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, code := range p.fileContent {
|
|
|
|
tr := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Tr.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
lineNum := strconv.Itoa(p.lineOffset + idx + 1)
|
|
|
|
|
|
|
|
tdLinesnum := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Td.String(),
|
|
|
|
Attr: []html.Attribute{
|
|
|
|
{Key: "class", Val: "lines-num"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
spanLinesNum := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Span.String(),
|
|
|
|
Attr: []html.Attribute{
|
|
|
|
{Key: "data-line-number", Val: lineNum},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tdLinesnum.AppendChild(spanLinesNum)
|
|
|
|
tr.AppendChild(tdLinesnum)
|
|
|
|
|
|
|
|
if status.Escaped {
|
|
|
|
tdLinesEscape := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Td.String(),
|
|
|
|
Attr: []html.Attribute{
|
|
|
|
{Key: "class", Val: "lines-escape"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if statuses[idx].Escaped {
|
|
|
|
btnTitle := ""
|
|
|
|
if statuses[idx].HasInvisible {
|
|
|
|
btnTitle += locale.TrString("repo.invisible_runes_line") + " "
|
|
|
|
}
|
|
|
|
if statuses[idx].HasAmbiguous {
|
|
|
|
btnTitle += locale.TrString("repo.ambiguous_runes_line")
|
|
|
|
}
|
|
|
|
|
|
|
|
escapeBtn := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Button.String(),
|
|
|
|
Attr: []html.Attribute{
|
|
|
|
{Key: "class", Val: "toggle-escape-button btn interact-bg"},
|
|
|
|
{Key: "title", Val: btnTitle},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tdLinesEscape.AppendChild(escapeBtn)
|
|
|
|
}
|
|
|
|
|
|
|
|
tr.AppendChild(tdLinesEscape)
|
|
|
|
}
|
|
|
|
|
|
|
|
tdCode := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Td.String(),
|
|
|
|
Attr: []html.Attribute{
|
|
|
|
{Key: "class", Val: "lines-code chroma"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
codeInner := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Code.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "class", Val: "code-inner"}},
|
|
|
|
}
|
|
|
|
codeText := &html.Node{
|
|
|
|
Type: html.RawNode,
|
|
|
|
Data: string(code),
|
|
|
|
}
|
|
|
|
codeInner.AppendChild(codeText)
|
|
|
|
tdCode.AppendChild(codeInner)
|
|
|
|
tr.AppendChild(tdCode)
|
|
|
|
|
|
|
|
tbody.AppendChild(tr)
|
|
|
|
}
|
|
|
|
|
|
|
|
table.AppendChild(tbody)
|
|
|
|
|
|
|
|
twrapper := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Div.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "class", Val: "ui table"}},
|
|
|
|
}
|
|
|
|
twrapper.AppendChild(table)
|
|
|
|
|
|
|
|
header := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Div.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "class", Val: "header"}},
|
|
|
|
}
|
2024-04-13 01:30:20 +03:00
|
|
|
|
|
|
|
ptitle := &html.Node{
|
2024-03-16 10:09:49 +03:00
|
|
|
Type: html.ElementNode,
|
2024-04-13 01:30:20 +03:00
|
|
|
Data: atom.Div.String(),
|
2024-03-16 10:09:49 +03:00
|
|
|
}
|
2024-04-13 01:30:20 +03:00
|
|
|
ptitle.AppendChild(&html.Node{
|
|
|
|
Type: html.RawNode,
|
|
|
|
Data: string(p.title),
|
2024-03-16 10:09:49 +03:00
|
|
|
})
|
2024-04-13 01:30:20 +03:00
|
|
|
header.AppendChild(ptitle)
|
2024-03-16 10:09:49 +03:00
|
|
|
|
|
|
|
psubtitle := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Span.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "class", Val: "text small grey"}},
|
|
|
|
}
|
|
|
|
psubtitle.AppendChild(&html.Node{
|
|
|
|
Type: html.RawNode,
|
|
|
|
Data: string(p.subTitle),
|
|
|
|
})
|
|
|
|
header.AppendChild(psubtitle)
|
|
|
|
|
2024-03-16 10:18:47 +03:00
|
|
|
node := &html.Node{
|
2024-03-16 10:09:49 +03:00
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Div.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "class", Val: "file-preview-box"}},
|
|
|
|
}
|
2024-03-16 10:18:47 +03:00
|
|
|
node.AppendChild(header)
|
2024-03-25 18:05:01 +03:00
|
|
|
|
2024-03-27 20:36:12 +03:00
|
|
|
if p.isTruncated {
|
2024-03-25 18:05:01 +03:00
|
|
|
warning := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Div.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "class", Val: "ui warning message tw-text-left"}},
|
|
|
|
}
|
|
|
|
warning.AppendChild(&html.Node{
|
|
|
|
Type: html.TextNode,
|
|
|
|
Data: locale.TrString("markup.filepreview.truncated"),
|
|
|
|
})
|
|
|
|
node.AppendChild(warning)
|
|
|
|
}
|
|
|
|
|
2024-03-16 10:18:47 +03:00
|
|
|
node.AppendChild(twrapper)
|
2024-03-16 10:09:49 +03:00
|
|
|
|
2024-03-16 10:18:47 +03:00
|
|
|
return node
|
2024-03-16 10:09:49 +03:00
|
|
|
}
|