Folio.DSL (Folio v0.3.0)

Copy Markdown View Source

Builder functions for Folio content nodes.

Every function returns a %Folio.Content.*{} struct. Use inside #{} interpolation in ~MD sigils, or directly in Elixir code.

use Folio
content = [heading(1, "Title"), text("Hello"), strong("world")]
{:ok, pdf} = Folio.to_pdf(content)

Summary

Functions

Align content. Accepts :left, :center, :right or string equivalents.

Insert a bibliography listing.

Block-level container. Options: :width, :height, :above, :below.

Block quote. Options: :block, :attribution.

Circle. Options: :radius, :fill, :body.

Cite a bibliography entry.

Force a column break. Options: :weak.

Flow content into count columns. Options: :gutter.

Horizontal divider.

Ellipse. Options: :width, :height, :fill, :body.

Italic text.

Numbered list. Items can be plain content or {number, content} tuples.

Wrap content in a figure. Options: :caption, :placement ("top"/"bottom"), :scope, :numbering.

Footnote.

Grid layout. Options: :columns, :rows, :gutter. Use do block for cells.

Create a grid cell. Options: :colspan, :rowspan, :align, :fill.

Create a heading (levels 1-6).

Hide content (invisible but takes space).

Highlighted text. Options: :fill color.

Horizontal spacing. Options: :weak.

Embed an image. Options: :width, :height, :fit ("contain"/"cover"/"stretch").

Label a position for cross-references.

Line. Options: :start, :end, :length, :angle, :stroke.

Insert a line break.

Hyperlink. Second argument is optional display text.

Bullet list. Options: :tight, :marker.

Local style overrides for a content block. Mirrors Typst's #set text(...) / #set par(...) within a scope.

Math expression in Typst syntax. Options: :block.

Table of contents. Options: :title, :indent, :depth.

Add padding around content. Options: :left, :right, :top, :bottom.

Force a page break. Options: :weak.

Insert a paragraph break.

Place content at an alignment. Options: :alignment, :float.

Polygon from coordinate list. Options: :fill, :stroke.

Raw/code text. Options: :lang, :block.

Raw Typst source injected directly.

Rectangle. Options: :width, :height, :fill, :body.

Reference a labelled element.

Repeat content to fill available space.

Create a show rule that transforms matching content elements.

Small capitals text.

Square. Options: :size, :fill, :body.

Stack children in a direction. Options: :dir ("ttb"/"ltr"/"rtl"/"btt"), :spacing.

Strikethrough text.

Bold text.

Subscript text.

Superscript text.

Create a table. Options: :columns, :rows, :stroke, :gutter, :align. Use do block for rows.

Create a table cell. Options: :colspan, :rowspan, :align.

Create a table header row from a list of cell contents.

Create a table data row from a list of cell contents.

Single term item.

Definition list from {term, description} tuples. Options: :tight.

Create a text node. Options: :size, :weight, :fill, :tracking.

Document title.

Underlined text.

Vertical spacing. Options: :weak.

Functions

align(alignment, content)

Align content. Accepts :left, :center, :right or string equivalents.

Examples

use Folio
align(:center, heading(1, "Centered Title"))
align(:right, text("Right-aligned"))

bibliography(sources, opts \\ [])

@spec bibliography(
  String.t() | [String.t()],
  keyword()
) :: Folio.Content.Bibliography.t()

Insert a bibliography listing.

bibliography("refs.bib", title: "References", style: "ieee")

block(opts \\ [], list)

@spec block(
  keyword(),
  [{:do, [Folio.Content.t()]}]
) :: Folio.Content.Block.t()

Block-level container. Options: :width, :height, :above, :below.

Examples

use Folio
block(above: "1em", below: "1em",
  do: [text("Spaced block content.")])

blockquote(content, opts \\ [])

@spec blockquote(
  Folio.Content.t() | [Folio.Content.t()] | String.t(),
  keyword()
) :: Folio.Content.Quote.t()

Block quote. Options: :block, :attribution.

Examples

use Folio
blockquote("To be, or not to be.")
blockquote("Elementary, my dear Watson.", attribution: "Sherlock Holmes")

circle(opts \\ [])

