Gumbo.Options (gumbo v0.3.0)

Copy Markdown View Source

Parser configuration for Gumbo.parse/2.

Five fields map one-to-one onto gumbo's GumboOptions; :dirty is ours and controls scheduler dispatch rather than the parse itself. new/1 validates every key against gumbo's documented ranges and rejects anything else with {:error, {:invalid_option, key, value}} before any work happens.

Summary

Types

t()

tab_stop, stop_on_first_error, max_errors, max_attributes, max_tree_depth, fragment_context and fragment_namespace are GumboOptions fields; :dirty is this library's scheduler policy. fragment_context is nil (no fragment, gumbo's GUMBO_TAG_LAST) or a tag atom. tab_stop, max_errors, max_attributes and max_tree_depth are bounded by C int range, so a value that passes new/1 can never fail the NIF decode. max_errors defaults to 100 and max_attributes to 1024 rather than to gumbo's "no cap"; see the comments on the defaults for why, and pass -1 to either for no cap. max_tree_depth counts the parser's open elements, so an implied <html> counts and a fragment passes 401 to mean 400 levels of content, as the reference implementation's Ruby layer does; it defaults to -1, because the stack budget is what bounds depth here.

Functions

The workload size at and above which dispatch/2 picks :dirty.

The single scheduler policy for parse/2 and materialize/2: :dirty when forced, :normal when forbidden, and :dirty when :auto and the workload reaches dirty_threshold/0.

Returns the default options.

Validates opts against gumbo's documented ranges and merges them over the defaults.

Converts the struct to the map the NIF decodes.

Types

t()

@type t() :: %Gumbo.Options{
  dirty: :auto | boolean(),
  fragment_context: nil | Gumbo.Tag.t(),
  fragment_namespace: Gumbo.Types.namespace(),
  max_attributes: -1..2_147_483_647,
  max_errors: -1..2_147_483_647,
  max_tree_depth: -1..2_147_483_647,
  stop_on_first_error: boolean(),
  tab_stop: 1..2_147_483_647
}

tab_stop, stop_on_first_error, max_errors, max_attributes, max_tree_depth, fragment_context and fragment_namespace are GumboOptions fields; :dirty is this library's scheduler policy. fragment_context is nil (no fragment, gumbo's GUMBO_TAG_LAST) or a tag atom. tab_stop, max_errors, max_attributes and max_tree_depth are bounded by C int range, so a value that passes new/1 can never fail the NIF decode. max_errors defaults to 100 and max_attributes to 1024 rather than to gumbo's "no cap"; see the comments on the defaults for why, and pass -1 to either for no cap. max_tree_depth counts the parser's open elements, so an implied <html> counts and a fragment passes 401 to mean 400 levels of content, as the reference implementation's Ruby layer does; it defaults to -1, because the stack budget is what bounds depth here.

Functions

dirty_threshold()

@spec dirty_threshold() :: 20480

The workload size at and above which dispatch/2 picks :dirty.

Examples

iex> Gumbo.Options.dirty_threshold()
20_480

dispatch(options, size)

@spec dispatch(t(), non_neg_integer()) :: :normal | :dirty

The single scheduler policy for parse/2 and materialize/2: :dirty when forced, :normal when forbidden, and :dirty when :auto and the workload reaches dirty_threshold/0.

Examples

iex> Gumbo.Options.dispatch(Gumbo.Options.new(), 20_479)
:normal

iex> Gumbo.Options.dispatch(Gumbo.Options.new(), 20_480)
:dirty

iex> {:ok, forced} = Gumbo.Options.new(dirty: true)
iex> Gumbo.Options.dispatch(forced, 1)
:dirty

new()

@spec new() :: t()

Returns the default options.

Examples

iex> Gumbo.Options.new().tab_stop
8

new(opts)

@spec new(keyword() | map()) :: {:ok, t()} | {:error, term()}

Validates opts against gumbo's documented ranges and merges them over the defaults.

An unknown key or an out-of-range value returns {:error, {:invalid_option, key, value}}.

Examples

iex> {:ok, options} = Gumbo.Options.new(max_errors: 3)
iex> options.max_errors
3

iex> Gumbo.Options.new(bogus: 1)
{:error, {:invalid_option, :bogus, 1}}

to_nif(options)

@spec to_nif(t()) :: map()

Converts the struct to the map the NIF decodes.

Every field is present, so the NIF can decode strictly.

Examples

iex> Gumbo.Options.to_nif(Gumbo.Options.new()) |> Map.keys() |> Enum.sort()
[:dirty, :fragment_context, :fragment_namespace, :max_attributes, :max_errors, :max_tree_depth, :stop_on_first_error, :tab_stop]