Readmix.Generator behaviour (readmix v0.8.1)

Copy Markdown

Defines the behavior and utilities for Readmix generators.

Generators are modules that implement this behaviour and provide actions that can be invoked from documents.

To create a generator, use:

defmodule MyGenerator do
  use Readmix.Generator

  @doc "This action does something"
  action :my_action,
    as: :generate_my,
    params: [
      param1: [type: :string, doc: "Parameter documentation"]
    ]

  @moduledoc """
  My custom actions.

  #{Readmix.Docs.generate()}
  """

  defp generate_my(params, context) do
    {:ok, "Generated content"}
  end
end

This is equivalent to the following:

defmodule MyGenerator do
  @behaviour Readmix.Generator

  @actions [
    my_action: [
      params: [
        param1: [type: :string, doc: "Parameter documentation"]
      ],
      doc: "This action does something"
    ]
  ]

  @moduledoc """
  My custom actions.

  #{Readmix.Docs.generate(@actions)}
  """

  @impl true
  def actions, do: @actions

  @impl true
  def generate(:my_action, params, context) do
    generate_my(params, context)
  end

  defp generate_my(params, context) do
    {:ok, "Generated content"}
  end
end

Summary

Functions

Declares a generator action.

Types

action_name()

@type action_name() :: atom()

action_spec()

@type action_spec() :: [action_spec_opt()]

action_spec_opt()

@type action_spec_opt() ::
  {:as, function_name :: atom()}
  | {:params, NimbleOptions.schema()}
  | {:doc, String.t()}

block()

@type block() :: {:text, iodata()} | {:generated, block_spec()}

block_name()

@type block_name() :: atom()

block_spec()

@type block_spec() :: Readmix.BlockSpec.t()

context()

@type context() :: %Readmix.Context{
  block: term(),
  previous_content: [block()],
  readmix: Readmix.t(),
  siblings: term()
}

params()

@type params() :: %{optional(binary()) => integer() | float() | binary()}

Callbacks

actions()

@callback actions() :: [{action_name(), action_spec()}]

generate(block_name, params, context)

@callback generate(block_name(), params(), context()) ::
  {:ok, iodata()} | {:error, term()}

Functions

action(atom, spec)

(macro)

Declares a generator action.

The first argument is the action name as used in document tags. The second is a keyword list describing the action:

  • :as - the name of the private function implementing the action. Defaults to the action name itself.
  • :params - a NimbleOptions schema validating the parameters accepted by the action.

A @doc attribute placed immediately before the action/2 call is captured and used to document the action in the generated module documentation.