apkg

package
v0.0.0-...-61fbb62 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CardTypeNew = iota
	CardTypeLearning
	CardTypeReview
	CardTypeRelearning
	CardTypeDue
)

todo: 猜测

View Source
const (
	CardQueueTypeNew = iota
	CardQueueTypeLearning
	CardQueueTypeReview
	CardQueueTypeRelearning
	CardQueueTypeDue
)

todo: 猜测

View Source
const (
	VirtualDeckID   = 170214000000
	VirtualDeckName = "BK.EasyExport"
	SimpleTplID     = 1000
)
View Source
const ApkgDBName = "collection.anki2"
View Source
const SplitFieldOfNote = "\x1f"

Variables

View Source
var ErrNoteNotFound = errors.New("note not found")

ErrNoteNotFound is returned when a note cannot be found.

View Source
var GlobalTags = []string{}

Functions

func AutoMigrateModels

func AutoMigrateModels(db *gorm.DB) error

AutoMigrateModels migrates the provided models and creates necessary indexes.

func CreateIndexes

func CreateIndexes(db *gorm.DB) error

CreateIndexes creates database indexes to improve query performance.

Types

type Card

type Card struct {
	ID int `gorm:"primaryKey;column:id" json:"id"` // Unique identifier for the card

	// NID - Note ID
	NID int `gorm:"column:nid" json:"nid"`
	// DID - Deck ID
	DID int `gorm:"column:did" json:"did"`

	// Ord - Ordinal, identifies card's template
	Ord int `gorm:"column:ord" json:"ord"`

	Mod int64 `gorm:"column:mod" json:"mod"` // Last modified in milliseconds
	Usn int   `gorm:"column:usn" json:"usn"` // Update sequence number

	// Type of the card: 0: New, 1: Learning, 2: Review, 3: Relearning
	// 表示卡片的类型(如新的、学习中、待复习等)
	Type int `gorm:"column:type" json:"type"`

	// Queue the card is in (new, learning, due, etc.)
	// 表示卡片当前所处的队列(如新的、学习中、待复习等)
	Queue int `gorm:"column:queue" json:"queue"`

	// // Due date for review. For new cards and learning cards this is the order in which they will be shown.
	// 表示卡片的到期日期
	Due int `gorm:"column:due" json:"due"`

	// Interval (used in SRS algorithm). Determines if the review card is young (Ivl < 21) or mature (Ivl >= 21).
	// 用于SRS算法的间隔时间,用于决定卡片是"年轻"还是"成熟"
	Ivl int `gorm:"column:ivl" json:"ivl"`

	Factor int `gorm:"column:factor" json:"factor"` // Ease factor (used in SRS algorithm)
	Reps   int `gorm:"column:reps" json:"reps"`     // Number of reviews
	Lapses int `gorm:"column:lapses" json:"lapses"` // Number of lapses
	Left   int `gorm:"column:left" json:"left"`     // Steps left to graduation (in learning)
	Odue   int `gorm:"column:odue" json:"odue"`     // Original due date (for cards in relearning)
	Odid   int `gorm:"column:odid" json:"odid"`     // Original deck ID (for cards in filtered decks)

	Flags int    `gorm:"column:flags" json:"flags"` // Flags
	Data  string `gorm:"column:data" json:"data"`   // Unused, currently just an empty string.
}

Card represents the 'cards' table which stores the information about the review cards generated from notes. Anki 卡片是通过笔记(Notes)和卡片类型(Card Types)相结合来实现的。每个笔记可以生成一个或多个卡

func (Card) TableName

func (Card) TableName() string

type CardService

type CardService struct {
	DB *gorm.DB
}

CardService provides methods to work with notes and cards.

func (*CardService) CreateCard

func (cs *CardService) CreateCard(cid int, note *Note) (*Card, error)

CreateCard creates a new card based on the given front and back information.

func (*CardService) FindCardByFront

func (cs *CardService) FindCardByFront(front string) ([]Note, []Card, error)

FindCardByFront finds notes and cards with the given front.

func (*CardService) GetAllFronts

func (cs *CardService) GetAllFronts() ([]string, error)

GetAllFronts returns a slice of all fronts from notes.

type Col

