From 34f15bd644c49ef149706933520450ad6527561b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20Caba=C3=A7o?= Date: Tue, 7 May 2024 00:00:27 +0100 Subject: [PATCH 1/2] fix specs --- lib/francis_htmx.ex | 46 ++++++++++++++++++++++----------------------- mix.exs | 2 +- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/lib/francis_htmx.ex b/lib/francis_htmx.ex index ef506d4..bd604c0 100644 --- a/lib/francis_htmx.ex +++ b/lib/francis_htmx.ex @@ -39,10 +39,24 @@ defmodule FrancisHtmx do @doc """ Renders htmx content by loading htmx.js and rendering binary content. """ + @spec htmx((Plug.Conn.t() -> binary()), Keyword.t()) :: Macro.t() defmacro htmx(content, opts \\ []) do + title = Keyword.get(opts, :title, "") + quote location: :keep do get("/", fn conn -> - root(unquote(content).(conn), unquote(opts)) + """ + + + + #{unquote(title)} + + + + #{unquote(content).(conn)} + + + """ end) end end @@ -52,35 +66,19 @@ defmodule FrancisHtmx do Requires a variable named "assigns" to exist and be set to a map. """ + @spec sigil_E(String.t(), Keyword.t()) :: Macro.t() defmacro sigil_E(content, _opts \\ []) do unless Macro.Env.has_var?(__CALLER__, {:assigns, nil}) do raise "~E requires a variable named \"assigns\" to exist and be set to a map" end quote location: :keep do - unquote(content) - |> EEx.eval_string([assigns: var!(assigns)], engine: Phoenix.HTML.Engine) - |> then(fn {:safe, content} -> Enum.join(content) end) - end - end + content = + EEx.eval_string(unquote(content), [assigns: var!(assigns)], engine: Phoenix.HTML.Engine) - @doc """ - Renders the root content with htmx.js loaded required for the htmx/1 macro. - """ - def root(content, opts) when is_binary(content) do - title = Keyword.get(opts, :title, "") - - """ - - - - #{title} - - - - #{content} - - - """ + content + |> Phoenix.HTML.html_escape() + |> Phoenix.HTML.safe_to_string() + end end end diff --git a/mix.exs b/mix.exs index 04fd192..9d0cc37 100644 --- a/mix.exs +++ b/mix.exs @@ -1,6 +1,6 @@ defmodule FrancisHtmx.MixProject do use Mix.Project - @version "0.1.0" + @version "0.1.1" def project do [ name: "Francis HTMX", From 6723de7e514e955c4a77165c82f0d37ced0edd51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20Caba=C3=A7o?= Date: Tue, 7 May 2024 00:20:02 +0100 Subject: [PATCH 2/2] * add head configuration so users can add extra script tags --- lib/francis_htmx.ex | 5 ++++- test/francis_htmx_test.exs | 30 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/francis_htmx.ex b/lib/francis_htmx.ex index bd604c0..5ab245c 100644 --- a/lib/francis_htmx.ex +++ b/lib/francis_htmx.ex @@ -42,6 +42,7 @@ defmodule FrancisHtmx do @spec htmx((Plug.Conn.t() -> binary()), Keyword.t()) :: Macro.t() defmacro htmx(content, opts \\ []) do title = Keyword.get(opts, :title, "") + head = Keyword.get(opts, :head, "") quote location: :keep do get("/", fn conn -> @@ -49,8 +50,10 @@ defmodule FrancisHtmx do - #{unquote(title)} + #{unquote(head)} + + #{unquote(title)} #{unquote(content).(conn)} diff --git a/test/francis_htmx_test.exs b/test/francis_htmx_test.exs index f8e8098..766e91b 100644 --- a/test/francis_htmx_test.exs +++ b/test/francis_htmx_test.exs @@ -9,7 +9,18 @@ defmodule FrancisHtmxTest do assert html |> Floki.find("script") - |> Floki.attribute("src") == ["https://unpkg.com/htmx.org/dist/htmx.js"] + |> Floki.attribute("src") == [ + "https://cdn.tailwindcss.com", + "https://unpkg.com/htmx.org/dist/htmx.js" + ] + + assert html + |> Floki.find("link") + |> Floki.attribute("href") == ["/app.css"] + + assert html + |> Floki.find("title") + |> Floki.text() == "Testing HTMX" assert html |> Floki.find("body") @@ -23,11 +34,18 @@ defmodule FrancisHtmxTestHandler do use Francis import FrancisHtmx - htmx(fn _ -> - assigns = %{title: "test"} + htmx( + fn _ -> + assigns = %{title: "test"} - ~E""" -
<%= @title %>
+ ~E""" +
<%= @title %>
+ """ + end, + title: "Testing HTMX", + head: """ + + """ - end) + ) end