Quillon.Schema.ContentExpr (Quillon v0.3.0)

Copy Markdown View Source

Parser and matcher for content expressions.

Content expressions define what children a node can contain using a simple grammar:

ExpressionMeaning
"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

Types

An element in a content expression

Quantifier for repetition

t()

A parsed content expression

Functions

Check if a list of child types matches the content expression.

Parse a content expression string into a structured form.

Types

element()

@type element() ::
  {:one, atom()}
  | {:one_or_more, atom()}
  | {:zero_or_more, atom()}
  | {:choice, [atom()], quantifier()}

An element in a content expression

quantifier()

@type quantifier() :: :one | :one_or_more | :zero_or_more

Quantifier for repetition

t()

@type t() :: {:seq, [element()]} | element()

A parsed content expression

Functions

matches?(element, child_types, groups)

@spec matches?(t() | nil, [atom()], map()) :: boolean()

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(expr)

@spec parse(String.t() | nil) :: t() | nil

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}