View Source Earmark.AstTools (Earmark v1.4.47)

Tools for AST manipulation

Summary

Functions

Convenience function to access an attribute from an AST node or a list of attributes

Convenience function to access an attribute from an AST node or a list of attributes with a default value.

A helper to merge attributes in their cannonical representation

A convenience function that extracts the original attributes to be merged with new attributes and puts the result into the node again

Wrap a function that can only be called on nodes

Functions

Link to this function

find_att_in_node(node_or_atts, att)

View Source
@spec find_att_in_node(Earmark.ast_node() | Earmark.ast_attributes(), binary()) ::
  any()

Convenience function to access an attribute from an AST node or a list of attributes

iex(4)> find_att_in_node({"a", [{"class", "link"}], [], %{}}, "class")
"link"

iex(5)> find_att_in_node({"a", [{"class", "link"}], [], %{}}, "target")
nil

iex(6)> find_att_in_node([{"class", "link"}], "class")
"link"

iex(7)> find_att_in_node([{"class", "link"}], "target")
nil
Link to this function

find_att_in_node(node_or_atts, att, default)

View Source
@spec find_att_in_node(Earmark.ast_node() | Earmark.ast_attributes(), binary(), any()) ::
  any()

Convenience function to access an attribute from an AST node or a list of attributes with a default value.

iex(8)> find_att_in_node({"a", [{"class", "link"}], [], %{}}, "target", :default)
:default


iex(9)> find_att_in_node([{"class", "link"}], "target", :default)
:default
@spec merge_atts(Earmark.ast_attributes(), map() | Keyword.t()) ::
  Earmark.ast_attributes()

A helper to merge attributes in their cannonical representation

iex(1)> merge_atts([{"href", "url"}], target: "_blank")
[{"href", "url"}, {"target", "_blank"}]

iex(2)> merge_atts([{"href", "url"}, {"target", "nonsense"}], %{"target" => "_blank"})
[{"href", "url"}, {"target", "_blank nonsense"}]

iex(3)>  merge_atts([{"href", "url"}, {"target", "nonsense"}, {"alt", "nowhere"}],
...(3)>              [{"target", "_blank"}, title: "where?"])
[{"alt", "nowhere"}, {"href", "url"}, {"target", "_blank nonsense"}, {"title", "where?"}]
Link to this function

merge_atts_in_node(arg, new_atts)

View Source
@spec merge_atts_in_node(Earmark.ast_node(), map() | Keyword.t()) ::
  Earmark.ast_node()

A convenience function that extracts the original attributes to be merged with new attributes and puts the result into the node again

iex(10)> merge_atts_in_node({"img", [{"src", "there"}, {"alt", "there"}], [], %{some: "meta"}}, alt: "here")
{"img", [{"alt", "here there"}, {"src", "there"}], [], %{some: "meta"}}
@spec node_only_fn((Earmark.ast_node() -> any())) ::
  (Earmark.ast_node() | binary() -> any())

Wrap a function that can only be called on nodes

iex(11)> f = fn {t, _, _, _} -> t end
...(11)> f_ = node_only_fn(f)
...(11)> {f_.({"p", [], [], %{}}), f_.("text")}
{"p", "text"}