-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter.go
60 lines (47 loc) · 1.07 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"encoding/csv"
"os"
bf "github.com/russross/blackfriday/v2"
)
const (
HTML Option = 1 << iota
)
type Option int
type DeckWriter struct {
fp *os.File
options Option
}
// NewDeckWriter return the DeckWriter struct which contain a filepointer `fp`
// to which the Deck can be written to.
func NewDeckWriter(fp *os.File, options Option) *DeckWriter {
return &DeckWriter{fp: fp, options: options}
}
// WriteDeck will write the Deck to the filepointer `fp` in a specified format
func (dw *DeckWriter) WriteDeck(d *Deck) error {
err := dw.writeToCSV(d)
if err != nil {
return err
}
return nil
}
// writeToCSV will write the Deck (AST) in the specified csv format
func (dw *DeckWriter) writeToCSV(d *Deck) error {
csvWriter := csv.NewWriter(dw.fp)
defer csvWriter.Flush()
for _, c := range d.Cards {
var row []string
for _, f := range c.Fields {
var c string
switch dw.options {
case HTML:
c = string(bf.Run([]byte(f.Content)))
default:
c = f.Content
}
row = append(row, c)
}
csvWriter.Write(row)
}
return nil
}