2018-07-20 17:08:15 -04:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-07-20 17:08:15 -04:00
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
2021-04-19 18:25:08 -04:00
|
|
|
"bufio"
|
2018-07-20 17:08:15 -04:00
|
|
|
"html"
|
|
|
|
"io"
|
2021-03-29 16:44:28 -04:00
|
|
|
"strconv"
|
2018-07-20 17:08:15 -04:00
|
|
|
|
2021-03-29 16:44:28 -04:00
|
|
|
"code.gitea.io/gitea/modules/csv"
|
2018-07-20 17:08:15 -04:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2021-03-29 16:44:28 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-06-06 04:06:59 -04:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2018-07-20 17:08:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-04-19 18:25:08 -04:00
|
|
|
markup.RegisterRenderer(Renderer{})
|
2018-07-20 17:08:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
// Renderer implements markup.Renderer for csv files
|
2022-01-20 12:46:10 -05:00
|
|
|
type Renderer struct{}
|
2018-07-20 17:08:15 -04:00
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
// Name implements markup.Renderer
|
|
|
|
func (Renderer) Name() string {
|
2018-07-20 17:08:15 -04:00
|
|
|
return "csv"
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
// Extensions implements markup.Renderer
|
|
|
|
func (Renderer) Extensions() []string {
|
2019-08-15 18:09:50 -04:00
|
|
|
return []string{".csv", ".tsv"}
|
2018-07-20 17:08:15 -04:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:09:51 -04:00
|
|
|
// SanitizerRules implements markup.Renderer
|
|
|
|
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
|
|
|
|
return []setting.MarkupSanitizerRule{
|
2024-11-18 00:25:42 -05:00
|
|
|
{Element: "table", AllowAttr: "class", Regexp: `^data-table$`},
|
|
|
|
{Element: "th", AllowAttr: "class", Regexp: `^line-num$`},
|
|
|
|
{Element: "td", AllowAttr: "class", Regexp: `^line-num$`},
|
2021-06-23 17:09:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
func writeField(w io.Writer, element, class, field string) error {
|
|
|
|
if _, err := io.WriteString(w, "<"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, element); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(class) > 0 {
|
2024-11-18 00:25:42 -05:00
|
|
|
if _, err := io.WriteString(w, ` class="`); err != nil {
|
2021-04-19 18:25:08 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, class); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-18 00:25:42 -05:00
|
|
|
if _, err := io.WriteString(w, `"`); err != nil {
|
2021-04-19 18:25:08 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, ">"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, html.EscapeString(field)); err != nil {
|
|
|
|
return err
|
2021-03-29 16:44:28 -04:00
|
|
|
}
|
2021-04-19 18:25:08 -04:00
|
|
|
if _, err := io.WriteString(w, "</"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, element); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.WriteString(w, ">")
|
|
|
|
return err
|
|
|
|
}
|
2021-03-29 16:44:28 -04:00
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
// Render implements markup.Renderer
|
2024-03-13 22:51:55 -04:00
|
|
|
func (r Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
|
2022-01-20 12:46:10 -05:00
|
|
|
tmpBlock := bufio.NewWriter(output)
|
2024-03-13 22:51:55 -04:00
|
|
|
maxSize := setting.UI.CSV.MaxFileSize
|
2024-06-06 04:06:59 -04:00
|
|
|
maxRows := setting.UI.CSV.MaxRows
|
2021-03-29 16:44:28 -04:00
|
|
|
|
2024-06-06 04:06:59 -04:00
|
|
|
if maxSize != 0 {
|
|
|
|
input = io.LimitReader(input, maxSize+1)
|
2024-03-13 22:51:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rd, err := csv.CreateReaderAndDetermineDelimiter(ctx, input)
|
2021-04-19 18:25:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-03-29 16:44:28 -04:00
|
|
|
}
|
2021-04-19 18:25:08 -04:00
|
|
|
if _, err := tmpBlock.WriteString(`<table class="data-table">`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-06 04:06:59 -04:00
|
|
|
|
|
|
|
row := 0
|
2018-07-20 17:08:15 -04:00
|
|
|
for {
|
|
|
|
fields, err := rd.Read()
|
2024-06-06 04:06:59 -04:00
|
|
|
if err == io.EOF || (row >= maxRows && maxRows != 0) {
|
2018-07-20 17:08:15 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-06 04:06:59 -04:00
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
if _, err := tmpBlock.WriteString("<tr>"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-29 16:44:28 -04:00
|
|
|
element := "td"
|
2024-06-06 04:06:59 -04:00
|
|
|
if row == 0 {
|
2021-03-29 16:44:28 -04:00
|
|
|
element = "th"
|
|
|
|
}
|
2024-06-06 04:06:59 -04:00
|
|
|
if err := writeField(tmpBlock, element, "line-num", strconv.Itoa(row+1)); err != nil {
|
2021-04-19 18:25:08 -04:00
|
|
|
return err
|
|
|
|
}
|
2018-07-20 17:08:15 -04:00
|
|
|
for _, field := range fields {
|
2021-04-19 18:25:08 -04:00
|
|
|
if err := writeField(tmpBlock, element, "", field); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := tmpBlock.WriteString("</tr>"); err != nil {
|
|
|
|
return err
|
2018-07-20 17:08:15 -04:00
|
|
|
}
|
2021-03-29 16:44:28 -04:00
|
|
|
|
|
|
|
row++
|
2018-07-20 17:08:15 -04:00
|
|
|
}
|
2024-06-06 04:06:59 -04:00
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
if _, err = tmpBlock.WriteString("</table>"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-06 04:06:59 -04:00
|
|
|
|
|
|
|
// 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>`
|
2024-11-22 00:48:09 -05:00
|
|
|
rawLink := ` <a href="` + ctx.RenderOptions.Links.RawLink() + `/` + util.PathEscapeSegments(ctx.RenderOptions.RelativePath) + `">`
|
2024-06-06 04:06:59 -04:00
|
|
|
|
|
|
|
// Try to get the user translation
|
2024-11-22 00:48:09 -05:00
|
|
|
if locale, ok := ctx.Value(translation.ContextKey).(translation.Locale); ok {
|
2024-06-06 04:06:59 -04:00
|
|
|
warn += locale.TrString("repo.file_too_large")
|
|
|
|
rawLink += locale.TrString("repo.file_view_raw")
|
|
|
|
} else {
|
|
|
|
warn += "The file is too large to be shown."
|
|
|
|
rawLink += "View Raw"
|
|
|
|
}
|
|
|
|
|
|
|
|
warn += rawLink + `</a></td></tr></table>`
|
|
|
|
|
|
|
|
// Write the HTML string to the output
|
|
|
|
if _, err := tmpBlock.WriteString(warn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:25:08 -04:00
|
|
|
return tmpBlock.Flush()
|
2018-07-20 17:08:15 -04:00
|
|
|
}
|