2018-07-21 05:08:15 +08:00
|
|
|
// 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 04:44:28 +08:00
|
|
|
"strconv"
|
2018-07-21 05:08:15 +08:00
|
|
|
|
2021-03-30 04:44:28 +08:00
|
|
|
"code.gitea.io/gitea/modules/csv"
|
2018-07-21 05:08:15 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2021-03-30 04:44:28 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2018-07-21 05:08:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
markup.RegisterParser(Parser{})
|
|
|
|
}
|
|
|
|
|
2021-03-30 04:44:28 +08:00
|
|
|
// Parser implements markup.Parser for csv files
|
2018-07-21 05:08:15 +08:00
|
|
|
type Parser struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements markup.Parser
|
|
|
|
func (Parser) Name() string {
|
|
|
|
return "csv"
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:06:31 +08:00
|
|
|
// NeedPostProcess implements markup.Parser
|
|
|
|
func (Parser) NeedPostProcess() bool { return false }
|
|
|
|
|
2018-07-21 05:08:15 +08:00
|
|
|
// Extensions implements markup.Parser
|
|
|
|
func (Parser) Extensions() []string {
|
2019-08-16 06:09:50 +08:00
|
|
|
return []string{".csv", ".tsv"}
|
2018-07-21 05:08:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render implements markup.Parser
|
2021-03-30 04:44:28 +08:00
|
|
|
func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
|
2018-07-21 05:08:15 +08:00
|
|
|
var tmpBlock bytes.Buffer
|
2021-03-30 04:44:28 +08:00
|
|
|
|
|
|
|
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 05:08:15 +08:00
|
|
|
for {
|
|
|
|
fields, err := rd.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tmpBlock.WriteString("<tr>")
|
2021-03-30 04:44:28 +08:00
|
|
|
element := "td"
|
|
|
|
if row == 1 {
|
|
|
|
element = "th"
|
|
|
|
}
|
|
|
|
writeField(element, "line-num", strconv.Itoa(row))
|
2018-07-21 05:08:15 +08:00
|
|
|
for _, field := range fields {
|
2021-03-30 04:44:28 +08:00
|
|
|
writeField(element, "", field)
|
2018-07-21 05:08:15 +08:00
|
|
|
}
|
2019-08-16 06:09:50 +08:00
|
|
|
tmpBlock.WriteString("</tr>")
|
2021-03-30 04:44:28 +08:00
|
|
|
|
|
|
|
row++
|
2018-07-21 05:08:15 +08:00
|
|
|
}
|
|
|
|
tmpBlock.WriteString("</table>")
|
|
|
|
|
|
|
|
return tmpBlock.Bytes()
|
|
|
|
}
|