genanki

package module
v0.0.0-...-ac87677 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2025 License: MIT Imports: 17 Imported by: 0

README

genanki-go

A Go library for generating Anki decks programmatically.

Features

  • Create Anki decks with notes and cards
  • Support for basic and cloze models
  • Add media files (images, audio, video)
  • Generate .apkg files for Anki import
  • Simple and intuitive API
  • Method chaining for more fluent API usage
  • Optional debug logging for troubleshooting

Installation

go get github.com/npcnixel/genanki-go

Quick Start

Here's a simple example of creating a basic Anki deck:

package main

import (
	"fmt"
	"os"

	"github.com/npcnixel/genanki-go"
)

func main() {
	// Create a basic model and deck
	model := genanki.StandardBasicModel("My Model")
	deck := genanki.StandardDeck("My Deck", "A deck for testing")

	// Add notes to the deck using method chaining
	deck.
		AddNote(
			genanki.NewNote(
				model.ID,
				[]string{"What is the capital of France?", "Paris"},[]string{"geography"},
			),
		).
		AddNote(
			genanki.NewNote(
				model.ID,
				[]string{"What is 2+2?", "4"}, []string{"math"},
			),
		)

	// Create and write package
	pkg := genanki.NewPackage([]*genanki.Deck{deck}).AddModel(model.Model)
	if err := pkg.WriteToFile("output.apkg"); err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("Successfully created Anki deck: output.apkg")
}
Debug Logging

To enable debug logging, you can set the DEBUG environment variable to true:

go run main.go -debug
// OR 
DEBUG=true go run main.go

Or programmatically:

pkg := genanki.NewPackage([]*genanki.Deck{deck}).AddModel(model.Model)
pkg.SetDebug(true)  // Enable debug logging

Debug logging will show detailed information about the database operations, including:

  • Number of notes and cards
  • Note field contents
  • Number of models and decks
  • Database verification steps

Advanced Usage

Creating Different Types of Models
// Create a basic model
basicModel := genanki.StandardBasicModel("Basic Model")

// Create a cloze model
clozeModel := genanki.StandardClozeModel("Cloze Model")

// Create a deck
deck := genanki.StandardDeck("My Deck", "A deck for testing")
Customizing Models
// Create a model
model := genanki.StandardBasicModel("Custom Model")

// Customize the model (each method call separately)
model.Model.SetCSS(`
    .card { 
        font-family: Arial; 
        font-size: 20px;
        text-align: center;
        color: #333;
        background-color: #f5f5f5;
    }
    .question { 
        font-weight: bold; 
        color: navy;
    }
`)

model.Model.AddField(genanki.Field{
    Name: "Extra Info", 
    Font: "Arial",
    Size: 16,
})

basicModel.Model.SetCSS(`...`).AddField(genanki.Field{
    Name:  "Source",
    Ord:   2,
    Font:  "Arial",
    Size:  14,
    Color: "#666666",
})
Adding Media Files
// Create a package
pkg := genanki.NewPackage([]*genanki.Deck{deck})

// Add models to the package
pkg.AddModel(basicModel.Model)
pkg.AddModel(clozeModel.Model)

// Add media files
imageData, _ := os.ReadFile("image.jpg")
pkg.AddMedia("image.jpg", imageData)

audioData, _ := os.ReadFile("audio.mp3")
pkg.AddMedia("audio.mp3", audioData)
Creating Cloze Notes
// Create a cloze note
note := genanki.NewNote(clozeModel.ID, []string{
    "The capital of France is {{c1::Paris}}.",
    "The capital of Spain is {{c1::Madrid}}.",
}, []string{"geography"})

Windows Support

For Windows platform, you can use this branch

  • dev/feature-enable-windows-compatibility

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateIntID

func GenerateIntID() int64

func GenerateMediaHash

func GenerateMediaHash(data []byte) string

func SanitizeFilename

func SanitizeFilename(filename string) string

Types

type BasicModel

type BasicModel struct {
	*Model
}

func NewBasicModel

func NewBasicModel(id int64, name string) *BasicModel

func StandardBasicModel

func StandardBasicModel(name string) *BasicModel

StandardBasicModel creates a new basic model with Anki's standard model ID

type ClozeModel

type ClozeModel struct {
	*Model
}

func NewClozeModel

func NewClozeModel(id int64, name string) *ClozeModel

func StandardClozeModel

func StandardClozeModel(name string) *ClozeModel

StandardClozeModel creates a new cloze model with Anki's standard model ID

