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 (
|
|
|
|
"bytes"
|
|
|
|
"html"
|
|
|
|
"io"
|
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() {
|
|
|
|
markup.RegisterParser(Parser{})
|
|
|
|
}
|
|
|
|
|
2021-03-30 02:14:28 +05:30
|
|
|
// Parser implements markup.Parser for csv files
|
2018-07-21 02:38:15 +05:30
|
|
|
type Parser struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements markup.Parser
|
|
|
|
func (Parser) Name() string {
|
|
|
|
return "csv"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extensions implements markup.Parser
|
|
|
|
func (Parser) Extensions() []string {
|
2019-08-16 03:39:50 +05:30
|
|
|
return []string{".csv", ".tsv"}
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Render implements markup.Parser
|
2021-03-30 02:14:28 +05:30
|
|
|
func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
|
2018-07-21 02:38:15 +05:30
|
|
|
var tmpBlock bytes.Buffer
|
2021-03-30 02:14:28 +05:30
|
|
|
|
|
|
|
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < int64(len(rawBytes)) {
|
|
|
|
tmpBlock.WriteString("<pre>")
|
|
|
|
tmpBlock.WriteString(html.EscapeString(string(rawBytes)))
|
|
|
|
tmpBlock.WriteString("</pre>")
|
|
|
|
return tmpBlock.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
rd := csv.CreateReaderAndGuessDelimiter(rawBytes)
|
|
|
|
|
|
|
|
writeField := func(element, class, field string) {
|
|
|
|
tmpBlock.WriteString("<")
|
|
|
|
tmpBlock.WriteString(element)
|
|
|
|
if len(class) > 0 {
|
|
|
|
tmpBlock.WriteString(" class=\"")
|
|
|
|
tmpBlock.WriteString(class)
|
|
|
|
tmpBlock.WriteString("\"")
|
|
|
|
}
|
|
|
|
tmpBlock.WriteString(">")
|
|
|
|
tmpBlock.WriteString(html.EscapeString(field))
|
|
|
|
tmpBlock.WriteString("</")
|
|
|
|
tmpBlock.WriteString(element)
|
|
|
|
tmpBlock.WriteString(">")
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpBlock.WriteString(`<table class="data-table">`)
|
|
|
|
row := 1
|
2018-07-21 02:38:15 +05:30
|
|
|
for {
|
|
|
|
fields, err := rd.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tmpBlock.WriteString("<tr>")
|
2021-03-30 02:14:28 +05:30
|
|
|
element := "td"
|
|
|
|
if row == 1 {
|
|
|
|
element = "th"
|
|
|
|
}
|
|
|
|
writeField(element, "line-num", strconv.Itoa(row))
|
2018-07-21 02:38:15 +05:30
|
|
|
for _, field := range fields {
|
2021-03-30 02:14:28 +05:30
|
|
|
writeField(element, "", field)
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
2019-08-16 03:39:50 +05:30
|
|
|
tmpBlock.WriteString("</tr>")
|
2021-03-30 02:14:28 +05:30
|
|
|
|
|
|
|
row++
|
2018-07-21 02:38:15 +05:30
|
|
|
}
|
|
|
|
tmpBlock.WriteString("</table>")
|
|
|
|
|
|
|
|
return tmpBlock.Bytes()
|
|
|
|
}
|