-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_markdown.go
147 lines (123 loc) · 2.79 KB
/
io_markdown.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package gandalff
import (
"fmt"
"io"
"os"
"github.com/caerbannogwhite/preludiometa"
)
type MarkDownWriter struct {
header bool
index bool
types bool
path string
naText string
writer io.Writer
dataframe DataFrame
}
func NewMarkDownWriter() *MarkDownWriter {
return &MarkDownWriter{
header: true,
index: false,
path: "",
naText: NA_TEXT,
writer: nil,
dataframe: nil,
}
}
func (w *MarkDownWriter) SetHeader(header bool) *MarkDownWriter {
w.header = header
return w
}
func (w *MarkDownWriter) SetIndex(index bool) *MarkDownWriter {
w.index = index
return w
}
func (w *MarkDownWriter) SetPath(path string) *MarkDownWriter {
w.path = path
return w
}
func (w *MarkDownWriter) SetNaText(naText string) *MarkDownWriter {
w.naText = naText
return w
}
func (w *MarkDownWriter) SetWriter(writer io.Writer) *MarkDownWriter {
w.writer = writer
return w
}
func (w *MarkDownWriter) SetDataFrame(dataframe DataFrame) *MarkDownWriter {
w.dataframe = dataframe
return w
}
func (w *MarkDownWriter) Write() DataFrame {
if w.dataframe == nil {
w.dataframe = BaseDataFrame{err: fmt.Errorf("writeMarkDown: no dataframe specified"), ctx: w.dataframe.GetContext()}
return w.dataframe
}
if w.dataframe.IsErrored() {
return w.dataframe
}
if w.path != "" {
file, err := os.OpenFile(w.path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return BaseDataFrame{err: err, ctx: w.dataframe.GetContext()}
}
defer file.Close()
w.writer = file
}
if w.writer == nil {
w.dataframe = BaseDataFrame{err: fmt.Errorf("writeMarkDown: no writer specified"), ctx: w.dataframe.GetContext()}
return w.dataframe
}
err := writeMarkDown(w.dataframe, w.writer, w.header, w.index, w.naText)
if err != nil {
w.dataframe = BaseDataFrame{err: err, ctx: w.dataframe.GetContext()}
}
return w.dataframe
}
func writeMarkDown(dataframe DataFrame, writer io.Writer, header, index bool, naText string) error {
buff := ""
if header {
buff += "|"
if index {
buff += " |"
}
for _, col := range dataframe.Names() {
buff += "**" + col + "**|"
}
buff += "\n"
}
buff += "|"
if index {
buff += "----:|"
}
for i := range dataframe.Names() {
switch dataframe.Types()[i].GetDefaultJustification() {
case preludiometa.JUSTIFY_LEFT:
buff += ":----|"
case preludiometa.JUSTIFY_RIGHT:
buff += "----:|"
case preludiometa.JUSTIFY_CENTER:
buff += ":---:|"
}
}
buff += "\n"
for i := 0; i < dataframe.NRows(); i++ {
buff += "|"
if index {
buff += fmt.Sprintf("%d|", i)
}
for j := 0; j < dataframe.NCols(); j++ {
if dataframe.At(j).IsNull(i) {
buff += naText + "|"
} else {
buff += dataframe.At(j).GetAsString(i) + "|"
}
}
buff += "\n"
}
_, err := writer.Write([]byte(buff))
if err != nil {
return err
}
return nil
}