Typst as lib
Small wrapper around Typst that makes it easier to use it as a templating engine. This API is currently not really stable, although I try to implement the features with as little change to the API as possible.
Usage
rust code
// main.rs
static TEMPLATE_FILE: &str = include_str!;
static FONT: & = include_bytes!;
static OUTPUT: &str = "./examples/output.pdf";
static IMAGE: & = include_bytes!;
Typst code
// template.typ
#import sys: inputs
#set page(paper: "a4")
#set text(font: "TeX Gyre Cursor", 11pt)
#let content = inputs.v
#let last_index = content.len() - 1
#for (i, elem) in content.enumerate() [
  == #elem.heading
  Text: #elem.text \
  Num1: #elem.num1 \
  Num2: #elem.num2 \
  #if elem.image != none [#image.decode(elem.image, height: 40pt)]
  #if i < last_index [
    #pagebreak()
  ]
]
Run example with:
Resolving files in memory
Use TypstTemplate::with_static_file_resolver and add the binaries as key value pairs ((file_name, &[u8])).
For Sources use TypstTemplate::with_static_source_file_resolver and add the sources as key value pairs ((file_name, String)).
Resolving files and packages
Resolving local files can be enabled with TypstTemplate::with_file_system_resolver. Resolving packages can be enabled with TypstTemplate::with_package_file_resolver.
See example which uses the file and the package resolver. The package feature needs to be enabled.
TypstTemplateCollection
If you want to compile multiple typst (main) source files you might want to use the TypstTemplateCollection, which allows you to specify the source file, when calling TypstTemplateCollection::compile, instead of passing it to new. The source file has to be added with TypstTemplateCollection::add_static_file_resolver first.
TypstTemplate is just a wrapper around TypstTemplateCollection, that also saves a FileId for the main source file.
Loading fonts
Loading fonts is not in the scope of this library (yet?). If you are interested in that, write an issue.