type Database

type Database struct {
	// contains filtered or unexported fields
}

func (*Database) AddCard

func (d *Database) AddCard(noteID, deckID int64, templateOrd int) (*Database, error)

func (*Database) AddDeck

func (d *Database) AddDeck(deck *Deck) (*Database, error)

func (*Database) AddModel

func (d *Database) AddModel(model *Model) (*Database, error)

func (*Database) AddNote

func (d *Database) AddNote(note *Note) (*Database, error)

func (*Database) Close

func (d *Database) Close() error

func (*Database) GetFilePath

func (d *Database) GetFilePath() (string, error)

func (*Database) SetDebug

func (d *Database) SetDebug(debug bool) *Database

SetDebug enables or disables debug logging

func (*Database) VerifyContent

func (d *Database) VerifyContent() error

type Deck

type Deck struct {
	ID       int64
	Name     string
	Desc     string
	Notes    []*Note
	Media    map[string][]byte
	Created  time.Time
	Modified time.Time
}

func NewDeck

func NewDeck(id int64, name string, desc string) *Deck

func StandardDeck

func StandardDeck(name string, desc string) *Deck

StandardDeck creates a new deck with Anki's standard deck ID

func (*Deck) AddMedia

func (d *Deck) AddMedia(filename string, data []byte) *Deck

func (*Deck) AddNote

func (d *Deck) AddNote(note *Note) *Deck

type Field

type Field struct {
	Name   string
	Ord    int
	Sticky bool
	RTF    bool
	Font   string
	Size   int
	Color  string
	Align  string
}

type MediaFile

type MediaFile struct {
	Filename string
	Data     []byte
	Hash     string
}

func NewMediaFile

func NewMediaFile(filename string, data []byte) *MediaFile

func NewMediaFileFromPath

func NewMediaFileFromPath(path string) (*MediaFile, error)

func NewMediaFileFromReader

func NewMediaFileFromReader(filename string, reader io.Reader) (*MediaFile, error)

func (*MediaFile) GetMimeType

func (m *MediaFile) GetMimeType() string

func (*MediaFile) IsAudio

func (m *MediaFile) IsAudio() bool

func (*MediaFile) IsImage

func (m *MediaFile) IsImage() bool

func (*MediaFile) IsVideo

func (m *MediaFile) IsVideo() bool

type Model

type Model struct {
	ID        int64
	Name      string
	Fields    []Field
	Templates []Template
	CSS       string
}

func NewModel

func NewModel(id int64, name string) *Model

func (*Model) AddField

func (m *Model) AddField(field Field) *Model

func (*Model) AddTemplate

func (m *Model) AddTemplate(template Template) *Model

func (*Model) SetCSS

func (m *Model) SetCSS(css string) *Model

type Note

type Note struct {
	ID        int64
	ModelID   int64
	Fields    []string
	Tags      []string
	Modified  time.Time
	SortField string
	CheckSum  int64
}

func NewNote

func NewNote(modelID int64, fields []string, tags []string) *Note

type Package

type Package struct {
	// contains filtered or unexported fields
}

func NewPackage

func NewPackage(data interface{}) *Package

NewPackage creates a new package from decks or a database

func (*Package) AddMedia

func (p *Package) AddMedia(filename string, data []byte) *Package

func (*Package) AddMediaFromPath

func (p *Package) AddMediaFromPath(path string) error

func (*Package) AddMediaFromReader

func (p *Package) AddMediaFromReader(filename string, reader io.Reader) error

func (*Package) AddModel

func (p *Package) AddModel(model *Model) *Package

AddModel adds a model to the package

func (*Package) ClearMedia

func (p *Package) ClearMedia()

func (*Package) GetMediaCount

func (p *Package) GetMediaCount() int

func (*Package) GetMediaFile

func (p *Package) GetMediaFile(filename string) *MediaFile

func (*Package) GetMediaFiles

func (p *Package) GetMediaFiles() []*MediaFile

func (*Package) GetMediaSize

func (p *Package) GetMediaSize() int64

func (*Package) RemoveMedia

func (p *Package) RemoveMedia(filename string)

func (*Package) SetDebug

func (p *Package) SetDebug(debug bool) *Package

SetDebug enables or disables debug logging

func (*Package) WriteToFile

func (p *Package) WriteToFile(path string) error

type Template

type Template struct {
	Name  string
	Ord   int
	Qfmt  string
	Afmt  string
	Bqfmt string
	Bafmt string
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL