AshTypst.Context (ash_typst v0.3.0)

Copy Markdown View Source

Persistent Typst rendering context.

A context wraps a Rust-side SystemWorld that keeps fonts, virtual files, and the compiled document in memory. The typical lifecycle is:

  1. new/1 — create a context (scans fonts, sets root path)
  2. set_markup/2 — load a Typst template
  3. Optionally inject data via set_virtual_file/3, stream_virtual_file/4, or set_inputs/2
  4. compile/1 — compile the markup into a paged document
  5. render_svg/2 or export_pdf/2 — render output from the compiled document (export_html/2 and export_bundle/2 compile on their own)

Steps 2-5 can be repeated without re-creating the context. Fonts and virtual files persist until explicitly changed.

Thread safety

Each function acquires internal locks, so a single context can be shared across processes. However, concurrent compile and set_markup calls on the same context will serialize — design your pipeline accordingly.

Summary

Functions

Append a chunk to a virtual file (creates it if new).

Remove a virtual file. Invalidates the compiled document.

Compile the current markup.

Export the document as a multi-file bundle.

Export the document as HTML.

Export the compiled document as a PDF binary.

List font families available in this context.

Create a new context.

Render a page of the compiled document as SVG.

Set a single sys.inputs key/value pair.

Replace all sys.inputs with the given map of string keys/values.

Set the main Typst markup. Invalidates any compiled document.

Set (or overwrite) a virtual file with text content. Invalidates the compiled document.

Set (or overwrite) a virtual file with raw binary content. Invalidates the compiled document.

Stream an Elixir enumerable into a virtual file as a Typst array.

Functions

append_virtual_file(ctx, path, chunk)

Append a chunk to a virtual file (creates it if new).

Does not invalidate the compiled document — call compile/1 after streaming is complete.

clear_virtual_file(ctx, path)

Remove a virtual file. Invalidates the compiled document.

compile(ctx)

Compile the current markup.

Returns {:ok, %CompileResult{}} with the page count and warnings, or {:error, %CompileError{}} with diagnostics.

export_bundle(ctx, opts \\ [])

Export the document as a multi-file bundle.

The bundle target lets a single Typst project emit multiple documents and assets. The template declares its outputs with top-level document and asset elements. Each takes the destination path as its first positional argument, for example:

#document("index.html", title: [Home])[
  = Home
  See the #link("about.html")[about page].
]
#document("about.html", title: [About])[= About]
#asset("logo.svg", read("logo.svg", encoding: none))

Performs its own compilation (separate from compile/1). Returns {:ok, %AshTypst.BundleResult{}} whose files is a map of relative path (no leading slash) to the rendered binary content, e.g. %{"index.html" => "...", "about.html" => "..."}, and whose warnings is a list of AshTypst.Diagnostic warnings.

Documents within the bundle are exported according to their declared format (HTML, PDF, SVG, or PNG); assets are emitted verbatim.

Options

  • :pretty — format HTML/SVG documents in a human-readable way (default false, i.e. minified)
  • :render_bleed — include bleed margins for SVG documents (default false)

export_html(ctx, opts \\ [])

Export the document as HTML.

Performs its own compilation (separate from compile/1).

Options

  • :pretty — format the HTML in a human-readable way (default false, i.e. minified)

export_pdf(ctx, opts \\ [])

Export the compiled document as a PDF binary.

Options

  • :pages — page range string like "1-3,5,7-9" (1-indexed)
  • :pdf_standards — list of standards, e.g. [:pdf_a_2b]
  • :document_id — stable identifier for caching

font_families(ctx)

List font families available in this context.

new(opts \\ [])

Create a new context.

Fonts are scanned once during creation and reused across all operations.

Options

  • :root — root path for template resolution (default ".")
  • :font_paths — additional font directories to search
  • :ignore_system_fonts — skip system fonts (default false)

render_svg(ctx, opts \\ [])

Render a page of the compiled document as SVG.

Options

  • :page — zero-indexed page number (default 0)
  • :pretty — format the SVG in a human-readable way (default false, i.e. minified)
  • :render_bleed — expand the rendered area beyond the page bounds to include bleed margins, useful when preparing documents for print (default false)

set_input(ctx, key, value)

Set a single sys.inputs key/value pair.

set_inputs(ctx, inputs)

Replace all sys.inputs with the given map of string keys/values.

set_markup(ctx, markup)

Set the main Typst markup. Invalidates any compiled document.

set_virtual_file(ctx, path, content)

Set (or overwrite) a virtual file with text content. Invalidates the compiled document.

set_virtual_file_binary(ctx, path, content)

Set (or overwrite) a virtual file with raw binary content. Invalidates the compiled document.

Use this for non-text files like images (PNG, SVG) that Typst reads via #image(read("name", encoding: none)).

stream_virtual_file(ctx, path, stream, opts \\ [])

Stream an Elixir enumerable into a virtual file as a Typst array.

Each element is encoded via AshTypst.Code.encode/2 and batched to Rust for memory efficiency.

Options

  • :variable_name — the #let binding name (default "data")
  • :context — encoding context passed to AshTypst.Code.encode/2
  • :batch_size — records per NIF call (default 100)