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
endThis 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
@type action_name() :: atom()
@type action_spec() :: [action_spec_opt()]
@type action_spec_opt() :: {:as, function_name :: atom()} | {:params, NimbleOptions.schema()} | {:doc, String.t()}
@type block() :: {:text, iodata()} | {:generated, block_spec()}
@type block_name() :: atom()
@type block_spec() :: Readmix.BlockSpec.t()
Callbacks
@callback actions() :: [{action_name(), action_spec()}]
@callback generate(block_name(), params(), context()) :: {:ok, iodata()} | {:error, term()}
Functions
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- aNimbleOptionsschema 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.