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.

parse!(html) deprecated

Handles the ~HTML sigil to create an EasyHTML struct.

Extracts text.

Returns HTML string for the given EasyHTML struct.

to_string(struct) deprecated

Builds an Elixir tree data structure representing HTML data.

Functions

Link to this function

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>"
Link to this function

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>]"
This function is deprecated. Use from_document/1 or from_fragment/1 instead.
Link to this macro

sigil_HTML(string, modifiers)

(macro)

Handles the ~HTML sigil to create an EasyHTML struct.

Examples

~HTML[<p>Hello, <em>World</em>!</p>]

Extracts text.

Examples

iex> EasyHTML.text(~HTML[<p>Hello, <em>World</em>!</p>])
"Hello, World!"
Link to this function

to_html(struct, options \\ [])

Returns HTML string for the given EasyHTML struct.

  • :skip_whitespace_nodes - when true, ignores text nodes that consist entirely of whitespace, usually whitespace between tags. Defaults to true.

Examples

iex> EasyHTML.to_html(~HTML[<p>Hello, <em>World</em>!</p>])
"<p>Hello, <em>World</em>!</p>"
Link to this function

to_string(struct)

This function is deprecated. Use text/1 instead.
Link to this function

to_tree(struct, options \\ [])

Builds an Elixir tree data structure representing HTML data.

Options

  • :sort_attributes - when true, attributes lists are sorted alphabetically by name. Defaults to false.

  • :skip_whitespace_nodes - when true, ignores text nodes that consist entirely of whitespace, usually whitespace between tags. Defaults to true.

Examples

iex> EasyHTML.to_tree(~HTML[<p>Hello, <em>World</em>!</p>])
[{"p", [], ["Hello, ", {"em", [], ["World"]}, "!"]}]