gssg/ssg

Types

The main SSG (Static Site Generator) type that holds the configuration and routes for your static site.

Type Parameters

  • page_type: The type of your page content before rendering (e.g., Element(msg) from Lustre)

Fields

  • out_dir: The output directory where generated files will be written
  • routes: A list of path and page tuples representing all routes in your site
  • render: A function that converts your page type to an HTML string
pub type SSG(page_type) {
  SSG(
    out_dir: String,
    routes: List(#(String, page_type)),
    render: fn(page_type) -> String,
  )
}

Constructors

  • SSG(
      out_dir: String,
      routes: List(#(String, page_type)),
      render: fn(page_type) -> String,
    )

Represents errors that can occur during the build process.

pub type SSGBuildError {
  CannotWriteFile(path: String, reason: simplifile.FileError)
}

Constructors

  • CannotWriteFile(path: String, reason: simplifile.FileError)

    Unable to write a file to disk, includes the path and the underlying file error

Values

pub fn add_dynamic_route(
  ssg: SSG(page_type),
  base_path: String,
  data: dict.Dict(String, data_type),
  render_page: fn(data_type) -> page_type,
) -> SSG(page_type)

Add multiple dynamic routes from a dictionary of data. Each entry in the dictionary will generate a separate page at {base_path}/{key}/index.html.

Parameters

  • ssg: The SSG instance to add routes to
  • base_path: The base URL path (e.g., “/blog”, “/posts”)
  • data: A dictionary where keys are URL slugs and values are the data for each page
  • render_page: A function that converts data into a page

Example

let assert Ok(posts) = markup.parse_dir("data/posts")

ssg.new("priv", element.to_document_string)
|> ssg.add_dynamic_route("/blog", dict.from_list(posts), post.view)

If posts contains [#("hello-world", post1), #("second-post", post2)], this will generate:

  • priv/blog/hello-world/index.html
  • priv/blog/second-post/index.html
pub fn add_static_route(
  ssg: SSG(page_type),
  path: String,
  page: page_type,
) -> SSG(page_type)

Add a static route to your site. The page will be rendered to {path}/index.html.

Parameters

  • ssg: The SSG instance to add the route to
  • path: The URL path (e.g., “/”, “/about”, “/contact”)
  • page: The page content to render

Example

ssg.new("priv", element.to_document_string)
|> ssg.add_static_route("/", home.view())
|> ssg.add_static_route("/about", about.view())

This will generate:

  • priv/index.html
  • priv/about/index.html
pub fn build(ssg: SSG(page_type)) -> Result(Nil, SSGBuildError)

Build the static site by rendering all routes and writing them to disk. Creates all necessary directories and writes HTML files.

Parameters

  • ssg: The SSG instance with all routes configured

Returns

  • Ok(Nil) if the build succeeds
  • Error(SSGBuildError) if any file cannot be written

Example

let result =
  ssg.new("priv", element.to_document_string)
  |> ssg.add_static_route("/", home.view())
  |> ssg.build()

case result {
  Ok(_) -> io.println("Build successful!")
  Error(e) -> io.println("Build failed: " <> string.inspect(e))
}
pub fn new(
  out_dir: String,
  render: fn(page_type) -> String,
) -> SSG(page_type)

Create a new SSG instance with an output directory and render function.

Parameters

  • out_dir: The directory where generated HTML files will be written
  • render: A function that converts your page type to an HTML string

Example

import lustre/element
import gssg/ssg

let site = ssg.new("priv", element.to_document_string)
Search Document