type Col struct {
	ID int `gorm:"primaryKey;column:id" json:"id"` // Unique identifier for the collection

	Crt    int    `gorm:"column:crt" json:"crt"`       // Collection created time (timestamp)
	Mod    int64  `gorm:"column:mod" json:"mod"`       // Last modified in milliseconds
	Scm    int64  `gorm:"column:scm" json:"scm"`       // Schema modified time (used for syncing)
	Ver    int    `gorm:"column:ver" json:"ver"`       // Version of the collection
	Dty    int    `gorm:"column:dty" json:"dty"`       // Dirty (need to be synced)
	Usn    int    `gorm:"column:usn" json:"usn"`       // Update sequence number (for finding diffs when syncing)
	Ls     int    `gorm:"column:ls" json:"ls"`         // Last sync time
	Conf   string `gorm:"column:conf" json:"conf"`     // Configuration JSON object
	Models string `gorm:"column:models" json:"models"` // JSON object containing note models
	Decks  string `gorm:"column:decks" json:"decks"`   // JSON object containing deck information
	Dconf  string `gorm:"column:dconf" json:"dconf"`   // JSON object containing deck options
	Tags   string `gorm:"column:tags" json:"tags"`     // A cache of tags used in the collection
}

Col represents the 'col' table which contains information about the collection like settings and statistics.

func (Col) TableName

func (Col) TableName() string

type Deck

type Deck struct {
	Path string
	*Storage
}

Deck Anki 的 apkg 管理实体, 一一对应 deck Ref: - official: https://docs.ankiweb.net/intro.html - open source: -- https://gist.github.com/sartak/3921255 -- https://github.com/SergioFacchini/anki-cards-web-browser/blob/master/documentation/Processing%20Anki's%20.apkg%20files.md

func CreateDeck

func CreateDeck(ctx context.Context, fPath string) (*Deck, error)

CreateDeck initializes and returns a new Deck with a connected database.

func (*Deck) CardService

func (deck *Deck) CardService() *CardService

func (*Deck) Col

func (deck *Deck) Col() *Col

func (*Deck) ExportToAPKG

func (deck *Deck) ExportToAPKG(exportPath string) error

ExportToAPKG exports the database and associated files as an .apkg file.

func (*Deck) NoteService

func (deck *Deck) NoteService() *NoteService

type Field

type Field struct {
	Ord    int      `json:"ord"`
	Sticky bool     `json:"sticky"`
	Rtl    bool     `json:"rtl"`
	Media  []string `json:"media"`
	Size   int      `json:"size"`
	Font   string   `json:"font"`
	Name   string   `json:"name"`
}

type Grave

type Grave struct {
	ID   int `gorm:"primaryKey;column:id" json:"id"` // Unused, currently just set to zero
	Usn  int `gorm:"column:usn" json:"usn"`          // Update sequence number
	Oid  int `gorm:"column:oid" json:"oid"`          // Original ID (card, note, or deck id)
	Type int `gorm:"column:type" json:"type"`        // Type of object (card, note, or deck)
}

Grave represents the 'graves' table which logs deleted cards, notes, and decks.

func (Grave) TableName

func (Grave) TableName() string

type Note

type Note struct {
	ID int `gorm:"primaryKey;column:id" json:"id"` // Unique identifier for the note

	Guid string `gorm:"column:guid" json:"guid"` // Globally unique id, used for syncing

	// Mid - Model ID,
	// links to card templates which define how to use the fields to generate "front" and "back" sides of cards.
	Mid int `gorm:"column:mid" json:"mid"`
	// Mod - Last modified in milliseconds
	Mod int64 `gorm:"column:mod" json:"mod"`

	// Usn - Update sequence number
	Usn  int    `gorm:"column:usn" json:"usn"`
	Tags string `gorm:"column:tags" json:"tags"` // Space-separated string of tags.

	// FLDs - Fields of the note joined by 0x1f character.
	// They are used by card templates to generate "front" and "back" sides of cards.
	//
	// - "Basic" notes have "Front" and "Back" fields for basic question-answer cards.
	// - "Basic (and reversed card)" notes add a reversed card from the answer to the question alongside the basic card.
	// - "Basic (optional reversed card)" has "Front", "Back", and "Add Reverse" fields, creating reversed cards when "Add Reverse" is filled.
	// - "Cloze" notes are used to create fill-in-the-blank cards where text is omitted.
	// - "Image Occlusion" notes utilize images with sections blocked out to test recognition of image parts.
	FLDs string `gorm:"column:flds" json:"flds"`

	// SFLD - Sort field: the value of the field by which notes are sorted in the browser.
	SFLD int `gorm:"column:sfld" json:"sfld"`

	CSum  int64  `gorm:"column:csum" json:"csum"`   // Checksum used for duplicate check.
	Flags int    `gorm:"column:flags" json:"flags"` // Flags
	Data  string `gorm:"column:data" json:"data"`   // Unused, currently just an empty string.
}

