2018-07-20 23:08:15 +02:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-07-20 23:08:15 +02:00
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
2021-04-20 00:25:08 +02:00
|
|
|
"bufio"
|
2018-07-20 23:08:15 +02:00
|
|
|
"bytes"
|
2024-03-19 03:20:36 +01:00
|
|
|
"fmt"
|
2018-07-20 23:08:15 +02:00
|
|
|
"html"
|
|
|
|
"io"
|
2021-06-23 23:09:51 +02:00
|
|
|
"regexp"
|
2021-03-29 22:44:28 +02:00
|
|
|
"strconv"
|
2018-07-20 23:08:15 +02:00
|
|
|
|
2021-03-29 22:44:28 +02:00
|
|
|
"code.gitea.io/gitea/modules/csv"
|
2018-07-20 23:08:15 +02:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2021-03-29 22:44:28 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2018-07-20 23:08:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-04-20 00:25:08 +02:00
|
|
|
markup.RegisterRenderer(Renderer{})
|
2018-07-20 23:08:15 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 00:25:08 +02:00
|
|
|
// Renderer implements markup.Renderer for csv files
|
2022-01-20 18:46:10 +01:00
|
|
|
type Renderer struct{}
|
2018-07-20 23:08:15 +02:00
|
|
|
|
2021-04-20 00:25:08 +02:00
|
|
|
// Name implements markup.Renderer
|
|
|
|
func (Renderer) Name() string {
|
2018-07-20 23:08:15 +02:00
|
|
|
return "csv"
|
|
|
|
}
|
|
|
|
|
2021-04-20 00:25:08 +02:00
|
|
|
// Extensions implements markup.Renderer
|
|
|
|
func (Renderer) Extensions() []string {
|
2019-08-16 00:09:50 +02:00
|
|
|
return []string{".csv", ".tsv"}
|
2018-07-20 23:08:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 23:09:51 +02:00
|
|
|
// SanitizerRules implements markup.Renderer
|
|
|
|
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
|
|
|
|
return []setting.MarkupSanitizerRule{
|
2024-11-18 04:42:30 +01:00
|
|
|
{Element: "table", AllowAttr: "class", Regexp: regexp.MustCompile(`^data-table$`)},
|
|
|
|
{Element: "th", AllowAttr: "class", Regexp: regexp.MustCompile(`^line-num$`)},
|
|
|
|
{Element: "td", AllowAttr: "class", Regexp: regexp.MustCompile(`^line-num$`)},
|
2021-06-23 23:09:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 00:25:08 +02: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 {
|
|
|
|
if _, err := io.WriteString(w, " class=\""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, class); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, "\""); err != nil {
|
|
|
|
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 22:44:28 +02:00
|
|
|
}
|
2021-04-20 00:25:08 +02: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 22:44:28 +02:00
|
|
|
|
2021-04-20 00:25:08 +02:00
|
|
|
// Render implements markup.Renderer
|
2024-03-14 03:51:55 +01:00
|
|
|
func (r Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
|
2022-01-20 18:46:10 +01:00
|
|
|
tmpBlock := bufio.NewWriter(output)
|
2024-03-14 03:51:55 +01:00
|
|
|
maxSize := setting.UI.CSV.MaxFileSize
|
2021-03-29 22:44:28 +02:00
|
|
|
|
2024-03-14 03:51:55 +01:00
|
|
|
if maxSize == 0 {
|
|
|
|
return r.tableRender(ctx, input, tmpBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawBytes, err := io.ReadAll(io.LimitReader(input, maxSize+1))
|
2021-04-20 00:25:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-14 03:51:55 +01:00
|
|
|
if int64(len(rawBytes)) <= maxSize {
|
|
|
|
return r.tableRender(ctx, bytes.NewReader(rawBytes), tmpBlock)
|
|
|
|
}
|
|
|
|
return r.fallbackRender(io.MultiReader(bytes.NewReader(rawBytes), input), tmpBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Renderer) fallbackRender(input io.Reader, tmpBlock *bufio.Writer) error {
|
|
|
|
_, err := tmpBlock.WriteString("<pre>")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
scan := bufio.NewScanner(input)
|
|
|
|
scan.Split(bufio.ScanRunes)
|
|
|
|
for scan.Scan() {
|
|
|
|
switch scan.Text() {
|
|
|
|
case `&`:
|
|
|
|
_, err = tmpBlock.WriteString("&")
|
|
|
|
case `'`:
|
|
|
|
_, err = tmpBlock.WriteString("'") // "'" is shorter than "'" and apos was not in HTML until HTML5.
|
|
|
|
case `<`:
|
|
|
|
_, err = tmpBlock.WriteString("<")
|
|
|
|
case `>`:
|
|
|
|
_, err = tmpBlock.WriteString(">")
|
|
|
|
case `"`:
|
|
|
|
_, err = tmpBlock.WriteString(""") // """ is shorter than """.
|
|
|
|
default:
|
|
|
|
_, err = tmpBlock.Write(scan.Bytes())
|
2021-04-20 00:25:08 +02:00
|
|
|
}
|
2024-03-14 03:51:55 +01:00
|
|
|
if err != nil {
|
2024-03-08 09:44:50 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-04-20 00:25:08 +02:00
|
|
|
}
|
2024-03-22 12:17:30 +01:00
|
|
|
if err = scan.Err(); err != nil {
|
|
|
|
return fmt.Errorf("fallbackRender scan: %w", err)
|
2024-03-19 03:20:36 +01:00
|
|
|
}
|
2021-04-20 00:25:08 +02:00
|
|
|
|
2024-03-14 03:51:55 +01:00
|
|
|
_, err = tmpBlock.WriteString("</pre>")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tmpBlock.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Renderer) tableRender(ctx *markup.RenderContext, input io.Reader, tmpBlock *bufio.Writer) error {
|
|
|
|
rd, err := csv.CreateReaderAndDetermineDelimiter(ctx, input)
|
2021-04-20 00:25:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-03-29 22:44:28 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 00:25:08 +02:00
|
|
|
if _, err := tmpBlock.WriteString(`<table class="data-table">`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-29 22:44:28 +02:00
|
|
|
row := 1
|
2018-07-20 23:08:15 +02:00
|
|
|
for {
|
|
|
|
fields, err := rd.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-20 00:25:08 +02:00
|
|
|
if _, err := tmpBlock.WriteString("<tr>"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-29 22:44:28 +02:00
|
|
|
element := "td"
|
|
|
|
if row == 1 {
|
|
|
|
element = "th"
|
|
|
|
}
|
2021-04-20 00:25:08 +02:00
|
|
|
if err := writeField(tmpBlock, element, "line-num", strconv.Itoa(row)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-20 23:08:15 +02:00
|
|
|
for _, field := range fields {
|
2021-04-20 00:25:08 +02:00
|
|
|
if err := writeField(tmpBlock, element, "", field); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := tmpBlock.WriteString("</tr>"); err != nil {
|
|
|
|
return err
|
2018-07-20 23:08:15 +02:00
|
|
|
}
|
2021-03-29 22:44:28 +02:00
|
|
|
|
|
|
|
row++
|
2018-07-20 23:08:15 +02:00
|
|
|
}
|
2021-04-20 00:25:08 +02:00
|
|
|
if _, err = tmpBlock.WriteString("</table>"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tmpBlock.Flush()
|
2018-07-20 23:08:15 +02:00
|
|
|
}
|