You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

csv.go 1.2KB

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