-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.go
executable file
·76 lines (61 loc) · 1.48 KB
/
export.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
package main
import (
"bufio"
// "github.com/gocarina/gocsv"
"github.com/atotto/encoding/csv"
"github.com/jszroberto/kindle-words/kindledb"
"os"
"strings"
)
func exportVocabularyCom(path string, words []kindledb.Word) error {
outputs := map[string]string{}
for _, word := range words {
if word.IsEnglish() {
if outputs[word.Book] != "" {
outputs[word.Book] += ","
}
outputs[word.Book] += strings.ToLower(word.Value)
}
}
if err := os.MkdirAll(path+"/", 0755); err != nil {
panic("Unable to create directory for tagfile! - " + err.Error())
}
for key, value := range outputs {
file, err := os.Create(path + "/" + key)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
_, err = writer.WriteString(value)
if err != nil {
return err
}
writer.Flush()
}
return nil
}
func exportHtml(path string, words []kindledb.Word) error {
return exportToFolders(path, "html", "templates/book.html", words, true)
}
func exportEvernote(path string, words []kindledb.Word) error {
return exportToFolders(path, "html", "templates/evernote.html", words, false)
}
func exportToCSV(path string, words []kindledb.Word) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := csv.NewWriter(file)
w.WriteStructAll(words) // calls Flush internally
if err := w.Error(); err != nil {
return err
}
// err = gocsv.MarshalFile(&words, file)
//
// if err != nil {
// return err
// }
return nil
}