# `Markup.Language`
[🔗](https://github.com/paradox460/markup/blob/markup-v0.1.0/core/lib/markup/language.ex#L1)

The behaviour a markup language package implements.

The core (`Markup`, `Markup.Document`) owns the document, the plugin
pipeline, Access/Enumerable, traversal, streaming, sigils and the HEEx glue.
A language owns its node structs, its options and its parser and renderer.

## Discovery

A language package registers its language module in its application env,
in `mix.exs`:

    def application do
      [env: [markup_languages: [MyLang]]]
    end

`Markup.Languages.all/0` collects `:markup_languages` from every loaded
application, so adding the package to your deps is all it takes. You can also
list modules explicitly with `config :markup, languages: [MyLang]`.

## Callbacks

  * `c:id/0` - lowercase ASCII letters only, e.g. `:djot`. Used for `language:`
    and the `~MU` sigil modifier.
  * `c:node_namespace/0` - bare-atom selectors that aren't a `Markup.Node`
    kind resolve under it (`:div` becomes `Namespace.Div`).
  * `c:formats/0` - output formats; the first MUST be `:html`.
  * `c:options_schema/0` - a `NimbleOptions` schema for the language's own
    option keys (e.g. `:render`, `:parse`). The core adds `:plugins`,
    `:sanitize` and `:assigns`.
  * `c:parse/2` - parses source into top-level node structs.
  * `c:render/3` - renders a document whose pipeline has already run. It applies
    syntax highlighting, sanitizes with
    `Markup.Sanitize.prepare(Markup.Document.get_option(doc, :sanitize))` (a fully
    validated keyword list, or `nil`) and escapes HEEx braces when `ctx.heex`.
  * `c:default_sanitize_options/0` - defaults merged under user `sanitize:` options.
    Keys follow `Markup.Sanitize.schema/0`.
  * `c:safe_html/3` - sanitizes (`sanitize` is already merged and prepared: a map,
    or `nil` to skip) and optionally HTML-escapes a string.

Optional:

  * `c:merge_option/3` - merges a new value for one of the language's option
    keys into the current one. Defaults to a deep `Keyword.merge/2` when both
    are keyword lists, otherwise the new value replaces the old. The result is
    validated against the schema entry.
  * `c:render_source/4` - a fast path that renders source directly. It's only
    called when the document has no nodes, has a binary source and has no
    pending steps. The language can still fall back to its tree path internally.
  * `c:stream_blocks/2` - parses a stream buffer into top-level blocks.
    Defaults to `c:parse/2`.
  * `c:heex_prepare/1` - prepares a document for HEEx rendering (for example by
    rewriting HEEx constructs in the source). Defaults to identity.

# `format`

```elixir
@type format() :: atom()
```

# `render_ctx`

```elixir
@type render_ctx() :: %{heex: boolean()}
```

# `default_sanitize_options`

```elixir
@callback default_sanitize_options() :: keyword()
```

# `formats`

```elixir
@callback formats() :: [format()]
```

# `heex_prepare`
*optional* 

```elixir
@callback heex_prepare(Markup.Document.t()) :: Markup.Document.t()
```

# `id`

```elixir
@callback id() :: atom()
```

# `merge_option`
*optional* 

```elixir
@callback merge_option(key :: atom(), current :: term(), value :: term()) :: term()
```

# `node_namespace`

```elixir
@callback node_namespace() :: module()
```

# `options_schema`

```elixir
@callback options_schema() :: keyword()
```

# `parse`

```elixir
@callback parse(source :: String.t(), Markup.Document.t()) ::
  {:ok, [struct()]} | {:error, Exception.t()}
```

# `render`

```elixir
@callback render(Markup.Document.t(), format(), render_ctx()) ::
  {:ok, String.t()} | {:error, Exception.t()}
```

# `render_source`
*optional* 

```elixir
@callback render_source(String.t(), Markup.Document.t(), format(), render_ctx()) ::
  {:ok, String.t()} | {:error, Exception.t()}
```

# `safe_html`

```elixir
@callback safe_html(html :: String.t(), sanitize :: map() | nil, escape :: boolean()) ::
  {:ok, String.t()} | {:error, term()}
```

# `stream_blocks`
*optional* 

```elixir
@callback stream_blocks(buffer :: String.t(), Markup.Document.t()) ::
  {:ok, [struct()]} | {:error, Exception.t()}
```

---

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