Note represents the 'notes' table which stores all the information of notes including metadata and the content itself.

func (Note) Front

func (n Note) Front() string

func (Note) TableName

func (Note) TableName() string

type NoteContentFormatter

type NoteContentFormatter string
const (
	NoteCFmtPlainText NoteContentFormatter = ""
	NoteCFmtMarkdown                       = "markdown"
)

type NoteOption

type NoteOption func(*NoteOptions)

NoteOption configures NoteOptions.

func NoteWithContentFormatter

func NoteWithContentFormatter(contentFmt NoteContentFormatter) NoteOption

func NoteWithGUID

func NoteWithGUID(guid string) NoteOption

func NoteWithNID

func NoteWithNID(nid int) NoteOption

func NoteWithTags

func NoteWithTags(tags ...string) NoteOption

type NoteOptions

type NoteOptions struct {
	NID              int                  `json:"nid,omitempty"`
	GUID             string               `json:"guid,omitempty"`
	Tags             []string             `json:"tags,omitempty"`
	ContentFormatter NoteContentFormatter `json:"note_content_formatter,omitempty"`
}

NoteOptions includes options for creating a new note.

func (*NoteOptions) Use

func (no *NoteOptions) Use(opts ...NoteOption) *NoteOptions

type NoteService

type NoteService struct {
	DB *gorm.DB
}

NoteService provides methods to work with notes and cards.

func (*NoteService) CreateNote

func (cs *NoteService) CreateNote(ctx context.Context, front, back string, opts ...NoteOption) (*Note, error)

type RevLog

type RevLog struct {
	ID int64 `gorm:"primaryKey;column:id" json:"id"` // Timestamp (based on 13 digits), used as ID

	Cid int `gorm:"column:cid" json:"cid"` // Card ID

	// Usn - Update sequence number
	Usn     int `gorm:"column:usn" json:"usn"`
	Ease    int `gorm:"column:ease" json:"ease"`       // Ease button pressed (again, hard, good, easy)
	Ivl     int `gorm:"column:ivl" json:"ivl"`         // New interval
	LastIvl int `gorm:"column:lastIvl" json:"lastIvl"` // Last interval
	Factor  int `gorm:"column:factor" json:"factor"`   // New ease factor
	Time    int `gorm:"column:time" json:"time"`       // Review time in milliseconds
	Type    int `gorm:"column:type" json:"type"`       // Review type
}

RevLog represents the 'revlog' table which stores the review logs of cards.

func (RevLog) TableName

func (RevLog) TableName() string

type Storage

type Storage struct {
	DB *gorm.DB
}

func CreateInMemStorage

func CreateInMemStorage(ctx context.Context, initial func(db *gorm.DB) error) (*Storage, error)

func CreateStorage

func CreateStorage(ctx context.Context, fPath, dbName string, initial func(db *gorm.DB) error) (*Storage, error)

CreateStorage initializes and returns a new Storage with a connected database.

func (*Storage) Close

func (p *Storage) Close() error

type Template

type Template struct {
	Ord   int    `json:"ord"`
	Bsize int    `json:"bsize"`
	Did   *int   `json:"did"`
	Bafmt string `json:"bafmt"`
	Qfmt  string `json:"qfmt"`
	Afmt  string `json:"afmt"`
	Bfont string `json:"bfont"`
	Bqfmt string `json:"bqfmt"`
	Name  string `json:"name"`
}

type TplModel

type TplModel struct {
	ID        int           `json:"id"`
	Tmpls     []Template    `json:"tmpls"`
	LatexPre  string        `json:"latexPre"`
	Req       []any         `json:"req"`
	Flds      []Field       `json:"flds"`
	Tags      []string      `json:"tags"`
	Type      int           `json:"type"`
	Mod       int64         `json:"mod"`
	LatexSVG  int           `json:"latexsvg"`
	Sortf     int           `json:"sortf"`
	Usn       int           `json:"usn"`
	Did       int           `json:"did"`
	Vers      []interface{} `json:"vers"`
	LatexPost string        `json:"latexPost"`
	Name      string        `json:"name"`
	Css       string        `json:"css"`
	Gf        bool          `json:"gf"`
}

Jump to

Keyboard shortcuts

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