-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
190 lines (140 loc) · 4.01 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package mankidown
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"
)
// Anki header fields.
// See https://docs.ankiweb.net/importing/text-files.html#file-headers
const (
HeaderSeparator = "#separator:Pipe"
HeaderHtml = "#html:true"
HeaderGuidColumn = "#guid column:1"
HeaderTagColumn = "#tags column:2"
HeaderDeck = "#deck:%s\n"
HeaderNoteType = "#notetype:%s\n"
HeaderTags = "#tags:%s\n"
separatorChar = `|`
guidColumnName = "mankidown-Guid"
tagsColumnName = "Tags"
outFileExt = ".txt"
mankidownTag = "mankidown"
// Anki Import UI wants each note type field to be non empty. We allow
// empty fields in markdown and add emptyComment in order to fill the fields
// with something
emptyComment = "<!---->"
)
type Config struct {
GuidPrefix string
InFile string
Deck string
NoteType string
Tags []string
}
type Exporter struct {
config *Config
}
func NewExporter(config *Config) *Exporter {
return &Exporter{config: config}
}
func (ex *Exporter) Export(notes *Notes) error {
var err error
inFileBaseName := filepath.Base(ex.config.InFile)
inFile := strings.TrimSuffix(inFileBaseName, filepath.Ext(inFileBaseName))
outFile := inFile + outFileExt
var out io.Writer
f, err := os.OpenFile(outFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to open output file %q: %v", outFile, err)
}
defer func() {
if err = f.Close(); err != nil {
err = fmt.Errorf("failed to close output file %q: %v", outFile, err)
}
}()
out = f
ex.appendHeaders(out)
ex.appendHeaderColumns(out, notes.FieldNames())
ex.appendHeaderTags(out, inFile)
for i, note := range notes.Notes {
// 1 field) id
fields := []string{}
fields = append(fields, ex.buildIdField(note, i, outFile))
// 2 field) tags
fields = append(fields, buildTagsField(note))
for _, field := range note.Fields() {
fields = append(fields, buildFieldAsLine(field.Html))
}
noteline := strings.Join(fields, separatorChar)
fmt.Fprintf(out, "%s\n", noteline)
}
return nil
}
func (ex *Exporter) appendHeaders(out io.Writer) {
// separator
fmt.Fprintln(out, HeaderSeparator)
// html
fmt.Fprintln(out, HeaderHtml)
// guid column
fmt.Fprintln(out, HeaderGuidColumn)
// tag column
fmt.Fprintln(out, HeaderTagColumn)
// deck
if ex.config.Deck != "" {
fmt.Fprintf(out, HeaderDeck, ex.config.Deck)
}
// notetype
if ex.config.NoteType != "" {
fmt.Fprintf(out, HeaderNoteType, ex.config.NoteType)
}
}
func (ex *Exporter) appendHeaderColumns(out io.Writer, columns []string) {
cols := []string{}
// prepend id as first (anki duplication)
cols = append(cols, guidColumnName) //1)
cols = append(cols, tagsColumnName) //2)
cols = append(cols, columns...)
c := strings.Join(cols, separatorChar)
fmt.Fprintf(out, "#columns:%s\n", c)
}
func (ex *Exporter) appendHeaderTags(out io.Writer, inFile string) {
tags := ex.config.Tags
// Split the inFile words
f := func(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}
tagsFromInFile := strings.FieldsFunc(inFile, f)
tags = append(tags, tagsFromInFile...)
tags = append(tags, mankidownTag)
fmt.Fprintf(out, HeaderTags, strings.Join(tags, " "))
}
func (ex *Exporter) buildIdField(n *Note, i int, outFile string) string {
if n.hasGuid() {
return n.Guid()
}
if ex.config.GuidPrefix != "" {
return ex.config.GuidPrefix + strconv.Itoa(i)
}
return outFile + strconv.Itoa(i)
}
// buildTagsField builds the Tags string for a note
func buildTagsField(n *Note) string {
return strings.Join(n.Tags(), " ")
}
func buildFieldAsLine(html string) string {
// 1) replace all but the last \n of the field,
occurrencesCount := strings.Count(html, "\n")
fieldLine := strings.Replace(html, "\n", "<br>", occurrencesCount-1)
// 2) replace the last one with nothing
fieldLine = strings.Replace(fieldLine, "\n", "", -1)
// 3) delete <br> between tags
fieldLine = strings.Replace(fieldLine, "><br><", "><", -1)
if fieldLine == "" {
return emptyComment
}
return fieldLine
}