summaryrefslogtreecommitdiff
path: root/internal/processor
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-08-02 15:08:33 +0300
committerPaul Buetow <paul@buetow.org>2025-08-02 15:08:33 +0300
commit310ad64d1ad0b6e30a7dfaab0344dd78cabae463 (patch)
tree41d51e7ab49f2bbbb0bb0a13faf9678cccaf34c3 /internal/processor
parent18a475657cbc7b2ff8ee537b082eeef25e9bf619 (diff)
also export reverse cards via apkg
Diffstat (limited to 'internal/processor')
-rw-r--r--internal/processor/processor.go47
-rw-r--r--internal/processor/processor_test.go27
2 files changed, 56 insertions, 18 deletions
diff --git a/internal/processor/processor.go b/internal/processor/processor.go
index 1e1c20f..4bb6b0a 100644
--- a/internal/processor/processor.go
+++ b/internal/processor/processor.go
@@ -415,16 +415,45 @@ func (p *Processor) GenerateAnkiFile() (string, error) {
AudioFormat: p.flags.AudioFormat,
})
- // Generate cards from output directory
- if err := gen.GenerateFromDirectory(p.flags.OutputDir); err != nil {
- return "", fmt.Errorf("failed to generate cards: %w", err)
- }
-
- // Add translations to cards
+ // Use the translation cache as the source of truth for cards
translations := p.translationCache.GetAll()
- for i := range gen.GetCards() {
- if translation, ok := translations[gen.GetCards()[i].Bulgarian]; ok {
- gen.GetCards()[i].Translation = translation
+ if len(translations) == 0 {
+ fmt.Println(" No translations found in cache, generating cards from directory...")
+ // Fallback to old method if cache is empty but files might exist
+ if err := gen.GenerateFromDirectory(p.flags.OutputDir); err != nil {
+ return "", fmt.Errorf("failed to generate cards from directory: %w", err)
+ }
+ } else {
+ fmt.Printf(" Generating cards from %d translations in cache...\n", len(translations))
+ for bulgarian, english := range translations {
+ card := anki.Card{
+ Bulgarian: bulgarian,
+ Translation: english,
+ }
+
+ // Find associated media files in the output directory
+ wordDir := p.findCardDirectory(bulgarian)
+ if wordDir != "" {
+ // Look for audio file
+ audioFile := filepath.Join(wordDir, fmt.Sprintf("audio.%s", p.flags.AudioFormat))
+ if _, err := os.Stat(audioFile); err == nil {
+ card.AudioFile = audioFile
+ }
+
+ // Look for image file
+ imageFile := filepath.Join(wordDir, "image.jpg") // Assuming jpg, adjust if needed
+ if _, err := os.Stat(imageFile); err == nil {
+ card.ImageFile = imageFile
+ }
+
+ // Load phonetic information as notes
+ phoneticFile := filepath.Join(wordDir, "phonetic.txt")
+ if data, err := os.ReadFile(phoneticFile); err == nil {
+ notes := strings.TrimSpace(string(data))
+ card.Notes = strings.ReplaceAll(notes, "\n", "<br>")
+ }
+ }
+ gen.AddCard(card)
}
}
diff --git a/internal/processor/processor_test.go b/internal/processor/processor_test.go
index 26ab71b..40f5cbe 100644
--- a/internal/processor/processor_test.go
+++ b/internal/processor/processor_test.go
@@ -208,16 +208,22 @@ func TestGenerateAnkiFile(t *testing.T) {
p.translationCache.Add("ябълка", "apple")
p.translationCache.Add("котка", "cat")
+ // Create dummy word directories and files
+ p.findOrCreateWordDirectory("ябълка")
+ p.findOrCreateWordDirectory("котка")
+
_, err := p.GenerateAnkiFile()
if err != nil {
t.Errorf("GenerateAnkiFile failed: %v", err)
}
- // Check CSV file was created
- csvFile := filepath.Join(flags.OutputDir, "anki_import.csv")
+ // Check CSV file was created in home directory
+ homeDir, _ := os.UserHomeDir()
+ csvFile := filepath.Join(homeDir, "anki_import.csv")
if _, err := os.Stat(csvFile); os.IsNotExist(err) {
- t.Error("CSV file was not created")
+ t.Error("CSV file was not created in home directory")
}
+ os.Remove(csvFile) // Clean up
}
func TestGenerateAnkiFile_APKG(t *testing.T) {
@@ -236,18 +242,21 @@ func TestGenerateAnkiFile_APKG(t *testing.T) {
p.translationCache.Add("ябълка", "apple")
p.translationCache.Add("котка", "cat")
- // Create dummy audio files
- os.WriteFile(filepath.Join(word1Dir, "ябълка.mp3"), []byte("audio1"), 0644)
- os.WriteFile(filepath.Join(word2Dir, "котка.mp3"), []byte("audio2"), 0644)
+ // Create dummy audio and image files
+ os.WriteFile(filepath.Join(word1Dir, "audio.mp3"), []byte("audio1"), 0644)
+ os.WriteFile(filepath.Join(word2Dir, "audio.mp3"), []byte("audio2"), 0644)
+ os.WriteFile(filepath.Join(word1Dir, "image.jpg"), []byte("image1"), 0644)
_, err := p.GenerateAnkiFile()
if err != nil {
t.Errorf("GenerateAnkiFile (APKG) failed: %v", err)
}
- // Check APKG file was created
- apkgFile := filepath.Join(flags.OutputDir, "Test_Deck.apkg")
+ // Check APKG file was created in home directory
+ homeDir, _ := os.UserHomeDir()
+ apkgFile := filepath.Join(homeDir, "Test_Deck.apkg")
if _, err := os.Stat(apkgFile); os.IsNotExist(err) {
- t.Error("APKG file was not created")
+ t.Error("APKG file was not created in home directory")
}
+ os.Remove(apkgFile) // Clean up
}