View Source LiveViewNative.Template.Parser (live_view_native v0.4.0-rc.0)

Floki-compliant parser for LiveView Native template syntax

iex> """
...> <Group>
...>   <Text class="bold">Hello</Text>
...>   <Text>world!</Text>
...> </Group>
...> """
...> |> LiveViewNative.Template.Parser.parse_document()
{:ok, [{"Group", [], [{"Text", [{"class", "bold"}], ["Hello"]}, {"Text", [], ["world!"]}]}]}

You can pass this AST into Floki for querying:

iex> """
...> <Group>
...>   <Text class="bold">Hello</Text>"
...>   <Text>world!</Text>
...> </Group>
...> """
...> |> LiveViewNative.Template.Parser.parse_document!()
...> |> Floki.find("Text.bold")
[{"Text", [{"class", "bold"}], ["Hello"]}]

Floki Integration

Floki support passing parser in by option, this parser is compliant with that API:

iex> """
...> <Group>
...>   <Text class="bold">Hello</Text>"
...>   <Text>world!</Text>
...> </Group>
...> """
...> |> Floki.parse_document!(html_parser: LiveViewNative.Template.Parser)
...> |> Floki.find("Text.bold")
[{"Text", [{"class", "bold"}], ["Hello"]}]

Or you can configure as the default:

config :floki, :html_parser, LiveViewNative.Tempalte.Parser

Summary

Functions

Parses an LVN document from a string.

Parses a LVN Document from a string.

Functions

parse_document(document, args \\ [])

Parses an LVN document from a string.

This is the main function to get a tree from a LVN string.

Options

  • :attributes_as_maps - Change the behaviour of the parser to return the attributes as maps, instead of a list of {"key", "value"}. Default to false.

Examples

iex> LiveViewNative.Template.Parser.parse_document("<Group><Text></Text><Text>Hello</Text></Group>")
{:ok, [{"Group", [], [{"Text", [], []}, {"Text", [], ["Hello"]}]}]}

iex> LiveViewNative.Template.Parser.parse_document(
...>   ~S(<Group><Text></Text><Text class="main">Hello</Text></Group>),
...>   attributes_as_maps: true
...>)
{:ok, [{"Group", %{}, [{"Text", %{}, []}, {"Text", %{"class" => "main"}, ["Hello"]}]}]}

parse_document!(document, args \\ [])

Parses a LVN Document from a string.

Similar to parse_document/1, but raises LiveViewNative.Template.ParseError if there was an error parsing the document.

Example

iex> LiveViewNative.Template.Parser.parse_document!("<Group><Text></Text><Text>hello</Text></Group>")
[{"Group", [], [{"Text", [], []}, {"Text", [], ["hello"]}]}]