slime-lang / slime

Minimalistic HTML templates for Elixir, inspired by Slim.
http://slime-lang.com
MIT License
371 stars 57 forks source link

Add support for HEEx. #168

Open tensiondriven opened 2 years ago

tensiondriven commented 2 years ago

This PR adds support to the slime package for HEEx. The main changes to the slim/slime grammar are:

When rendering HEEx, attribute values will be escapes with { } instead of #{ }.

When rendering HEEx, tag names prefixed with a colon (:) will be rendered with a dot (.).

The dot and curlybrace syntax changes are specific to HEEx.

This is accomplished by adding a precompile_heex function, similar to the existing precompile function. It works by passing in a set of delimiters for escaping attribute values:

  @eex_delimiters {"#" <> "{", "}"}
  @heex_delimiters {"{", "}"}

These delimiters are passed down into Slime.Compiler to allow it to return the proper data structures, depending on if HTML or HEEx is requested. Additionally, Slime.Parser.Nodes.HEExNode was added which is returned in Slime.Parser.Transform.

The PEG grammar was updated to add the case where a tag name is prefixed with a colon (:). When precompile (which targets EEx, not HEEx) is called with input that contains a colon, a Slime.TemplateSyntaxError is raised.

Tests were added in test/rendering/heex_test.exs. Testing the HEEx functionality end-to-end was difficult because it would add a dependency on phoenix_slime, so those tests were omitted. This was picked up in my fork of phoenix_slime here: https://github.com/tensiondriven/phoenix_slime/blob/master/test/phoenix_slime_heex_test.exs

I also set up a test Phoenix app which pulls in the new versions of Slime and PhoenixSlime here: https://github.com/tensiondriven/phoenix_slime_test

I expect there are still some issues hiding in here somewhere, so please don't merge until enough review has been done. For now, I'm submitting the PR to get the review process going. The test app (linked above) is working and contains as many cases as I could think to test, but I'm sure there are more that are lurking.

Co-Authored-By: Topher Hunt hunt.topher@gmail.com

doomspork commented 1 year ago

@cmcaine @tensiondriven apologizes for being absent, some unfortunate events took me away from open source for awhile but I'm getting back into the swing of things. I'd love to move forward with this PR, how can I help?