This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
temple/lib/temple/parser/nonvoid_elements_aliases.ex
Mitchell Hanberg c965048f40
Better whitespace handling and control (#145)
* Fine tune whitespace

The EEx outut now emits more human-readable and predictable formatting.
This includes proper indenting, at least for each "root" template.

* Internal whitespace control

You can now use a bang version of any nonvoid tag to emit the markup
witout the internal whitespace. This means that there will not be a
newline emitted after the opening tag and before the closing tag.
2021-08-29 17:45:07 -04:00

60 lines
1.3 KiB
Elixir

defmodule Temple.Parser.NonvoidElementsAliases do
@moduledoc false
@behaviour Temple.Parser
defstruct name: nil, attrs: [], children: [], meta: %{}
alias Temple.Parser
@impl Parser
def applicable?({name, _, _}) do
name in Parser.nonvoid_elements_aliases()
end
def applicable?(_), do: false
@impl Parser
def run({name, _, args}) do
name = Parser.nonvoid_elements_lookup()[name]
{do_and_else, args} = Temple.Parser.Utils.split_args(args)
{do_and_else, args} = Temple.Parser.Utils.consolidate_blocks(do_and_else, args)
children = Temple.Parser.parse(do_and_else[:do])
Temple.Ast.new(__MODULE__,
name: to_string(name) |> String.replace_suffix("!", ""),
attrs: args,
children:
Temple.Ast.new(Temple.Parser.ElementList,
children: children,
whitespace: whitespace(to_string(name))
)
)
end
defp whitespace(name) do
if String.ends_with?(name, "!") do
:tight
else
:loose
end
end
defimpl Temple.Generator do
def to_eex(%{name: name, attrs: attrs, children: children}, indent \\ 0) do
[
"#{Parser.Utils.indent(indent)}<",
name,
Temple.Parser.Utils.compile_attrs(attrs),
">",
Temple.Generator.to_eex(children, indent),
"</",
name,
">"
]
end
end
end