Packages

WebAssembly is a web DSL for Elixir. You create html structure straight using do blocks. Means, you can intermix html-building blocks with full Elixir syntax. DSL output is an iolist, which you can flatten to string, but better use is to just feed it to the socket (via Plug & Cowboy). WebAssem...

Current section

Files

Jump to
webassembly lib tools.ex
Raw

lib/tools.ex

defmodule WebAssembly.Tools do
@moduledoc """
Tools manipulating assembly input & output.
See `WebAssembly.Tools.Input` & `WebAssembly.Tools.Output`.
"""
alias WebAssembly.Types, as: T
defmodule Input do
@doc ~S"""
Turn a keyword list of attributes to an list of attribute framgents
in a format used by html.
Underscores in key names are turned into dash signs.
## Examples
iex> WebAssembly.Tools.Input.htmlize_attrs(class: "light", id: :myid)
["class=\"light\"", " ", "id=\"myid\""]
iex> WebAssembly.Tools.Input.htmlize_attrs(http_equiv: "Content-Type")
["http-equiv=\"Content-Type\""]
"""
@spec htmlize_attrs(T.attrs) :: [String.t]
def htmlize_attrs(attrs) do
Enum.map(attrs, fn {k,v} ->
k = k |> to_string |> String.replace("_", "-")
~s/#{k}="#{v}"/
end)
|> Enum.intersperse(" ")
end
end
defmodule Output do
@doc ~S"""
Flush nested list of tag fragments into a flat string.
Flushing is not needed when you pass tag fragments
directly to Plug. Still it may come handy when testing,
prototyping or using WebAssembly output together with
web views based on templates.
## Examples
iex(1)> use WebAssembly
:ok
iex(2)> doc = builder do: div do: (span "hey!")
["\n<div>", ["\n<span>", "hey!", "</span>"], "</div>"]
iex(3)> WebAssembly.Tools.Output.flush doc
"\n<div>\n<span>hey!</span></div>"
"""
@spec flush(T.out_tag) :: String.t
def flush(chunks) when is_list(chunks) do
chunks |> List.flatten |> Enum.join
end
end
end