2018-07-21 02:38:15 +05:30
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
2021-04-20 03:55:08 +05:30
|
|
|
"bufio"
|
2018-07-21 02:38:15 +05:30
|
|
|
"bytes"
|
|
|
|
"html"
|
|
|
|
"io"
|
2021-04-20 03:55:08 +05:30
|
|
|
"io/ioutil"
|
2021-06-24 02:39:51 +05:30
|
|
|
"regexp"
|
2021-03-30 02:14:28 +05:30
|
|
|
"strconv"
|
2018-07-21 02:38:15 +05:30
|
|
|
|
2021-03-30 02:14:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/csv"
|
2018-07-21 02:38:15 +05:30
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2021-03-30 02:14:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2018-07-21 02:38:15 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-04-20 03:55:08 +05:30
|
|
|
markup.RegisterRenderer(Renderer{})
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// Renderer implements markup.Renderer for csv files
|
|
|
|
type Renderer struct {
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// Name implements markup.Renderer
|
|
|
|
func (Renderer) Name() string {
|
2018-07-21 02:38:15 +05:30
|
|
|
return "csv"
|
|
|
|
}
|
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// NeedPostProcess implements markup.Renderer
|
|
|
|
func (Renderer) NeedPostProcess() bool { return false }
|
2021-04-13 12:36:31 +05:30
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// Extensions implements markup.Renderer
|
|
|
|
func (Renderer) Extensions() []string {
|
2019-08-16 03:39:50 +05:30
|
|
|
return []string{".csv", ".tsv"}
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
|
|
|
|
2021-06-24 02:39:51 +05:30
|
|
|
// SanitizerRules implements markup.Renderer
|
|
|
|
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
|
|
|
|
return []setting.MarkupSanitizerRule{
|
|
|
|
{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-04-20 03:55:08 +05:30
|
|
|
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-30 02:14:28 +05:30
|
|
|
}
|
2021-04-20 03:55:08 +05:30
|
|
|
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-30 02:14:28 +05:30
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// Render implements markup.Renderer
|
|
|
|
func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
|
|
|
|
var tmpBlock = bufio.NewWriter(output)
|
2021-03-30 02:14:28 +05:30
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// FIXME: don't read all to memory
|
|
|
|
rawBytes, err := ioutil.ReadAll(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < int64(len(rawBytes)) {
|
|
|
|
if _, err := tmpBlock.WriteString("<pre>"); err != nil {
|
|
|
|
return err
|
2021-03-30 02:14:28 +05:30
|
|
|
}
|
2021-04-20 03:55:08 +05:30
|
|
|
if _, err := tmpBlock.WriteString(html.EscapeString(string(rawBytes))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tmpBlock.WriteString("</pre>")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rd, err := csv.CreateReaderAndGuessDelimiter(bytes.NewReader(rawBytes))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-03-30 02:14:28 +05:30
|
|
|
}
|
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
if _, err := tmpBlock.WriteString(`<table class="data-table">`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-30 02:14:28 +05:30
|
|
|
row := 1
|
2018-07-21 02:38:15 +05:30
|
|
|
for {
|
|
|
|
fields, err := rd.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-20 03:55:08 +05:30
|
|
|
if _, err := tmpBlock.WriteString("<tr>"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-30 02:14:28 +05:30
|
|
|
element := "td"
|
|
|
|
if row == 1 {
|
|
|
|
element = "th"
|
|
|
|
}
|
2021-04-20 03:55:08 +05:30
|
|
|
if err := writeField(tmpBlock, element, "line-num", strconv.Itoa(row)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-21 02:38:15 +05:30
|
|
|
for _, field := range fields {
|
2021-04-20 03:55:08 +05:30
|
|
|
if err := writeField(tmpBlock, element, "", field); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := tmpBlock.WriteString("</tr>"); err != nil {
|
|
|
|
return err
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
2021-03-30 02:14:28 +05:30
|
|
|
|
|
|
|
row++
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
2021-04-20 03:55:08 +05:30
|
|
|
if _, err = tmpBlock.WriteString("</table>"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tmpBlock.Flush()
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|