Skip to content

Commit 5304fa6

Browse files
committed
add csv file render support defaultly
1 parent 0c59eda commit 5304fa6

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/log"
1515
"code.gitea.io/gitea/modules/setting"
1616
// register supported doc types
17+
_ "code.gitea.io/gitea/modules/markup/csv"
1718
_ "code.gitea.io/gitea/modules/markup/markdown"
1819
_ "code.gitea.io/gitea/modules/markup/orgmode"
1920

modules/markup/csv/csv.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package markup
6+
7+
import (
8+
"bytes"
9+
"encoding/csv"
10+
"io"
11+
12+
"code.gitea.io/gitea/modules/markup"
13+
)
14+
15+
func init() {
16+
markup.RegisterParser(Parser{})
17+
}
18+
19+
// Parser implements markup.Parser for orgmode
20+
type Parser struct {
21+
}
22+
23+
// Name implements markup.Parser
24+
func (Parser) Name() string {
25+
return "csv"
26+
}
27+
28+
// Extensions implements markup.Parser
29+
func (Parser) Extensions() []string {
30+
return []string{".csv"}
31+
}
32+
33+
// Render implements markup.Parser
34+
func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isUncyclo bool) []byte {
35+
rd := csv.NewReader(bytes.NewReader(rawBytes))
36+
var tmpBlock bytes.Buffer
37+
tmpBlock.WriteString(`<table class="table">`)
38+
for {
39+
fields, err := rd.Read()
40+
if err == io.EOF {
41+
break
42+
}
43+
if err != nil {
44+
continue
45+
}
46+
tmpBlock.WriteString("<tr>")
47+
for _, field := range fields {
48+
tmpBlock.WriteString("<td>")
49+
tmpBlock.WriteString(field)
50+
tmpBlock.WriteString("</td>")
51+
}
52+
tmpBlock.WriteString("<tr>")
53+
}
54+
tmpBlock.WriteString("</table>")
55+
56+
return tmpBlock.Bytes()
57+
}

0 commit comments

Comments
 (0)