@spec circle(keyword()) :: Folio.Content.Circle.t()

Circle. Options: :radius, :fill, :body.

Examples

use Folio
circle(radius: "20pt", fill: "#ff0000")

cite(key, opts \\ [])

@spec cite(
  String.t(),
  keyword()
) :: Folio.Content.Cite.t()

Cite a bibliography entry.

cite("knuth1984", form: "prose")

colbreak(opts \\ [])

@spec colbreak(keyword()) :: Folio.Content.Colbreak.t()

Force a column break. Options: :weak.

Examples

use Folio
colbreak()
colbreak(weak: true)

columns(count, opts \\ [], list)

@spec columns(pos_integer(), keyword(), [{:do, [Folio.Content.t()]}]) ::
  Folio.Content.Columns.t()

Flow content into count columns. Options: :gutter.

Examples

use Folio
columns(2, gutter: "12pt",
  do: [
    text("Left column content."),
    colbreak(),
    text("Right column content.")
  ])

divider()

@spec divider() :: Folio.Content.Divider.t()

Horizontal divider.

Examples

use Folio
divider()

ellipse(opts \\ [])

@spec ellipse(keyword()) :: Folio.Content.Ellipse.t()

Ellipse. Options: :width, :height, :fill, :body.

Examples

use Folio
ellipse(width: "80pt", height: "40pt", fill: "#aaffaa")

emph(content)

Italic text.

Examples

use Folio
emph("emphasis")

enum(items, opts \\ [])

@spec enum(
  [term()],
  keyword()
) :: Folio.Content.EnumList.t()

Numbered list. Items can be plain content or {number, content} tuples.

enum(["First", "Second"])                    # auto-numbered
enum([{1, "First"}, {5, "Fifth"}])           # explicit numbers
enum(["A", "B"], start: 3)                   # start from 3

figure(content, opts \\ [])

Wrap content in a figure. Options: :caption, :placement ("top"/"bottom"), :scope, :numbering.

footnote(content)

Footnote.

Examples

use Folio
[text("See here"), footnote("Additional details in the appendix.")]

grid(opts, list)

@spec grid(
  keyword(),
  [{:do, [Folio.Content.t()]}]
) :: Folio.Content.Grid.t()

Grid layout. Options: :columns, :rows, :gutter. Use do block for cells.

grid(columns: ["1fr", "1fr"], gutter: "6pt",
  do: [grid_cell("A"), grid_cell("B")])

grid_cell(content, opts \\ [])

Create a grid cell. Options: :colspan, :rowspan, :align, :fill.

Examples

use Folio
grid_cell("plain")
grid_cell(strong("header"), colspan: 2, fill: "#eeeeee")

heading(level, content)

@spec heading(1..6, Folio.Content.t() | [Folio.Content.t()] | String.t()) ::
  Folio.Content.Heading.t()

Create a heading (levels 1-6).

Examples

use Folio
heading(1, "Introduction")
heading(2, [strong("Bold"), text(" subtitle")])

hide(content)

Hide content (invisible but takes space).

Examples

use Folio
hide(text("placeholder"))

highlight(content, opts \\ [])

Highlighted text. Options: :fill color.

Examples

use Folio
highlight("important passage")
highlight("custom color", fill: "#ffff00")

hspace(amount, opts \\ [])

@spec hspace(
  String.t() | number(),
  keyword()
) :: Folio.Content.HSpace.t()

Horizontal spacing. Options: :weak.

Examples

use Folio
hspace("1em")
hspace("0.5cm", weak: true)

image(src, opts \\ [])

@spec image(
  String.t(),
  keyword()
) :: Folio.Content.Image.t()

Embed an image. Options: :width, :height, :fit ("contain"/"cover"/"stretch").

label(name)

@spec label(String.t()) :: Folio.Content.Label.t()

Label a position for cross-references.

Examples

use Folio
[heading(1, "Introduction"), label("intro")]

line(opts \\ [])

@spec line(keyword()) :: Folio.Content.Line.t()

Line. Options: :start, :end, :length, :angle, :stroke.

Examples

use Folio
line(length: "100%", stroke: "1pt")
line(start: "(0pt, 0pt)", end: "(50pt, 50pt)")

linebreak()

