# `Macaw.Page`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/page.ex#L1)

A page: the final browser document at one URL (spec/06 §1). It owns its title and its
one `<h1>`, its template stack, the regions it fills with surfaces, and the params it
accepts.

    {:ok, page} =
      Macaw.Page.new(
        name: "service",
        path: "/service/:service",
        title: "Service",
        h1: "Service",
        template: ["site"],
        regions: %{"main" => ["service_overview"]},
        params: %{"service" => %{type: :category}}
      )

A page is a document whose value is a term, read from JSON through the kernel's gates,
as a surface is: its value is `%{version: v, page: node}` (`to_map/1`), judged as terms
by `from_map/2`, and its JSON form `{"$version": v, "page": {…}}` is an adapter at the
edge (`to_json/1`, `from_json/2`).

`validate/3` is its static pass: its stack, its fills, its one `main`, each surface known
and valid, and its plans, the union of its components' declarations. Its params are read
from a request by `match/2` and `params/2`, before anything renders, an illegal value a
`404`-class refusal (spec/06 §5).

It holds no process and reads no clock. Names are strings, an atom read as its spelling;
no atom is made from a document.

# `entry`

```elixir
@type entry() :: %{surface: String.t()}
```

One surface a region holds, in its flow.

# `read`

```elixir
@type read() :: %{
  parameters: %{required(String.t()) =&gt; Macaw.Data.Parameter.t()},
  segments: [String.t()] | nil,
  values_from: %{required(String.t()) =&gt; {String.t(), String.t()}}
}
```

What a request reads of the page, built by `new/1` once (#417), never per request: its
params as the parameters `params/2` judges values by, its path pattern's segments (`nil`
for `/`), and each `values_from` param's `{source, field}`.

# `t`

```elixir
@type t() :: %Macaw.Page{
  h1: String.t(),
  name: String.t(),
  params: %{required(String.t()) =&gt; map()},
  path: String.t(),
  read: read() | nil,
  regions: %{required(String.t()) =&gt; [entry(), ...]},
  template: [String.t(), ...],
  title: String.t()
}
```

# `check`

```elixir
@spec check(t(), map(), keyword()) ::
  {:ok, map()}
  | {:not_found, [Macaw.Base.Fault.t(), ...]}
  | {:error, [Macaw.Base.Fault.t(), ...]}
```

The request-time check of the page's params (spec/07 §2.2, spec/06 §5), before anything
renders: each param whose legal values come from data (`values_from/1`) and whose value
isn't `nil` against its source's current values, read through the host's source with
the request's viewer, or through its `Macaw.Surface.Reader` given as `manager:` (#443).
Each `{source, field}` is read once, however many params take their values from it.
`params` are the values `params/2` answered.

It answers `{:ok, params}`; `{:not_found, faults}`, a `404`-class refusal as `params/2`'s
is, each illegal value `{[name], {:not_allowed, value}}`; or `{:error, faults}` when the
check can't be made, which a host answers as a server's fault, never a `404`: a source
the host doesn't give `{[name], {:unknown_source, source}}`, one it gives malformed
`{:bad_source, source}`, a field it doesn't describe `{:unknown_field, field}`, a source
that can't answer now `{:unavailable, why}`, and a bad env by key, as
`Macaw.Surface.legal/2` judges it (`sources:` and `now:` required, `viewer:`,
`timeout:` and `manager:` optional).

    iex> {:ok, page} = Macaw.Page.new(name: "service", path: "/service/:service", title: "S", h1: "S", template: ["site"], params: %{"service" => %{type: :category, values_from: "services.host"}})
    iex> sources = %{"services" => {%Macaw.Test.Rows{rows: [%{"t" => ~U[2026-11-18 12:00:00Z], "v" => 1, "host" => "api"}]}, nil}}
    iex> Macaw.Page.check(page, %{"service" => "api"}, sources: sources, now: ~U[2026-11-18 12:05:00Z])
    {:ok, %{"service" => "api"}}
    iex> Macaw.Page.check(page, %{"service" => "web"}, sources: sources, now: ~U[2026-11-18 12:05:00Z])
    {:not_found, [{["service"], {:not_allowed, "web"}}]}

# `explain`

