ModestEx (ModestEx v2.1.0)

This module exposes features to do pipeable transformations on html strings with CSS selectors, e.g. find(), prepend(), append(), replace() etc.

Credits:

The package implements bindings to Alexander Borisov's Lexbor. All Lexbor related features are implemeted using the Erlang library lexbor_erl.

Example

iex> ModestEx.find("<p><a>Hello</a> World</p>", "p a")
"<a>Hello</a>"

Summary

Functions

Append new html as a child at the end of selected node. Returns updated html string.

Find nodes with a CSS selector. Returns the outer html of each node as a list of strings.

Get all attributes with key. Returns single string or returns list of strings.

Get all attributes with key of selected nodes. Returns single string or returns list of strings.

Get all text. Returns list of strings.

Get text of selected nodes. Returns list of strings.

Insert new html after selected node. Returns updated html string.

Insert new html before selected node. Returns updated html string.

Get position of selected nodes in in relation to its parent. Returns list of positions.

Prepend new html as a child at the beginning of selected node. Returns updated html string.

Pretty print html.

Remove nodes with a CSS selector. Returns updated html string.

Replace selected node with new html Returns updated html string.

Internal use

Serialization scope. Default :body_children.

Serialize any string with valid or broken html. Returns valid html string.

Set value for all attributes with key. Returns single string or returns list of strings.

Set text for all nodes. Returns single string or returns list of strings.

Slice selected set into subset. Returns single string or list of strings.

Internal use

Wrap an HTML structure around each element in the set of matched elements. Returns updated html string.

Types

error()

@type error() :: {:error, String.t()}

input()

@type input() :: String.t() | [String.t()]

success()

@type success() :: String.t() | [String.t()]

Functions

append(bin, selector, new_bin)

@spec append(input(), String.t(), String.t()) :: success() | error()

Append new html as a child at the end of selected node. Returns updated html string.

Examples

iex> ModestEx.append("<div><p>Hello</p></div>", "div", "<p>World</p>")
"<div><p>Hello</p><p>World</p></div>"

find(bin, selector)

@spec find(input(), String.t()) :: success() | error()

Find nodes with a CSS selector. Returns the outer html of each node as a list of strings.

Examples

iex> ModestEx.find("<p><a>Hello</a> World</p>", "p a")
"<a>Hello</a>"

iex> ModestEx.find("<p><span>Hello</span> <span>World</span></p>", "span")
["<span>Hello</span>", "<span>World</span>"]

get_attribute(bin, key)

@spec get_attribute(input(), String.t()) :: success() | error()

Get all attributes with key. Returns single string or returns list of strings.

Examples

iex> ModestEx.get_attribute("<a href=\"https://elixir-lang.org\">Hello</a>", "href")
"https://elixir-lang.org"

get_attribute(bin, selector, key)

@spec get_attribute(input(), String.t(), String.t()) :: success() | error()

Get all attributes with key of selected nodes. Returns single string or returns list of strings.

Examples

iex> ModestEx.get_attribute("<a href=\"https://elixir-lang.org\">Hello</a>", "a", "href")
"https://elixir-lang.org"

get_text(bin)

@spec get_text(input()) :: success() | error()

Get all text. Returns list of strings.

Examples

iex> ModestEx.get_text("<div>Hello World</div>")
"Hello World"

get_text(bin, selector)

@spec get_text(input(), String.t()) :: success() | error()

Get text of selected nodes. Returns list of strings.

Examples

iex> ModestEx.get_text("<div>Hello World</div>", "div")
"Hello World"

insert_after(bin, selector, new_bin)

@spec insert_after(input(), String.t(), String.t()) :: success() | error()

Insert new html after selected node. Returns updated html string.

Examples

iex> ModestEx.insert_after("<div><p>Hello</p></div>", "div p", "<p>World</p>")
"<div><p>Hello</p><p>World</p></div>"

insert_before(bin, selector, new_bin)

@spec insert_before(input(), String.t(), String.t()) :: success() | error()

Insert new html before selected node. Returns updated html string.

Examples

