Parser and matcher for content expressions.
Content expressions define what children a node can contain using a simple grammar:
| Expression | Meaning |
|---|---|
"block+" | One or more from block group |
"inline*" | Zero or more from inline group |
"paragraph" | Exactly one paragraph |
"paragraph+" | One or more paragraphs |
"paragraph*" | Zero or more paragraphs |
"(paragraph | heading)+" | One or more of either type |
Examples
iex> expr = Quillon.Schema.ContentExpr.parse("block+")
iex> Quillon.Schema.ContentExpr.matches?(expr, [:paragraph, :heading], %{block: [:paragraph, :heading, :divider]})
true
iex> expr = Quillon.Schema.ContentExpr.parse("inline*")
iex> Quillon.Schema.ContentExpr.matches?(expr, [], %{inline: [:text]})
true
Summary
Functions
Check if a list of child types matches the content expression.
Parse a content expression string into a structured form.
Types
@type element() :: {:one, atom()} | {:one_or_more, atom()} | {:zero_or_more, atom()} | {:choice, [atom()], quantifier()}
An element in a content expression
@type quantifier() :: :one | :one_or_more | :zero_or_more
Quantifier for repetition
A parsed content expression
Functions
Check if a list of child types matches the content expression.
Examples
iex> expr = Quillon.Schema.ContentExpr.parse("block+")
iex> groups = %{block: [:paragraph, :heading, :divider]}
iex> Quillon.Schema.ContentExpr.matches?(expr, [:paragraph], groups)
true
iex> expr = Quillon.Schema.ContentExpr.parse("block+")
iex> groups = %{block: [:paragraph, :heading, :divider]}
iex> Quillon.Schema.ContentExpr.matches?(expr, [], groups)
false
iex> Quillon.Schema.ContentExpr.matches?(nil, [], %{})
true
iex> Quillon.Schema.ContentExpr.matches?(nil, [:text], %{})
false
Parse a content expression string into a structured form.
Examples
iex> Quillon.Schema.ContentExpr.parse("block+")
{:one_or_more, :block}
iex> Quillon.Schema.ContentExpr.parse("inline*")
{:zero_or_more, :inline}
iex> Quillon.Schema.ContentExpr.parse("paragraph")
{:one, :paragraph}
iex> Quillon.Schema.ContentExpr.parse("(paragraph | heading)+")
{:choice, [:paragraph, :heading], :one_or_more}