@spec linebreak() :: Folio.Content.Linebreak.t()

Insert a line break.

Examples

use Folio
[text("Line one."), linebreak(), text("Line two.")]

link(url, text \\ nil)

@spec link(String.t(), nil | Folio.Content.t() | String.t()) :: Folio.Content.Link.t()

Hyperlink. Second argument is optional display text.

Examples

use Folio
link("https://elixir-lang.org")
link("https://elixir-lang.org", "Elixir")
link("https://elixir-lang.org", strong("Elixir"))

list(items, opts \\ [])

@spec list(
  [term()],
  keyword()
) :: Folio.Content.List.t()

Bullet list. Options: :tight, :marker.

Examples

use Folio
list(["First item", "Second item", "Third item"])
list(["A", "B"], tight: false, marker: "–")

local_set(opts, list)

@spec local_set(
  keyword(),
  [{:do, [Folio.Content.t()]}]
) :: Folio.Content.LocalSet.t()

Local style overrides for a content block. Mirrors Typst's #set text(...) / #set par(...) within a scope.

local_set([hyphenate: false, justify: false],
  do: [text("No hyphenation here")])

math(content, opts \\ [])

@spec math(
  String.t(),
  keyword()
) :: Folio.Content.Math.t()

Math expression in Typst syntax. Options: :block.

Examples

use Folio
math("x^2 + y^2 = r^2")
math("sum_(i=1)^n i = (n(n+1))/2", block: true)

outline(opts \\ [])

@spec outline(keyword()) :: Folio.Content.Outline.t()

Table of contents. Options: :title, :indent, :depth.

Examples

use Folio
outline()
outline(title: "Contents", depth: 2)

pad(opts, list)

@spec pad(
  keyword(),
  [{:do, [Folio.Content.t()]}]
) :: Folio.Content.Pad.t()

Add padding around content. Options: :left, :right, :top, :bottom.

Examples

use Folio
pad(left: "1cm", right: "1cm",
  do: [text("Indented paragraph.")])

pagebreak(opts \\ [])

@spec pagebreak(keyword()) :: Folio.Content.Pagebreak.t()

Force a page break. Options: :weak.

Examples

use Folio
pagebreak()
pagebreak(weak: true)

parbreak()

@spec parbreak() :: Folio.Content.Parbreak.t()

Insert a paragraph break.

Examples

use Folio
[text("First paragraph."), parbreak(), text("Second paragraph.")]

place(content, opts \\ [])

Place content at an alignment. Options: :alignment, :float.

Examples

use Folio
place(image("logo.png"), alignment: "top + right", float: true)

polygon(vertices, opts \\ [])

@spec polygon(
  [{number(), number()}],
  keyword()
) :: Folio.Content.Polygon.t()

Polygon from coordinate list. Options: :fill, :stroke.

Examples

use Folio
polygon([{0, 0}, {50, 0}, {25, 40}], fill: "#aaaaff", stroke: "1pt")

raw(text, opts \\ [])

@spec raw(
  String.t(),
  keyword()
) :: Folio.Content.Raw.t()

Raw/code text. Options: :lang, :block.

Examples

use Folio
raw("IO.puts("hello")", lang: "elixir")
raw("SELECT * FROM users", lang: "sql", block: true)

raw_typst(source)

@spec raw_typst(String.t()) :: Folio.Content.RawTypst.t()

Raw Typst source injected directly.

raw_typst("#set text(hyphenate: false)\nHello")

rect(opts \\ [])

@spec rect(keyword()) :: Folio.Content.Rect.t()

Rectangle. Options: :width, :height, :fill, :body.

Examples

use Folio
rect(width: "100pt", height: "50pt", fill: "#cccccc")
rect(width: "4cm", body: text("inside"))

ref(target, supplement \\ nil)

Reference a labelled element.

Examples

use Folio
ref("intro")
ref("fig:chart", "Figure")

repeat(content)

Repeat content to fill available space.

Examples

use Folio
repeat(text("."))

show(target, transform)

Create a show rule that transforms matching content elements.

The target is an atom matching the content type. Common targets: :enum, :enum_item, :list, :list_item, :heading, :paragraph, :quote, :table, :grid, :block, etc.

