Typsty.Component behaviour (Typsty v0.1.0)

Copy Markdown View Source

Reusable parts of documents, such as tables, letterheads, headers and footers, written as Typst functions that documents import.

A component is a module that does use Typsty.Component. Its Typst source is in a .typ file next to it, with the same base name (tables.ex and tables.typ), or in a render/0 that returns a ~TYPST sigil:

defmodule MyApp.Documents.Tables do
  use Typsty.Component

  def render do
    ~TYPST"""
    #let line-items(items, currency: "$") = table(
      columns: (1fr, auto),
      [*Item*], [*Price*],
      ..items.map(item => (item.name, [#currency#item.price])).flatten(),
    )
    """
  end
end

A document lists the components it uses, and imports each one from components/<name>.typ, where the name is the last part of the module's name, in snake case (tables here, and line_items for MyApp.Documents.LineItems):

defmodule MyApp.Invoice do
  use Typsty, components: [MyApp.Documents.Tables]

  def render(assigns) do
    ~TYPST"""
    #import "components/tables.typ": line-items

    = Invoice #assigns.number
    #line-items(assigns.items)
    """
  end
end

Templates rendered from strings get components with the :components option: Typsty.to_pdf(source, assigns, components: [MyApp.Documents.Tables]). They work the same in a Typsty.Pool.

Headers and footers

A component that lays out pages, with a header and a footer, is a template function: a function that takes the document's content as its last argument and sets up the pages around it. Documents apply it to everything that follows with a show rule:

// letterhead.typ, next to letterhead.ex
#let letterhead(title: none, logo: none, body) = {
  set page(
    header: [#logo #h(1fr) #title],
    footer: context [Page #counter(page).display() of #counter(page).final().first()],
  )
  body
}
#import "components/letterhead.typ": letterhead
#show: letterhead.with(title: [Invoice #assigns.number], logo: image("logo.png", height: 1cm))

What components can have

A component only defines things. When its module compiles, Typsty checks that its top level only has definitions (#let), imports (#import), comments and blank lines, and raises Typsty.SyntaxError otherwise:

  • Content outside a function, such as text, a #table(...) or an #include, never appears in the documents that import the component. Components aren't partials.
  • A set or show rule outside a function only applies to the component's own file. Put it in a function, like the letterhead's set page(...).

Definitions don't have to be functions: #let brand-blue = rgb("#1a4d8f") is one too.

A component only gets data as arguments, which keeps it independent of the documents that use it. Like any file a template imports, it doesn't see the template's assigns. It can't read them from sys.inputs either, where Typst passes them: compiling it fails if it does. It can only use sys to read its other fields (sys.version), so it can't pass sys around or rename it, and it can't use eval, which could read it too. The check reads the source as written, without following scopes, so a parameter named sys is taken for the real one. It's a guard against mistakes, not a sandbox: components are your code.

Files that change from document to document, such as a customer's logo, are passed as arguments too, from the render's :files option. Pass the file itself, such as logo: image("logo.png"), rather than its path: a path is read relative to the file that reads it, and in a component that's components/ (an absolute path, "/logo.png", works too). Files that belong to the component go in its folder (see Files).

Files

A component's own files, such as its images and data, go in a folder next to its module, with the same base name (letterhead/, next to letterhead.ex), or the folder given with the files: option of use Typsty.Component:

lib/my_app/documents/letterhead.ex
lib/my_app/documents/letterhead.typ
lib/my_app/documents/letterhead/
    seal.svg
    offices.json

Renders get them at components/<name>/, next to the component's own components/<name>.typ. Paths in a component are relative to components/, where its .typ file is, so they start with its name:

// letterhead.typ
#let offices = json("letterhead/offices.json")
#let letterhead(title: none, body) = {
  set page(header: [#image("letterhead/seal.svg", height: 1cm) #h(1fr) #title])
  body
}

The paths start with the component's name, not its folder's: with name: "header", the component reads letterhead/seal.svg as header/seal.svg. Documents can read its files too: #image("components/letterhead/seal.svg").

  • The files are read when the module compiles, and kept in it, so they work in releases (which don't have lib/), and in a Typsty.Pool. Changing a file recompiles the component, as does adding one or taking one out.
  • Hidden files and folders, such as .DS_Store, are left out. File names can't have backslashes in them, which Typst can't read.
  • .typ files in the folder are checked like components, and their errors point at them. Paths in them are relative to them, as Typst reads them.
  • The paths a component reads files from and imports, when they're written out as strings (as in image("letterhead/seal.svg")), are checked when it compiles: a path into its folder must be one of its files, and a path to a file right in components/ (image("seal.svg"), which only has components' .typ files) or by its folder's name rather than its own is a mistake. Paths elsewhere, and paths built when it renders (image("letterhead/" + name)), are only checked when it renders.
  • Every render that lists the component gets its files, which takes about a third of a millisecond per MiB in a Typsty.Pool. Compiling a component whose files take more than 8 MiB warns: files that only some documents use are better passed with the render's :files option. (As for any files, renders given more than 32 MiB in all drop Typst's caches when they finish.)
  • Fonts aren't read from a component's folder: Typst finds fonts by family name, from the :font_paths directories, and the system's and Typst's own fonts.

Elixir projects often keep nested modules in a folder named after the module (letterhead/margins.ex). Those aren't the component's files, so compiling it fails if its folder has .ex or .exs files in it: give it another folder with use Typsty.Component, files: "...", or none with files: false.

Modules and files

The .typ file and the folder with the module's base name are only the defaults of the module its file is named after (MyApp.Documents.Letterhead in letterhead.ex). A component nested in another module, or next to one in the same file, has neither: it defines render/0, or gives its source with :template, and its folder with files:.

Components that use other components

A component lists the components it imports, and documents that list it get them too:

defmodule MyApp.Documents.Letterhead do
  use Typsty.Component, components: [MyApp.Documents.Brand]
end

Components are next to each other in components/, so one imports another by its file name: #import "brand.typ": brand-blue.

Errors and compilation

Errors in a component point at the line in its file: its .typ file, or the ~TYPST sigil in its module.

Documents only depend on their components at runtime: changing a component recompiles the component, not the documents that use it. A document's components are checked when it renders, so listing a module that isn't a component raises ArgumentError then, as does listing two components with the same name.

components/ is reserved for components and their files: the :files option can't have files in it, and documents don't read it from the :root directory. That way, a file in :files (one a user uploaded, say) can't take the place of a component a template imports, or of one of its files.

Summary

Callbacks

Returns the component's source, as a Typsty.Template struct. Define it with the ~TYPST sigil, or leave it out and put the source in a .typ file next to the module.

Functions

Makes the module a component. See the module documentation.

The ~TYPST sigil for writing a component's source inline, in render/0.

Callbacks

render()

@callback render() :: Typsty.Template.t()

Returns the component's source, as a Typsty.Template struct. Define it with the ~TYPST sigil, or leave it out and put the source in a .typ file next to the module.

Functions

__using__(opts)

(macro)

Makes the module a component. See the module documentation.

Options

  • :name - the component's name: documents import it from components/<name>.typ, and it reads its files as <name>/.... Defaults to the last part of the module's name, in snake case. Names have letters, digits, underscores and hyphens.
  • :template - the component's .typ file, relative to the module's file. Defaults to the module's file name with the .typ extension, for the module its file is named after (see Modules and files). A module that defines render/0 can't have one.
  • :components - the components it imports, as a list of modules. Documents that list this component get them too.
  • :files - the folder with the component's files (see Files), relative to the module's file, or false for none. Defaults to the folder next to the module with its base name (letterhead/ for letterhead.ex), if there is one, for the module its file is named after.

sigil_TYPST(arg1, modifiers)

(macro)

The ~TYPST sigil for writing a component's source inline, in render/0.

Like the sigil for documents (Typsty.sigil_TYPST/2), it returns a Typsty.Template struct, and its source is checked when it's compiled, with the rules for components (see What components can have). Components have no assigns, so it doesn't need an assigns variable.