NLdoc.Conversion.Writer.Html.State (NLdoc.Conversion.Writer.Html v1.2.34)
View SourceThis 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
@type html_tag_or_text() :: Floki.html_tag() | Floki.html_text()
@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
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
@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
@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.
@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.
@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.
@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 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"]}}