typst-as-lib
Small wrapper for typst that makes it easier to use it as a templating engine. Maybe useful for someone...
Usage
TypstTemplate
rust code
// main.rs
use ;
use fs;
use ;
use Font;
use TypstTemplate;
static TEMPLATE_FILE: &str = include_str!;
static FONT: & = include_bytes!;
// static OTHER_SOURCE: &str = include_str!("./templates/other_source.typ");
// static IMAGE: &[u8] = include_bytes!("./images/image.png");
// Implement Into<Dict> manually, so we can just pass the struct
// to the compile function.
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 i < last_index [
#pagebreak()
]
]
Run example with cargo r --example small_example.
TypstTemplateCollection
If you want to compile multiple typst 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_sources first.