typst-as-lib
Small wrapper around 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!;
// 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:
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.
Resolving files and packages
I don't want to put that logic into the library itself, as it is not useful for my use cases, but TypstTemplate::file_resolver can be used for this purpose. See example examples/resolve_files.rs. I used the code from typst-as-library. If there is interest for this, just write an issue. I could imagine providing this behind a feature flag (Maybe one for files and packages each).
Loading fonts
Loading fonts is not in the scope of this library (yet?). If you are interested in that, write an issue.
-
This is how the typst-cli loads system fonts:
-
Here is an example of loading fonts from a folder: