View Source Premailex.HTMLParser behaviour (Premailex v0.3.20)

Module that provide HTML parsing API using an underlying HTML parser library.

Summary

Functions

Searches an HTML tree for the selector.

Filters elements matching the selector from the HTML tree.

Parses a HTML string into an HTML tree.

Extracts text elements from the HTML tree.

Turns an HTML tree into a string.

Types

html_tree()

@type html_tree() :: tuple() | list()

selector()

@type selector() :: binary()

Callbacks

all(html_tree, selector)

@callback all(html_tree(), selector()) :: [html_tree()]

filter(html_tree, selector)

@callback filter(html_tree(), selector()) :: [html_tree()]

parse(binary)

@callback parse(binary()) :: html_tree()

text(html_tree)

@callback text(html_tree()) :: binary()

to_string(html_tree)

@callback to_string(html_tree()) :: binary()

Functions

all(tree, selector)

@spec all(html_tree(), selector()) :: [html_tree()]

Searches an HTML tree for the selector.

Examples

iex> Premailex.HTMLParser.all({"html", [], [{"head", [], []}, {"body", [], [{"h1", [], ["Title"]}]}]}, "h1")
[{"h1", [], ["Title"]}]

filter(tree, selector)

@spec filter(html_tree(), selector()) :: [html_tree()]

Filters elements matching the selector from the HTML tree.

Examples

iex> Premailex.HTMLParser.filter([{"html", [], [{"head", [], []}, {"body", [], [{"h1", [], ["Title"]}]}]}], "h1")
[{"html", [], [{"head", [], []}, {"body", [], []}]}]

parse(html)

@spec parse(binary()) :: html_tree()

Parses a HTML string into an HTML tree.

Examples

iex> Premailex.HTMLParser.parse("<html><head></head><body><h1>Title</h1></body></html>")
{"html", [], [{"head", [], []}, {"body", [], [{"h1", [], ["Title"]}]}]}

text(tree)

@spec text(html_tree()) :: binary()

Extracts text elements from the HTML tree.

Examples

iex> Premailex.HTMLParser.text({"html", [], [{"head", [], []}, {"body", [], [{"h1", [], ["Title"]}]}]})
"Title"

to_string(tree)

@spec to_string(html_tree()) :: binary()

Turns an HTML tree into a string.

Examples

iex> Premailex.HTMLParser.to_string({"html", [], [{"head", [], []}, {"body", [], [{"h1", [], ["Title"]}]}]})
"<html><head></head><body><h1>Title</h1></body></html>"