NLdoc.Conversion.Writer.Html.State (NLdoc.Conversion.Writer.Html v1.2.34)

View Source

This module defines a struct that captures the state of the converter during a HTML conversion.

Summary

Functions

Get current footnote number based on footnotes in state.

Get asset by id from state.

Gets the given field from the state object.

Modifies a state object by setting the given field to the value that field had in the old state.

Prepends a list of values or a single value to the list field in the state object.

Sets the given field on the state object to the given value.

Upsert new footnote to state.

Types

html_tag_or_text()

@type html_tag_or_text() :: Floki.html_tag() | Floki.html_text()

t()

@type t() :: %NLdoc.Conversion.Writer.Html.State{
  assets: [NLdoc.Spec.Asset.t()] | nil,
  existing_footnote_ids: [String.t()] | nil,
  ordered_footnote_ids: %{required(String.t()) => number()} | nil
}

Functions

current_footnote_number(state)

@spec current_footnote_number(t()) :: number()

Get current footnote number based on footnotes in state.

Examples

iex> %NLdoc.Conversion.Writer.Html.State{ordered_footnote_ids: %{ "abc" => 1, "xyz" => 2 }}
...> |> NLdoc.Conversion.Writer.Html.State.current_footnote_number()
2

find_asset(state, id)

@spec find_asset(state :: t(), id :: String.t()) :: NLdoc.Spec.Asset.t() | nil

Get asset by id from state.

Examples

iex> alias NLdoc.Conversion.Writer.Html.State
iex> state = %State{assets: [%NLdoc.Spec.Asset{id: "123"}]}
iex> state |> State.find_asset("123")
%NLdoc.Spec.Asset{id: "123"}
iex> state |> State.find_asset("456")
nil

get(state, atom)

@spec get(t(), :ordered_footnote_ids) :: %{required(String.t()) => number()}
@spec get(t(), :existing_footnote_ids) :: [String.t()]
@spec get(t(), :assets) :: [NLdoc.Spec.Asset.t()]

Gets the given field from the state object.

This function only accepts fields of the state, which are :ordered_footnote_ids, :existing_footnote_ids, :assets. It will raise if the field is not valid.

keep(new_state, old_state, atom)

@spec keep(t(), t(), :ordered_footnote_ids) :: t()
@spec keep(t(), t(), :existing_footnote_ids) :: t()
@spec keep(t(), t(), :assets) :: t()

Modifies a state object by setting the given field to the value that field had in the old state.

This function only accepts fields of the state, which are :ordered_footnote_ids, :existing_footnote_ids, :assets. It will raise if the field is not valid.

prepend(state, atom, values)

@spec prepend(t(), :existing_footnote_ids, [String.t()]) :: t()
@spec prepend(t(), :existing_footnote_ids, String.t()) :: t()
@spec prepend(t(), :assets, [NLdoc.Spec.Asset.t()]) :: t()
@spec prepend(t(), :assets, NLdoc.Spec.Asset.t()) :: t()

Prepends a list of values or a single value to the list field in the state object.

This function only accepts fields of the state, which are :existing_footnote_ids, :assets. It will raise if the field is not valid.

set(state, atom, value)

@spec set(t(), :ordered_footnote_ids, %{required(String.t()) => number()}) :: t()
@spec set(t(), :existing_footnote_ids, [String.t()]) :: t()
@spec set(t(), :assets, [NLdoc.Spec.Asset.t()]) :: t()

Sets the given field on the state object to the given value.

This function only accepts fields of the state, which are :ordered_footnote_ids, :existing_footnote_ids, :assets. It will raise if the field is not valid.

upsert_footnote_id(state, id)

@spec upsert_footnote_id(t(), String.t()) :: {number() | nil, t()}

Upsert new footnote to state.

Examples

When reference was encountered that refers to a valid footnote.

iex> %NLdoc.Conversion.Writer.Html.State{
...>   ordered_footnote_ids: %{ "abc" => 1 },
...>   existing_footnote_ids: ["abc", "xyz"]}
...> |> NLdoc.Conversion.Writer.Html.State.upsert_footnote_id("xyz")
{2, %NLdoc.Conversion.Writer.Html.State{
  ordered_footnote_ids: %{"abc" => 1, "xyz" => 2},
  existing_footnote_ids: ["abc", "xyz"]}}

When dead reference was encountered.

iex> %NLdoc.Conversion.Writer.Html.State{
...>   ordered_footnote_ids: %{ "abc" => 1 },
...>   existing_footnote_ids: ["abc", "xyz"]}
...> |> NLdoc.Conversion.Writer.Html.State.upsert_footnote_id("def")
{nil, %NLdoc.Conversion.Writer.Html.State{
  ordered_footnote_ids: %{"abc" => 1},
  existing_footnote_ids: ["abc", "xyz"]}}

When footnote is referenced that was referenced before

iex> %NLdoc.Conversion.Writer.Html.State{
...>   ordered_footnote_ids: %{ "abc" => 1, "xyz" => 2 },
...>   existing_footnote_ids: ["abc", "xyz"]}
...> |> NLdoc.Conversion.Writer.Html.State.upsert_footnote_id("xyz")
{2, %NLdoc.Conversion.Writer.Html.State{
  ordered_footnote_ids: %{"abc" => 1, "xyz" => 2},
  existing_footnote_ids: ["abc", "xyz"]}}