```elixir
@spec explain(t(), keyword()) :: {:ok, map()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The page document shown whole, as readable data (spec/06 §1.3): `{:ok, explained}` or
every fault at once, by key or by path from the page. It reads no source and validates
nothing: what it shows is what the documents say, and what the host supplies.

`explained` is `to_map/1`'s map with two keys beside `page`:

  * `surfaces`, every surface the page holds by name, its containers' panes among them,
    each `Macaw.Surface.to_map/1` of it: its cells, state, actions and controls;
  * `host`, what the host supplies, each part named and marked `host: true`, with
    `given` whether this env gives it: `sources`, each source the documents bind or the
    host gives, by name, with `bound` the paths in `explained` that bind it; `values`,
    each `{:host, key}` initial by key, as `sources` are; `registry`; `auth`, the viewer
    the host's auth answers; and `callbacks`, `"links.to"` and `"manager"`.

Only names are read of the host's parts: a source's configuration and credentials, the
registry's packages, the viewer, a callback and a host value never appear (macaw_data
spec/01 §7). Without its `host` key, `from_map/2` of the page's map and
`Macaw.Surface.from_map/2` of each surface's rebuild the page and its surfaces.

`env` is a keyword list: `surfaces:` as `validate/3` takes it, required; `sources:` and
`host:` maps by name, `registry:`, `viewer:`, `manager:` and `links:` as `show/2` and
`Macaw.State.new/2` take them, each optional. Its faults: `{[], {:type, :keyword}}`,
`{[key], :unknown_key}`, `{[key], :duplicate}`, `{[:surfaces], :required}`, `{:type,
:map}` at a map's key, `{:type, :names}` for a map with a key that is no name (never
echoed), `{[:registry], {:type, :registry}}`, the `manager:` and `links:` faults
`show/2` gives; then a surface the page names that `surfaces:` doesn't hold,
`{:unknown_surface, name}` at its `:surface`, and one that isn't a `%Macaw.Surface{}`
`{:bad_surface, name}` there.

    iex> {:ok, page} = Macaw.Page.new(name: "home", path: "/", title: "Home", h1: "Home", template: ["site"], regions: %{"main" => ["welcome"]})
    iex> {:ok, welcome} = Macaw.Surface.build("welcome", cells: [{"macaw.table", source: "orders"}])
    iex> {:ok, explained} = Macaw.Page.explain(page, surfaces: %{"welcome" => welcome}, sources: %{"orders" => {:configured, "secret"}})
    iex> explained.host.sources
    %{"orders" => %{host: true, given: true, bound: [[:surfaces, "welcome", :surface, :cells, "table_orders", :source]]}}
    iex> Enum.sort(Map.keys(explained))
    [:host, :page, :surfaces, :version]

# `from_json`

```elixir
@spec from_json(String.t(), keyword()) ::
  {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The page a JSON document holds: read through the JSON adapter (every gate, the version,
the limits and the migration, then the value, judged), then `new/1`, every fault by path
from the document. `limits:` as `Macaw.Layers.Form.gates/3` takes them; `vocabulary:`
the atoms a param's default or values may spell beyond the document's own words.

# `from_map`

```elixir
@spec from_map(term(), keyword()) ::
  {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The page a document value holds, judged as terms, or every fault by path from the
document: the version first, then the envelope (`page` and no other key), then `new/1`,
its faults under `:page`. No form is read, so no limit applies.

# `href`

```elixir
@spec href(t(), map()) ::
  {:ok, String.t()} | {:not_found, [Macaw.Base.Fault.t(), ...]}
```

The URL of the page at its params (spec/06 §5): `{:ok, url}`, its path with each path
param's text in place, then each other param whose value isn't its default in the
query, in name order; or `{:not_found, faults}`, `params/2`'s faults of the values, and
`:no_text` at a value with no text form (a theme color). `params/2` of `match/2` and the
query of the URL reads the params back.

    iex> {:ok, page} = Macaw.Page.new(name: "service", path: "/service/:service", title: "S", h1: "S", template: ["site"], params: %{"service" => %{type: :category}, "limit" => %{type: :number, default: 10}})
    iex> Macaw.Page.href(page, %{"service" => "api gateway", "limit" => 25})
    {:ok, "/service/api%20gateway?limit=25"}
    iex> Macaw.Page.href(page, %{"service" => "api", "limit" => 10})
    {:ok, "/service/api"}

# `match`

```elixir
@spec match(t(), String.t()) :: {:ok, %{required(String.t()) =&gt; String.t()}} | :error
```

The path params of a request's path, when the page's path matches it: `{:ok,
path_params}`, each segment decoded from its URL encoding by name, or `:error`
(spec/06 §5, page params).

    iex> {:ok, page} = Macaw.Page.new(name: "service", path: "/service/:service", title: "S", h1: "S", template: ["site"], params: %{"service" => %{type: :category}})
    iex> Macaw.Page.match(page, "/service/api%20gateway")
    {:ok, %{"service" => "api gateway"}}
    iex> Macaw.Page.match(page, "/services")
    :error

# `new`

```elixir
@spec new(keyword() | map()) :: {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

A page from a keyword list or a map with atom keys: `{:ok, page}` or every fault at
once, by path from the page, in the kernel's order (spec/06 §1.1).

    iex> {:ok, page} = Macaw.Page.new(name: :home, path: "/", title: "Home", h1: "Home", template: [:site], regions: %{main: [:welcome]})
    iex> page.regions
    %{"main" => [%{surface: "welcome"}]}
    iex> Macaw.Page.new(name: "home", path: "/p/:id", title: "", template: [])
    {:error, [{[:h1], :required}, {[:path], {:undeclared_param, "id"}}, {[:template], {:type, {:list, :name}}}, {[:title], {:type, :string}}]}

# `node`

```elixir
@spec node(t()) :: map()
```

The page as its node: its keys, every name a string, `regions` and `params` only when not empty.

# `params`

```elixir
@spec params(t(), map()) :: {:ok, map()} | {:not_found, [Macaw.Base.Fault.t(), ...]}
```

The page's params from a request's values, before anything renders (spec/06 §5):
`{:ok, params}`, every declared param by name with its default filled in, or
`{:not_found, faults}`, a `404`-class refusal, never a component's fault. `given` is the
path's and the query's values by name: a string is read as the URL's text for the
param's type, the empty string not given; any other term is judged as the value it is.
A name the page doesn't declare is left alone.

    iex> {:ok, page} = Macaw.Page.new(name: "list", path: "/list", title: "L", h1: "L", template: ["site"], params: %{"limit" => %{type: :number, default: 10}, "order" => %{type: :category, values: ["asc", "desc"]}})
    iex> Macaw.Page.params(page, %{"order" => "desc", "limit" => "25", "sort" => "x"})
    {:ok, %{"limit" => 25, "order" => "desc"}}
    iex> Macaw.Page.params(page, %{"order" => "up", "limit" => "many"})
    {:not_found, [{["limit"], {:type, :number}}, {["order"], {:enum, ["asc", "desc"]}}]}

# `steps`

```elixir
@spec steps(Macaw.Page.Validated.t(), Macaw.Data.Event.t()) ::
  {:ok, [%{to: String.t(), params: map(), href: String.t()}]}
  | {:error, [Macaw.Base.Fault.t(), ...]}
```

The navigation steps an event takes (spec/06 §5): `{:ok, steps}`, each `navigate`
action of the event's cell on the event's name, in declared order, `%{to: page, params:
params, href: url}`, its params read from the event and judged against the target's as
`params/2` judges them, and `href` the target's URL at them; or `{:error, faults}` with
no step taken. The library never navigates: the host performs each step (macaw_live
01 §9). The view change the same event makes is `Macaw.State.apply/2`'s.

Its faults: an event whose instance is no cell of the page, `{[:instance],
{:unknown_instance, instance}}`; a value the target refuses, at `[:actions, i, :params,
name]`, `i` the action's index among the cell's actions.

# `surfaces`

```elixir
@spec surfaces(Macaw.Page.Validated.t()) :: [Macaw.Page.Validated.entry()]
```

Every surface a validated page holds, its containers' panes among them, in page order
(spec/05 §5.1): the regions by name, each region's surfaces in flow order, each followed
depth first by its panes, its container cells in reading order and each one's panes in
order.

Each is `%{surface: name, validated: validated, at: path, depth: depth, container:
container}`: `at` its path from the page (`[:regions, region, i]`, a pane's its
container's `at` and `[:cells, cell, :panes, j]`), `depth` 1 for a region's and one more
per pane, and `container` `nil` for a region's, else `%{surface:, cell:, pane:}`. A pane
is listed shown or not: what holds state and computes reads this, and only rendering
waits for a pane to show (spec/05 §5.2).

# `to_json`

```elixir
@spec to_json(t()) :: {:ok, String.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The JSON document, `{:ok, json}`, stamped `"$version"`, written by the JSON adapter: a
param's default and values as their tags (a time as `$time`, an atom as `$atom`, a
point as `$point`). A value JSON can't hold is refused at its path.

# `to_map`

```elixir
@spec to_map(t()) :: %{version: pos_integer(), page: map()}
```

`%{version: current, page: node}`: the page's value as a document.

# `validate`

```elixir
@spec validate(t(), Macaw.Registry.t(), keyword()) ::
  {:ok, Macaw.Page.Validated.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The page's static pass (spec/06 §1.2): `{:ok, %Macaw.Page.Validated{}}` or every fault
at once, by path from the page. `env` is a keyword list: `surfaces:` the surfaces its
regions may name, by name, and `sources:` the host's sources as
`Macaw.Surface.validate/3` takes them, both required; `pages:` the pages its `navigate`
actions may name, by name, `%{}` by default, the page itself always among them.

It stacks the page's template, checks its fills against the stacked regions, its one
`main`, each surface known and valid with the registry (its faults at
`[:regions, region, i | path]`), each state that reads a page param against its params,
and each `navigate` action's target and params against the target's (spec/06 §5), and
gathers its plans. Each container cell's panes are resolved against `surfaces:` and
validated where they sit, depth first, to a depth of 3 (spec/05 §5.1, §5.4): a pane's
faults are at its path, `[…, :cells, cell, :panes, j | path]`, and a surface the page
already holds is `{:duplicate, name}` at its `:surface`.

# `values_from`

```elixir
@spec values_from(t()) :: %{required(String.t()) =&gt; {String.t(), String.t()}}
```

The params whose legal values come from data, `%{name => {source, field}}`: the hook a
request-time check reads (spec/06 §5, spec/07 §2.2). `params/2` judges such a param's
type and nothing more, and `check/3` its value against the source's current values.

    iex> {:ok, page} = Macaw.Page.new(name: "service", path: "/service/:service", title: "S", h1: "S", template: ["site"], params: %{"service" => %{type: :category, values_from: "services.name"}})
    iex> Macaw.Page.values_from(page)
    %{"service" => {"services", "name"}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
