View Source FancyFences.Processors (fancy_fences v0.3.1)

Common fence processors.

Summary

Functions

A fence processor for documenting fence processors usage.

Formats the given code block.

Embeds the original code and the evaluated result.

Embeds the original code and the evaluated results supporing multiple inspection points.

Functions

Link to this function

fence_processor_doc(code)

View Source

A fence processor for documenting fence processors usage.

This can be used for documenting fence processors. It return an admonition block containing both the sample code and the output of the fence processor.

Usage

It expects a map with the following key-value pairs:

  • block - the code block to use as an example
  • processor - an anonymous function with the processor that will be used.

For example:

```fence-processor
%{
  block: "Enum.map([1, 2, 3], fn x -> 2*x end)",
  processor: fn block -> FancyFences.Processors.inspect_code(block) end
}
```

Embedded code

A fenced code block of the form:

```lang
%{
   block: "1 + 1",
   processor: fn block ->
     "**Code:** `"<> block <> "`"
   end
}

```

will be transformed to:

Embedded code

A fenced code block of the form:

```lang
1 + 1
```

will be transformed to:

Code: 1 + 1

where lang the defined code language for this fence processor in your mix.exs.

where lang the defined code language for this fence processor in your mix.exs.

Notice that above we used fence_processor_doc/1 to document itself, that's why we have the nested admonition block.

Formats the given code block.

Embedded code

A fenced code block of the form:

```lang
for x <- [1, 2, 3] do
2 * x
end

```

will be transformed to:

for x <- [1, 2, 3] do
  2 * x
end

where lang the defined code language for this fence processor in your mix.exs.

Link to this function

inspect_code(code, opts \\ [])

View Source

Embeds the original code and the evaluated result.

Options

  • :format (boolean) - If set to true the code blocks will be formatted before inection. Defaults to false.

Embedded code

A fenced code block of the form:

```lang
Enum.map([1, 2, 3, 4], fn x -> 2*x end)
```

will be transformed to:

Enum.map([1, 2, 3, 4], fn x -> 2*x end)
[2, 4, 6, 8]

where lang the defined code language for this fence processor in your mix.exs.

Link to this function

multi_inspect(code, opts \\ [])

View Source
@spec multi_inspect(code :: String.t(), opts :: keyword()) :: String.t()

Embeds the original code and the evaluated results supporing multiple inspection points.

Options

  • :format (boolean) - If set to true the code blocks will be formatted before inection. Defaults to false.
  • :iex_prefix (boolean) - If set to true an iex> prefix will be added to the original code lines similar to doc tests. Defaults to false.
  • :separator (string) - The separator for inspection points. The original code will be split on these separators and an inspect statement will be added after each one. Defaults to >>>.

Embedded code

A fenced code block of the form:

```lang
list = [1, 2, 3, 4]
Enum.map(list, fn x -> 2*x end)
>>>

Enum.map(list, fn x -> 3 + x end)
>>>

Enum.reduce(list, 0, fn x, acc -> x + acc end)

```

will be transformed to:

iex> list = [1, 2, 3, 4]
...> Enum.map(list, fn x -> 2 * x end)
[2, 4, 6, 8]

iex> Enum.map(list, fn x -> 3 + x end)
[4, 5, 6, 7]

iex> Enum.reduce(list, 0, fn x, acc -> x + acc end)
10

where lang the defined code language for this fence processor in your mix.exs.

Notice that the last statement is inspected by default and there is no need to add a separator.