liveview-native / live_view_native_stylesheet

MIT License
10 stars 4 forks source link

Change to curly brace interpolation #52

Closed bcardarella closed 7 months ago

bcardarella commented 7 months ago

This is some of the laziest code I've ever written. But it works and doesn't require us to write our own tokenizer, interpolation handlers, and compiler for quoted output

bcardarella commented 7 months ago

@NduatiK any concerns with this implementation? We can replace with a more robust parser in the future but right now this seems OK unless you have a good argument against

bcardarella commented 7 months ago

the alternative to { .. } would be a more pure mustache syntax: {{ .. }} which I am very certain we're guaranteed not to have any syntax conflicts. But I am also fairly certain the two character synxtax will be OK as well

NduatiK commented 7 months ago

@bcardarella , this works, but would benefit from us supporting escaping:

    compiled_rules =
      rules
        # Replace { that is not escaped with slash
        |> String.replace(~r|([^\\]){|, "\\g{1}<%=")
        # Replace } that is not escaped with slash
        |> String.replace(~r|([^\\])}|, "\\g{1}%>")
        # Unescape {
        |> String.replace("\\{", "{")
        # Unescape }
        |> String.replace("\\}", "}")      
        |> EEx.compile_string()
This means that: Example 1 Example 2
"rule-{ number_2 }" "rule-{ text <> \"\\{abc\\}\" }"
"rule-<%= number_2 %>" "rule-<%= text <> \"{abc}\" %>"

We escape with "\{" instead of "{" because for some reason, "{" is equal to "{" in Elixir.


The above might be overkill, but it ensures we have an escape route if necessary eg if someone wants to include curly braces in the placeholder of a textfield

bcardarella commented 7 months ago

I considered adding escaping but I don't have a use case for it. If you can provide one we can add it otherwise I'd prefer not to increase surface area unnecessarily. If a UI's styles needed the { .. } syntax I don't want to go the route of escaping to permit that.

bcardarella commented 7 months ago

:+1: we can always add later if the need arises 😄