-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (69 loc) · 2.27 KB
/
main.go
File metadata and controls
83 lines (69 loc) · 2.27 KB
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
package main
import (
"fmt"
"log"
"github.com/lftk/anki"
)
func main() {
// Create a new, empty collection.
col, err := anki.Create()
if err != nil {
log.Fatalf("failed to create collection: %v", err)
}
defer col.Close()
// Create a new deck with a meaningful name for vocabulary learning.
deck := &anki.Deck{
Name: anki.DeckName("English-Chinese Vocab"),
}
if err = col.AddDeck(deck); err != nil {
log.Fatalf("failed to add deck: %v", err)
}
// --- Define the structure for our vocabulary notes (Notetype) ---
// Define the Question and Answer format for the cards using meaningful field names.
qfmt := `<div class="card-front">{{english}}</div>`
afmt := `{{FrontSide}}<hr id="answer"><div class="card-back">{{chinese}}</div>`
// Define some basic CSS for card styling.
style := `.card { font-family: arial; font-size: 24px; text-align: center; color: black; background-color: white; }`
// Create the Notetype, which acts as a blueprint for our notes.
notetype := &anki.Notetype{
Name: "Vocabulary",
// Define the fields that each note will have.
Fields: []*anki.Field{
anki.NewField("english"),
anki.NewField("chinese"),
},
// Define the card templates. We only need one for a basic note.
Templates: []*anki.Template{
anki.NewTemplate("Card 1", qfmt, afmt),
},
// Apply the styling and specify it's not a cloze-deletion type.
Config: anki.NewNotetypeConfig(style, false),
}
// Add the Notetype to the collection.
if err = col.AddNotetype(notetype); err != nil {
log.Fatalf("failed to add notetype: %v", err)
}
// --- Create and add the actual notes ---
// A list of vocabulary pairs to add as notes.
// The fields must be in the same order as defined in the Notetype.
notes := [][]string{
{"Hello", "你好"},
{"World", "世界"},
}
// Loop through the list and create a note for each vocabulary pair.
for _, fields := range notes {
note := &anki.Note{
NotetypeID: notetype.ID,
Fields: fields,
}
// Add the note to our deck.
if err = col.AddNote(deck.ID, note); err != nil {
log.Fatalf("failed to add note: %v", err)
}
}
// Save the collection as an .apkg file.
if err = col.SaveAs("vocab.apkg"); err != nil {
log.Fatalf("failed to save collection: %v", err)
}
fmt.Println("Successfully created vocab.apkg")
}