### Add Media Files to Package Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Demonstrates creating a genanki-rs Package with specified media files and writing it to an .apkg file. This involves passing a vector of file paths to the Package constructor. ```rust use genanki_rs::{Deck, Error, Package}; fn main() -> Result<(), Error> { // ... // my_deck.add(my_note) let mut my_package = Package::new(vec![my_deck], vec!["sound.mp3", "images/image.jpg"])?; my_package.write_to_file("output.apkg")?; Ok(()) } ``` -------------------------------- ### Reference Media in Note Fields and Templates Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Illustrates how to define a Model with a dedicated media field and then use this field in a Note to embed media. For audio, use `[sound:filename]`; for images, use ``. The filename should be the basename, not the full path. ```rust let my_model = Model::new( 1607392319, "Simple Model", vec![ Field::new("Question"), Field::new("Answer"), Field::new("MyMedia"), // ADD THIS ], vec![Template::new("Card 1") .qfmt("{{Question}}{{Question}}
{{MyMedia}}") // AND THIS .afmt(r#"{{FrontSide}}
{{Answer}}"#)], ); let my_note = Note::new(my_model, vec!["Capital of Argentina", "Buenos Aires", "[sound:sound.mp3]"])?; // or let my_note = Note::new(my_model, vec!["Capital of Argentina", "Buenos Aires", r#""#])?; ``` -------------------------------- ### Create and Populate a genanki-rs Deck Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Explains how to create an Anki Deck with a unique ID, name, and description, and then add previously created Notes to it. ```rust use genanki_rs::{Deck, Error}; fn main() -> Result<(), Error> { // let my_note = ... // Assume my_note is defined elsewhere let mut my_deck = Deck::new( 2059400110, // Unique deck ID "Country Capitals", "Deck for studying country capitals", ); my_deck.add_note(my_note); Ok(()) } ``` -------------------------------- ### Write genanki-rs Deck to APKG File Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Demonstrates how to generate an Anki package file (`.apkg`) from a populated Deck object. This file can then be imported directly into the Anki application. ```rust use genanki_rs::Error; fn main() -> Result<(), Error> { // let my_deck = ... // Assume my_deck is populated my_deck.write_to_file("output.apkg")?; Ok(()) } ``` -------------------------------- ### Create a genanki-rs Note Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Demonstrates the creation of a basic Anki Note using the `Note::new` function. This requires a pre-defined Anki Model and a vector of fields, which are typically HTML-encoded strings. ```rust use genanki_rs::{Note, Error}; fn main() -> Result<(), Error> { // let my_model = ... // Assume my_model is defined elsewhere let my_note = Note::new(my_model, vec!["Capital of Argentina", "Buenos Aires"])?; Ok(()) } ``` -------------------------------- ### Add genanki-rs Dependency Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Shows how to add the genanki-rs crate to your Rust project's Cargo.toml file to include it as a dependency. ```toml [dependencies] genanki-rs = "0.4" ``` -------------------------------- ### Define a genanki-rs Model with Custom CSS Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Shows how to apply custom CSS styling to an Anki Model. The `Model::css()` method allows embedding CSS rules to control the appearance of cards. ```rust use genanki_rs::{Field, Model, Template}; let custom_css = ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n}\n"; let my_model_with_css = Model::new( 1607392319, "Simple Model", vec![Field::new("Question"), Field::new("Answer")], vec![Template::new("Card 1") .qfmt("{{Question}}") .afmt(r"{{FrontSide}}
{{Answer}}")]) .css(custom_css); ``` -------------------------------- ### Define a genanki-rs Model Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Illustrates how to define a custom Anki Model, specifying its unique ID, name, fields, and card templates. This structure dictates how notes of this type are presented in Anki. ```rust use genanki_rs::{Field, Model, Template, Error}; fn main() -> Result<(), Error> { let my_model = Model::new( 1607392319, // Unique model ID "Simple Model", vec![Field::new("Question"), Field::new("Answer")], vec![Template::new("Card 1") .qfmt("{{Question}}") .afmt(r"{{FrontSide}}
{{Answer}}")], ); // let my_note = ... Ok(()) } ``` -------------------------------- ### Configure Sort Field Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Explains how to manage the `sort_field` for Anki notes, which is used for sorting in the Browse interface. The `sort_field` can be set using `Note::sort_field()` or by specifying the index of the field using `Model::sort_field_index()`. ```rust // By default, the sort_field is the first field. // You can change it by calling Note::sort_field(). // let mut my_note = Note::new(...); // my_note.sort_field("CustomSortField"); // You can also call Model::sort_field_index(), passing the sort_field_index // to change the sort field. 0 means the first field in the Note, 1 means the second, etc. // let mut my_model = Model::new(...); // my_model.sort_field_index(1); // Sets the second field as the sort field ``` -------------------------------- ### HTML Encode Special Characters Source: https://github.com/yannickfunk/genanki-rs/blob/master/README.md Provides guidance on handling special characters like `<`, `>`, and `&` within note fields. These characters must be HTML-encoded to prevent data corruption, as field data is treated as HTML. This applies to plain text and LaTeX content. ```rust let fields = vec!["AT&T was originally called", "Bell Telephone Company"]; // This applies even if the content is LaTeX; let fields_with_latex = vec!["Piketty calls this the \"central contradiction of capitalism\".", "[latex]r > g[/latex]"]; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.