EarmarkAstDsl v0.1.1 EarmarkAstDsl View Source
EarmarkAstDsl is a toolset to generate EarmarkParser conformant AST Nodes.
Its main purpose is to remove boilerplate code from Earmark and
EarmarkParser tests.
Documentation for EarmarkAstDsl
.
tag
The most general helper is the tag function:
iex(1)> tag("div", "Some content")
{"div", [], ["Some content"]}
Content and attributes can be provided as arrays, ...
iex(2)> tag("p", ~w[Hello World], class: "hidden")
{"p", [{"class", "hidden"}], ["Hello", "World"]}
... or maps:
iex(3)> tag("p", ~w[Hello World], %{class: "hidden"})
{"p", [{"class", "hidden"}], ["Hello", "World"]}
Shortcuts for p
and div
iex(4)> p("A para")
{"p", [], ["A para"]}
iex(5)> div(tag("span", "content"))
{"div", [], [{"span", [], ["content"]}]}
Tables
Tables are probably the raison d'être ot this little lib, as their ast is quite verbose, as we will see here:
iex(6)> table("one cell only") # and look at the output
{"table", [], [
{"tbody", [], [
{"tr", [], [
{"td", [{"style", "text-align: left;"}], ["one cell only"]}
]}
]}
]}
Now if we want a header and have some more data:
iex(7)> table([~w[1-1 1-2], ~w[2-1 2-2]], head: ~w[left right]) # This is quite verbose!
{"table", [], [
{"thead", [], [
{"tr", [], [
{"th", [{"style", "text-align: left;"}], ["left"]},
{"th", [{"style", "text-align: left;"}], ["right"]},
]}
]},
{"tbody", [], [
{"tr", [], [
{"td", [{"style", "text-align: left;"}], ["1-1"]},
{"td", [{"style", "text-align: left;"}], ["1-2"]},
]},
{"tr", [], [
{"td", [{"style", "text-align: left;"}], ["2-1"]},
{"td", [{"style", "text-align: left;"}], ["2-2"]},
]}
]}
]}
And tables can easily be aligned differently in Markdown, which makes some style helpers very useful
iex(8)> table([~w[1-1 1-2], ~w[2-1 2-2]],
...(8)> head: ~w[alpha beta],
...(8)> text_aligns: ~w[right center])
{"table", [], [
{"thead", [], [
{"tr", [], [
{"th", [{"style", "text-align: right;"}], ["alpha"]},
{"th", [{"style", "text-align: center;"}], ["beta"]},
]}
]},
{"tbody", [], [
{"tr", [], [
{"td", [{"style", "text-align: right;"}], ["1-1"]},
{"td", [{"style", "text-align: center;"}], ["1-2"]},
]},
{"tr", [], [
{"td", [{"style", "text-align: right;"}], ["2-1"]},
{"td", [{"style", "text-align: center;"}], ["2-2"]},
]}
]}
]}
Link to this section Summary
Link to this section Types
Link to this section Functions
Specs
div(content_t(), free_atts_t()) :: ast_t()
Specs
p(content_t(), free_atts_t()) :: ast_t()
Specs
table(content_t(), free_atts_t()) :: ast_t()
Specs
tag(maybe(binary()), content_t(), free_atts_t()) :: ast_t()