From 310ad64d1ad0b6e30a7dfaab0344dd78cabae463 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 2 Aug 2025 15:08:33 +0300 Subject: also export reverse cards via apkg --- internal/processor/processor.go | 47 +++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'internal/processor/processor.go') 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", "
") + } + } + gen.AddCard(card) } } -- cgit v1.2.3