Skip to content

Commit

Permalink
Sigil L for safe Slime HTML (#63)
Browse files Browse the repository at this point in the history
* Initial ~l and ~L macros

* Document

* Test

* Escape the octothorpe so it's included in the docs

* Fix bad test

* Interpolation -> Templating in docs and error msg
  • Loading branch information
the-mikedavis authored and Rakoth committed Dec 3, 2018
1 parent 99c7884 commit a92b033
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/phoenix_slime.ex
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
defmodule PhoenixSlime do
@doc """
Provides the `~l` sigil with HTML safe Slime syntax inside source files.
Raises on attempts to use `\#{}`. Use `~L` to allow templating with `\#{}`.
iex> import PhoenixSlime
iex> assigns = %{w: "world"}
iex> ~l"\""
...> p = "hello " <> @w
...> "\""
{:safe, [[["" | "<p>"] | "hello world"] | "</p>"]}
"""
defmacro sigil_l(expr, opts) do
handle_sigil(expr, opts, __CALLER__.line)
end

@doc """
Provides the `~L` sigil with HTML safe Slime syntax inside source files.
iex> import PhoenixSlime
iex> ~L"\""
...> p hello \#{"world"}
...> "\""
{:safe, [[["" | "<p>hello "] | "world" ] | "</p>"]}
"""
defmacro sigil_L(expr, opts) do
handle_sigil(expr, opts, __CALLER__.line)
end

defp handle_sigil({:<<>>, _, [expr]}, [], line) do
expr
|> Slime.Renderer.precompile()
|> EEx.compile_string(engine: Phoenix.HTML.Engine, line: line + 1)
end

defp handle_sigil(_, _, _) do
raise ArgumentError, ~S(Templating is not allowed with #{} in ~l sigil.) <>
~S( Remove the #{}, use = to insert values, or ) <>
~S(use ~L to template with #{}.)
end
end
1 change: 1 addition & 0 deletions test/phoenix_slime_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule PhoenixSlimeTest do
use ExUnit.Case
alias Phoenix.View
doctest PhoenixSlime

defmodule MyApp.PageView do
use Phoenix.View, root: "test/fixtures/templates"
Expand Down

0 comments on commit a92b033

Please sign in to comment.