Kuddle.Path (Kuddle v0.2.1) View Source
Utility module for looking up nodes in a document.
Usage:
nodes = Kuddle.select(document, path)
[{:node, "node", attrs, children}] = Kuddle.select(document, ["node"])
Link to this section Summary
Types
An attribute key (i.e. %Value{}) can be anything, normally it will be an id or string though
An attribute value can be anything
A Kuddle document is a list of nodes, nothing fancy.
A single node in a document
In addition to the attribute_path, node attributes can also use shorthands for
{:attr, key, value}
and {:value, value}
, as {key, value}
and value
respectively.
Node names are strings
A path is a list of selectors that should be used when matching against the document.
Any single path selector
Functions
Select nodes from the given kuddle document, see the path type for the supported selectors
Link to this section Types
Specs
attr_key() :: any()
An attribute key (i.e. %Value{}) can be anything, normally it will be an id or string though
Specs
attr_value() :: any()
An attribute value can be anything
Specs
attribute_path() :: {:attr, attr_key()} | {:attr, attr_key(), attr_value()} | {:value, attr_value()}
Specs
document() :: Kuddle.Decoder.document()
A Kuddle document is a list of nodes, nothing fancy.
Specs
document_node() :: Kuddle.Decoder.document_node()
A single node in a document
Specs
node_attributes() :: [attribute_path() | {any(), any()} | any()]
In addition to the attribute_path, node attributes can also use shorthands for
{:attr, key, value}
and {:value, value}
, as {key, value}
and value
respectively.
Specs
node_name() :: String.t()
Node names are strings
Specs
path() :: [selector()]
A path is a list of selectors that should be used when matching against the document.
It allows different fragments which can be used to match different properties of the node.
Fragments:
node_name
- the node name can be passed as a plain string in the path to select a node based on its nameExample:
[%Kuddle.Node{name: "node"}] = Kuddle.select(document, ["node"]) [] = Kuddle.select(document, ["doesnt_exist"])
{:attr, key}
- a node with an attribute key can be looked up as well, this will ignore thevalue and only look for key value pairs with the key
Example:
[%Kuddle.Node{attributes: [{%{value: "id"}, _value}]}] = Kuddle.select(document, [{:attr, "id"}]) [] = Kuddle.select(document, [{:attr, "cid"}])
{:attr, key, value}
- an attribute of key and value can be looked up as wellExample:
[%Kuddle.Node{attributes: [{%{value: "id"}, %{value: "egg"}}]}] = Kuddle.select(document, [{:attr, "id", "egg"}]) [] = Kuddle.select(document, [{:attr, "cid", "8847"}])
{:value, value}
- for nodes with normal values, the loose value can be looked up as wellExample:
[%Kuddle.Node{attributes: [%{value: 1}]}] = Kuddle.select(document, [{:value, 1}]) [] = Kuddle.select(document, [{:value, 2}])
{:node, node_name}
- equivalent to just providing thenode_name
Example:
[%Kuddle.Node{name: "node"}] = Kuddle.select(document, [{:node, "node"}]) [] = Kuddle.select(document, [{:node, "doesnt_exist"}])
{:node, node_name, attrs}
- lookup a node with attributesExample:
[%Kuddle.Node{name: "node", attributes: [1]}] = Kuddle.select(document, [{:node, "node", [1]}]) [%Kuddle.Node{name: "node", attributes: [1]}] = Kuddle.select(document, [{:node, "node", [{:value, 1}]}]) [%Kuddle.Node{name: "node2", attributes: [{%{value: "id"}, _value}]}] = Kuddle.select(document, [{:node, "node2", [{:attr, "id"}]}]) [%Kuddle.Node{name: "node3", attributes: [{%{value: "id"}, %{value: "bacon"}}]}] = Kuddle.select(document, [{:node, "node3", [{:attr, "id", "bacon"}]}]) [%Kuddle.Node{name: "node3", attributes: [{%{value: "id"}, %{value: "bacon"}}]}] = Kuddle.select(document, [{:node, "node3", [{"id", "bacon"}]}]) [] = Kuddle.select(document, [{:node, "node3", [{"id", "fries"}]}])
Specs
selector() :: node_name() | attribute_path() | {:node, node_name()} | {:node, node_name(), node_attributes()}
Any single path selector
Link to this section Functions
Specs
Select nodes from the given kuddle document, see the path type for the supported selectors
Args:
document
- the document to lookup, or nilpath
- the selectors to use when looking up the nodesacc
- the current accumulator, defaults to an empty list