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
Functions
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 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 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 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 all text. Returns list of strings.
Examples
iex> ModestEx.get_text("<div>Hello World</div>")
"Hello World"
Get text of selected nodes. Returns list of strings.
Examples
iex> ModestEx.get_text("<div>Hello World</div>", "div")
"Hello World"
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 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>"
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 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 html.
Examples
iex> ModestEx.pretty_print("<p>Hello World</p>")
"<p>\n\tHello World\n</p>\n"
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 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>"
Internal use
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 bodySet this in the application config.
config :modest_ex, scope: :html
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 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 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 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>"]
Internal use
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>"