Skip to content

Commit

Permalink
Deprecate DefaultTemplate in favor of HybridTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
wmnnd committed Dec 3, 2023
1 parent b17cd20 commit 17d9dff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/keila/mailings/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ defmodule Keila.Mailings.Builder do
alias Keila.Contacts.Contact
alias Swoosh.Email
alias KeilaWeb.Router.Helpers, as: Routes
alias Keila.Templates.{Template, Css, Html, DefaultTemplate, HybridTemplate}
alias Keila.Templates.{Template, Css, Html, HybridTemplate}
import Swoosh.Email
import __MODULE__.Markdown
import __MODULE__.LiquidRenderer

@default_contact %Keila.Contacts.Contact{
id: "c_id",
Expand Down Expand Up @@ -146,7 +146,7 @@ defmodule Keila.Mailings.Builder do
defp put_body(email, campaign = %{settings: %{type: :text}}, assigns) do
body_with_signature =
(campaign.text_body || "") <>
"\n\n-- \n" <> (assigns["signature"] || DefaultTemplate.signature())
"\n\n-- \n" <> (assigns["signature"] || HybridTemplate.signature())

case render_liquid(body_with_signature, assigns) do
{:ok, text_body} ->
Expand Down Expand Up @@ -222,7 +222,7 @@ defmodule Keila.Mailings.Builder do
end

defp put_signature_content(email, assigns) do
signature_content = assigns["signature"] || DefaultTemplate.signature()
signature_content = assigns["signature"] || HybridTemplate.signature()

with {:ok, signature_content_text} <- render_liquid(signature_content, assigns),
{:ok, signature_content_html, _} <- Earmark.as_html(signature_content_text) do
Expand Down
20 changes: 20 additions & 0 deletions lib/keila/mailings/builder/liquid_renderer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,24 @@ defmodule Keila.Mailings.Builder.LiquidRenderer do
defp template_error_to_string(%{line: {line, _}, reason: reason}) do
"Parsing error in line #{line}: #{reason}"
end

@doc """
Parse and render a string as a liquid template and then transform from
Markdown to HTML.
"""
@spec render_liquid_and_markdown(input :: String.t(), assigns :: map()) ::
{:ok, html :: String.t()} | {:error, String.t()}
def render_liquid_and_markdown(input, assigns) do
with {:ok, markdown} <- render_liquid(input, assigns),
{:ok, html} <- render_markdown(markdown) do
{:ok, markdown, html}
end
end

defp render_markdown(markdown) do
case Earmark.as_html(markdown) do
{:ok, html, _} -> {:ok, html}
{:error, _, _} -> {:error, "Error processing Markdown"}
end
end
end
6 changes: 6 additions & 0 deletions lib/keila/templates/default_template.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Keila.Templates.DefaultTemplate do
@doc """
DEPRECATED. Use `Keila.Templates.HybridTemplate` instead.
Convenience module providing defaults for html and styles, as well as a style
template configuration.
"""
Expand Down Expand Up @@ -98,21 +100,25 @@ defmodule Keila.Templates.DefaultTemplate do
Powered by [Keila - OpenSource Newsletters](https://www.keila.io/)
"""

@deprecated "Use HybridTemplate instead"
@spec styles() :: Keila.Templates.Css.t()
def styles() do
@styles
end

@deprecated "Use HybridTemplate instead"
@spec html_template() :: %Solid.Template{}
def html_template() do
@html_body
end

@deprecated "Use HybridTemplate instead"
@spec style_template() :: Keila.Templates.StyleTemplate.t()
def style_template() do
@style_template
end

@deprecated "Use HybridTemplate instead"
@spec signature() :: String.t()
def signature() do
@signature
Expand Down

0 comments on commit 17d9dff

Please sign in to comment.