The transform receives the matched struct and returns replacement content (a struct, a list of structs, or a string).

Example — custom enum formatting

show(:enum, fn %Folio.Content.EnumList{children: items} ->
  items
  |> Enum.with_index(1)
  |> Enum.map(fn {item, num} ->
    block([above: "0.65em", below: "0.65em"],
      do: [
        local_set([first_line_indent: 0],
          do: [
            hspace("1.25cm"),
            text("#{num}."),
            hspace("0.3em"),
            item.body
          ]
        )
      ]
    )
  end)
  |> List.flatten()
end)

smallcaps(content)

Small capitals text.

Examples

use Folio
smallcaps("Section Title")

square(opts \\ [])

@spec square(keyword()) :: Folio.Content.Square.t()

Square. Options: :size, :fill, :body.

Examples

use Folio
square(size: "40pt", fill: "#0055bb")

stack(opts, list)

@spec stack(
  keyword(),
  [{:do, [Folio.Content.t()]}]
) :: Folio.Content.Stack.t()

Stack children in a direction. Options: :dir ("ttb"/"ltr"/"rtl"/"btt"), :spacing.

strike(content)

Strikethrough text.

Examples

use Folio
strike("deleted")

strong(content)

Bold text.

Examples

use Folio
strong("important")
strong([text("also "), emph("works")])

subscript(content)

@spec subscript(Folio.Content.t() | [Folio.Content.t()] | String.t()) ::
  Folio.Content.Sub.t()

Subscript text.

Examples

use Folio
[text("H"), subscript("2"), text("O")]

superscript(content)

@spec superscript(Folio.Content.t() | [Folio.Content.t()] | String.t()) ::
  Folio.Content.Super.t()

Superscript text.

Examples

use Folio
[text("x"), superscript("2")]

table(opts, list)

@spec table(
  keyword(),
  [{:do, [Folio.Content.t()]}]
) :: Folio.Content.Table.t()

Create a table. Options: :columns, :rows, :stroke, :gutter, :align. Use do block for rows.

Examples

use Folio
table(columns: ["1fr", "1fr"],
  do: [
    table_header(["Name", "Score"]),
    table_row(["Alice", "95"]),
    table_row(["Bob", "87"])
  ])

table_cell(content, opts \\ [])

Create a table cell. Options: :colspan, :rowspan, :align.

Examples

use Folio
table_cell("plain cell")
table_cell(strong("header"), colspan: 2, align: "center")

table_header(cells)

@spec table_header([Folio.Content.t() | String.t()]) :: Folio.Content.TableHeader.t()

Create a table header row from a list of cell contents.

Examples

use Folio
table_header(["Name", "Age", "City"])

table_row(cells)

@spec table_row([Folio.Content.t() | String.t()]) :: Folio.Content.TableRow.t()

Create a table data row from a list of cell contents.

Examples

use Folio
table_row(["Alice", "30", "London"])

term_item(term, description)

@spec term_item(term(), term()) :: Folio.Content.TermItem.t()

Single term item.

Examples

use Folio
term_item("Folio", "A PDF library for Elixir.")

term_list(items, opts \\ [])

@spec term_list(
  [{term(), term()}],
  keyword()
) :: Folio.Content.TermList.t()

Definition list from {term, description} tuples. Options: :tight.

Examples

use Folio
term_list([
  {"Elixir", "A functional language."},
  {"Typst", "A modern typesetting system."}
])

text(str, opts \\ [])

@spec text(
  String.t(),
  keyword()
) :: Folio.Content.Text.t()

Create a text node. Options: :size, :weight, :fill, :tracking.

Examples

use Folio
text("Hello, world!")
text("Big", size: "24pt", weight: "bold", fill: "#ff0000")

title(content)

Document title.

Examples

use Folio
title("My Report")
title([strong("Folio"), text(" — PDF Library")])

underline(content)

Underlined text.

Examples

use Folio
underline("underlined")

vspace(amount, opts \\ [])

@spec vspace(
  String.t() | number(),
  keyword()
) :: Folio.Content.VSpace.t()

Vertical spacing. Options: :weak.

Examples

use Folio
vspace("1em")
vspace("2pt", weak: true)