iex> ModestEx.insert_before("<div><p>World</p></div>", "div p", "<p>Hello</p>")
"<div><p>Hello</p><p>World</p></div>"

position(bin, selector)

@spec position(input(), String.t()) :: success() | error()

Get position of selected nodes in in relation to its parent. Returns list of positions.

Examples

iex> ModestEx.position("<p>Hello</p><div></div><p>World</p>", "p")
[1, 3]

prepend(bin, selector, new_bin)

@spec prepend(input(), String.t(), String.t()) :: success() | error()

Prepend new html as a child at the beginning of selected node. Returns updated html string.

Examples

iex> ModestEx.prepend("<div><p>World</p></div>", "div", "<p>Hello</p>")
"<div><p>Hello</p><p>World</p></div>"

pretty_print(bin)

@spec pretty_print(input()) :: success() | error()

Pretty print html.

Examples

iex> ModestEx.pretty_print("<p>Hello World</p>")
"<p>\n\tHello World\n</p>\n"

remove(bin, selector)

@spec remove(input(), String.t()) :: success() | error()

Remove nodes with a CSS selector. Returns updated html string.

Examples

iex> ModestEx.remove("<div><p>Hello</p>World</div>", "div p")
"<div>World</div>"

replace(bin, selector, new_bin)

@spec replace(input(), String.t(), String.t()) :: success() | error()

Replace selected node with new html Returns updated html string.

Examples

iex> ModestEx.replace("<div><p>Hello</p></div>", "div p", "<p>World</p>")
"<div><p>World</p></div>"

resolve(list)

Internal use

scope()

Serialization scope. Default :body_children.

Examples

:html
  html will be serialized to complete document
  "<html><head></head><body>...</body></html>"
:head
  html will be reduced only to the head fragment
  "<head>...</head>"
:body
  html will be reduced only to the body fragment
  "<body>...</body>"
:body_children
  html will be reduced to the children of the body

Set this in the application config.

config :modest_ex, scope: :html

serialize(bin, scope \\ :html)

@spec serialize(input(), Atom.t()) :: success() | error()

Serialize any string with valid or broken html. Returns valid html string.

Examples

iex> ModestEx.serialize("<div>Hello<span>World")
"<html><head></head><body><div>Hello<span>World</span></div></body></html>"

set_attribute(bin, selector, key, value)

@spec set_attribute(input(), String.t(), String.t(), input()) :: success() | error()

Set value for all attributes with key. Returns single string or returns list of strings.

Examples

iex> ModestEx.set_attribute("<a>Hello</a>", "a", "href", "https://elixir-lang.org")
"<a href=\"https://elixir-lang.org\">Hello</a>"

set_text(bin, selector, text)

@spec set_text(input(), String.t(), input()) :: success() | error()

Set text for all nodes. Returns single string or returns list of strings.

Examples

iex> ModestEx.set_text("<div><p>Hello</p></div>", "div p", "World")
"<div><p>World</p></div>"

slice(bin, selector, start_index, end_index)

@spec slice(input(), String.t(), Integer.t(), Integer.t()) :: success() | error()

Slice selected set into subset. Returns single string or list of strings.

Examples

iex> ModestEx.slice("<h1>Lorem ipsum</h1><p>dolor sit amet</p><ul><li>Coffee</li><li>Tea</li><li>Milk</li></ul><p>Sed ut perspiciatis</p><p>unde omnis iste natus</p>", "body > *", 0, -1)
["<h1>Lorem ipsum</h1>", "<p>dolor sit amet</p>", "<ul><li>Coffee</li><li>Tea</li><li>Milk</li></ul>", "<p>Sed ut perspiciatis</p>", "<p>unde omnis iste natus</p>"]

to_scope(flag)

Internal use

wrap(bin, selector, new_bin)

@spec wrap(input(), String.t(), String.t()) :: success() | error()

Wrap an HTML structure around each element in the set of matched elements. Returns updated html string.

Examples

iex> ModestEx.wrap("<p>Hello</p><p>World</p>", "p", "<div></div>")
"<div><p>Hello</p><p>World</p></div>"