THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Browse Source

Add csv file render support defaultly (#4105)

* add csv file render support defaultly

* escaping csv column content
pull/4495/head
Lunny Xiao 11 months ago
parent
commit
0bb1c84208
3 changed files with 84 additions and 0 deletions
  1. 1
    0
      main.go
  2. 58
    0
      modules/markup/csv/csv.go
  3. 25
    0
      modules/markup/csv/csv_test.go

+ 1
- 0
main.go View File

@@ -14,6 +14,7 @@ import (
14 14
 	"code.gitea.io/gitea/modules/log"
15 15
 	"code.gitea.io/gitea/modules/setting"
16 16
 	// register supported doc types
17
+	_ "code.gitea.io/gitea/modules/markup/csv"
17 18
 	_ "code.gitea.io/gitea/modules/markup/markdown"
18 19
 	_ "code.gitea.io/gitea/modules/markup/orgmode"
19 20
 

+ 58
- 0
modules/markup/csv/csv.go View File

@@ -0,0 +1,58 @@
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
+	"html"
11
+	"io"
12
+
13
+	"code.gitea.io/gitea/modules/markup"
14
+)
15
+
16
+func init() {
17
+	markup.RegisterParser(Parser{})
18
+}
19
+
20
+// Parser implements markup.Parser for orgmode
21
+type Parser struct {
22
+}
23
+
24
+// Name implements markup.Parser
25
+func (Parser) Name() string {
26
+	return "csv"
27
+}
28
+
29
+// Extensions implements markup.Parser
30
+func (Parser) Extensions() []string {
31
+	return []string{".csv"}
32
+}
33
+
34
+// Render implements markup.Parser
35
+func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
36
+	rd := csv.NewReader(bytes.NewReader(rawBytes))
37
+	var tmpBlock bytes.Buffer
38
+	tmpBlock.WriteString(`<table class="table">`)
39
+	for {
40
+		fields, err := rd.Read()
41
+		if err == io.EOF {
42
+			break
43
+		}
44
+		if err != nil {
45
+			continue
46
+		}
47
+		tmpBlock.WriteString("<tr>")
48
+		for _, field := range fields {
49
+			tmpBlock.WriteString("<td>")
50
+			tmpBlock.WriteString(html.EscapeString(field))
51
+			tmpBlock.WriteString("</td>")
52
+		}
53
+		tmpBlock.WriteString("<tr>")
54
+	}
55
+	tmpBlock.WriteString("</table>")
56
+
57
+	return tmpBlock.Bytes()
58
+}

+ 25
- 0
modules/markup/csv/csv_test.go View File

@@ -0,0 +1,25 @@
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
+	"testing"
9
+
10
+	"github.com/stretchr/testify/assert"
11
+)
12
+
13
+func TestRenderCSV(t *testing.T) {
14
+	var parser Parser
15
+	var kases = map[string]string{
16
+		"a":     "<table class=\"table\"><tr><td>a</td><tr></table>",
17
+		"1,2":   "<table class=\"table\"><tr><td>1</td><td>2</td><tr></table>",
18
+		"<br/>": "<table class=\"table\"><tr><td>&lt;br/&gt;</td><tr></table>",
19
+	}
20
+
21
+	for k, v := range kases {
22
+		res := parser.Render([]byte(k), "", nil, false)
23
+		assert.EqualValues(t, v, string(res))
24
+	}
25
+}

Loading…
Cancel
Save