EasyHTML (easyhtml v0.4.0)
EasyHTML makes working with HTML easy.
Examples
EasyHTML.from_fragment("<p>Hello, <em>world</em>!</p>")
#=> ~HTML[<p>Hello, <em>world</em> !</p>]
doc["em"]
#=> ~HTML[<em>world</em>]
import EasyHTML, only: :sigils
doc = ~HTML[<ul><li>foo</li><li>bar</li></ul>]
Enum.to_list(doc["li"])
#=> [~HTML[<li>foo</li>], ~HTML[<li>bar</li>]]
Summary
Functions
Parses an HTML document.
Parses a segment of an HTML document.
Handles the ~HTML
sigil to create an EasyHTML struct.
Extracts text.
Returns HTML string for the given EasyHTML struct.
Builds an Elixir tree data structure representing HTML data.
Functions
from_document(html)
Parses an HTML document.
Examples
iex> EasyHTML.from_document("<p>Hello, <em>World</em>!</p>") |> EasyHTML.to_html()
"<html><head></head><body><p>Hello, <em>World</em>!</p></body></html>"
from_fragment(html)
Parses a segment of an HTML document.
Examples
iex> EasyHTML.from_fragment("<p>Hello, <em>World</em>!</p>") |> inspect()
"~HTML[<p>Hello, <em>World</em> !</p>]"
parse!(html)
Handles the ~HTML
sigil to create an EasyHTML struct.
Examples
~HTML[<p>Hello, <em>World</em>!</p>]
text(struct)
Extracts text.
Examples
iex> EasyHTML.text(~HTML[<p>Hello, <em>World</em>!</p>])
"Hello, World!"
to_html(struct, options \\ [])
Returns HTML string for the given EasyHTML struct.
:skip_whitespace_nodes
- whentrue
, ignores text nodes that consist entirely of whitespace, usually whitespace between tags. Defaults totrue
.
Examples
iex> EasyHTML.to_html(~HTML[<p>Hello, <em>World</em>!</p>])
"<p>Hello, <em>World</em>!</p>"
to_string(struct)
to_tree(struct, options \\ [])
Builds an Elixir tree data structure representing HTML data.
Options
:sort_attributes
- whentrue
, attributes lists are sorted alphabetically by name. Defaults tofalse
.:skip_whitespace_nodes
- whentrue
, ignores text nodes that consist entirely of whitespace, usually whitespace between tags. Defaults totrue
.
Examples
iex> EasyHTML.to_tree(~HTML[<p>Hello, <em>World</em>!</p>])
[{"p", [], ["Hello, ", {"em", [], ["World"]}, "!"]}]