Sayfa.Config (Sayfa v0.5.0)

Copy Markdown View Source

Configuration handling for Sayfa sites.

Provides functions to read site configuration with sensible defaults. Configuration is resolved from three layers (lowest to highest priority):

  1. Built-in defaults
  2. Application environment (config :sayfa, :site, ...)
  3. Runtime options passed to resolve/1

Examples

# Get a single config value
Sayfa.Config.get(:title)
#=> "My Site"

# Get with a custom default
Sayfa.Config.get(:missing_key, "fallback")
#=> "fallback"

# Resolve full config map
config = Sayfa.Config.resolve(content_dir: "custom/content")
config.content_dir
#=> "custom/content"

Summary

Functions

Returns the absolute path to a file within the default theme.

Returns the built-in default configuration map.

Gets a single configuration value.

Resolves a complete configuration map.

Returns the path to the layouts directory for the given config.

Functions

default_theme_path(subpath)

@spec default_theme_path(String.t()) :: String.t()

Returns the absolute path to a file within the default theme.

Uses :code.priv_dir/1 to locate Sayfa's priv/ directory.

Examples

iex> path = Sayfa.Config.default_theme_path("layouts")
iex> String.ends_with?(path, "priv/default_theme/layouts")
true

defaults()

@spec defaults() :: map()

Returns the built-in default configuration map.

Examples

iex> defaults = Sayfa.Config.defaults()
iex> defaults.title
"My Site"

get(key)

@spec get(atom()) :: term()

Gets a single configuration value.

Looks up the key in application environment, falling back to built-in defaults.

Examples

iex> Sayfa.Config.get(:title)
"My Site"

iex> Sayfa.Config.get(:nonexistent, "default")
"default"

get(key, default)

@spec get(atom(), term()) :: term()

resolve(opts \\ [])

@spec resolve(keyword()) :: map()

Resolves a complete configuration map.

Merges built-in defaults, application environment, and runtime options (in that priority order) into a single map.

Examples

iex> config = Sayfa.Config.resolve(title: "Custom Site")
iex> config.title
"Custom Site"

iex> config = Sayfa.Config.resolve([])
iex> config.output_dir
"dist"

theme_layouts_dir(map)

@spec theme_layouts_dir(map()) :: String.t()

Returns the path to the layouts directory for the given config.

For the default theme, returns the path inside Sayfa's priv/ directory. For custom themes, returns themes/<name>/layouts/ relative to the site root.

Examples

iex> config = %{theme: "default"}
iex> path = Sayfa.Config.theme_layouts_dir(config)
iex> String.ends_with?(path, "default_theme/layouts")
true