|
| 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