<commit_before>defmodule Bamboo.Phoenix do
  @moduledoc """
  Render templates and layouts with Phoenix.

  This module makes it very easy to render layouts and views using Phoenix.
  Pass an atom (e.g. `:welcome_email`) as the template name to render HTML and
  plain text emails. Use a string if you only want to render one type, e.g.
  `"welcome_email.text"` or `"welcome_email.html"`.

  ## Examples

      defmodule Email do
        use Bamboo.Phoenix, view: MyApp.EmailView

        def text_and_html_email_with_layout do
          new_email()
          # You could set just a text layout or just an html layout
          |> put_text_layout
          |> put_html_layout
          # Or you can set a layout for both html and text at the same time
          |> put_layout
          # Pass an atom to render html AND plain text templates
          |> render(:text_and_html_email)
        end

        def text_and_html_email_without_layouts do
          new_email()
          |> render(:text_and_html_email)
        end

        def email_with_assigns(user) do
          new_email()
          # @user will be available in the template
          |> render(:email_with_assigns, user: user)
        end

        def email_with_already_assigned_user(user) do
          new_email()
          # @user will be available in the template
          |> assign(:user, user)
          |> render(:email_with_assigns)
        end

        def html_email do
          new_email
          |> render("html_email.html")
        end

        def text_email do
          new_email
          |> render("text_email.text")
        end
      end
  """

  import Bamboo.Email, only: [put_private: 3]

  defmacro __using__(view: view_module) do
    verify_phoenix_dep
    quote do
      import Bamboo.Email
      import Bamboo.Phoenix, except: [render: 3]
      @email_view_module unquote(view_module)

      @doc """
      Render an Phoenix template and set the body on the email

      Pass an atom as the template name (:welcome_email) to render HTML *and* plain
      text emails. Use a string if you only want to render one type, e.g.
      "welcome_email.text" or "welcome_email.html". Scroll to the top for more examples.
      """
      def render(email, template, assigns \\ []) do
        Bamboo.Phoenix.render_email(@email_view_module, email, template, assigns)
      end
    end
  end
  defmacro __using__(opts) do
    raise ArgumentError, """
    expected Bamboo.Phoenix to have a view set, instead got: #{inspect opts}.

    Please set a view e.g. use Bamboo.Phoenix, view: MyApp.MyView
    """
  end

  defp verify_phoenix_dep do
    unless Code.ensure_loaded?(Phoenix) do
      raise "You tried to use Bamboo.Phoenix, but Phoenix module is not loaded. " <>
      "Please add phoenix to your dependencies."
    end
  end

  @doc """
  Render a Phoenix template and set the body on the email

  Pass an atom as the template name to render HTML *and* plain text emails,
  e.g. `:welcome_email`. Use a string if you only want to render one type, e.g.
  `"welcome_email.text"` or `"welcome_email.html"`. Scroll to the top for more examples.
  """
  def render(_email, _template_name, _assigns) do
    raise "function implemented for documentation only, please call: use Bamboo.Phoenix"
  end

  @doc """
  Sets the layout when rendering HTML templates

  ## Example

      def html_email_layout do
        new_email
        # Will use MyApp.LayoutView with email.html layout when rendering html emails
        |> put_html_layout({MyApp.LayoutView, "email.html"})
      end
  """
  def put_html_layout(email, layout) do
    email |> put_private(:html_layout, layout)
  end

  @doc """
  Sets the layout when rendering plain text templates

  ## Example

      def text_email_layout do
        new_email
        # Will use MyApp.LayoutView with email.text layout when rendering text emails
        |> put_text_layout({MyApp.LayoutView, "email.text"})
      end
  """
  def put_text_layout(email, layout) do
    email |> put_private(:text_layout, layout)
  end

  @doc """
  Sets the layout for rendering plain text and HTML templates

  ## Example

      def text_and_html_email_layout do
        new_email
        # Will use MyApp.LayoutView with the email.html template for html emails
        # Will use MyApp.LayoutView with the text.html template for text emails
        |> put_layout({MyApp.LayoutView, :email})
      end
  """
  def put_layout(email, {layout, template}) do
    email
    |> put_text_layout({layout, to_string(template) <> ".text"})
    |> put_html_layout({layout, to_string(template) <> ".html"})
  end

  @doc """
  Sets an assign for the email. These will be availabe when rendering the email
  """
  def assign(%{assigns: assigns} = email, key, value) do
    %{email | assigns: Map.put(assigns, key, value)}
  end<commit_msg>Update docs in Bamboo.Phoenix (#137)<commit_after>defmodule Bamboo.Phoenix do
  @moduledoc """
  Render templates and layouts with Phoenix.

  This module makes it very easy to render layouts and views using Phoenix.
  Pass an atom (e.g. `:welcome_email`) as the template name to render HTML and
  plain text emails. Use a string if you only want to render one type, e.g.
  `"welcome_email.text"` or `"welcome_email.html"`.

  ## Examples

      defmodule Email do
        use Bamboo.Phoenix, view: MyApp.EmailView

        def text_and_html_email_with_layout do
          new_email()
          # You could set just a text layout or just an html layout
          |> put_text_layout({MyApp.LayoutView, "email.text"})
          |> put_html_layout({MyApp.LayoutView, "email.html"})
          # Or you can set a layout for both html and text at the same time
          |> put_layout({MyApp.LayoutView, :email})
          # Pass an atom to render html AND plain text templates
          |> render(:text_and_html_email)
        end

        def text_and_html_email_without_layouts do
          new_email()
          |> render(:text_and_html_email)
        end

        def email_with_assigns(user) do
          new_email()
          # @user will be available in the template
          |> render(:email_with_assigns, user: user)
        end

        def email_with_already_assigned_user(user) do
          new_email()
          # @user will be available in the template
          |> assign(:user, user)
          |> render(:email_with_assigns)
        end

        def html_email do
          new_email
          |> render("html_email.html")
        end

        def text_email do
          new_email
          |> render("text_email.text")
        end
      end
  """

  import Bamboo.Email, only: [put_private: 3]

  defmacro __using__(view: view_module) do
    verify_phoenix_dep
    quote do
      import Bamboo.Email
      import Bamboo.Phoenix, except: [render: 3]
      @email_view_module unquote(view_module)

      @doc """
      Render an Phoenix template and set the body on the email

      Pass an atom as the template name (:welcome_email) to render HTML *and* plain
      text emails. Use a string if you only want to render one type, e.g.
      "welcome_email.text" or "welcome_email.html". Scroll to the top for more examples.
      """
      def render(email, template, assigns \\ []) do
        Bamboo.Phoenix.render_email(@email_view_module, email, template, assigns)
      end
    end
  end
  defmacro __using__(opts) do
    raise ArgumentError, """
    expected Bamboo.Phoenix to have a view set, instead got: #{inspect opts}.

    Please set a view e.g. use Bamboo.Phoenix, view: MyApp.MyView
    """
  end

  defp verify_phoenix_dep do
    unless Code.ensure_loaded?(Phoenix) do
      raise "You tried to use Bamboo.Phoenix, but Phoenix module is not loaded. " <>
      "Please add phoenix to your dependencies."
    end
  end

  @doc """
  Render a Phoenix template and set the body on the email

  Pass an atom as the template name to render HTML *and* plain text emails,
  e.g. `:welcome_email`. Use a string if you only want to render one type, e.g.
  `"welcome_email.text"` or `"welcome_email.html"`. Scroll to the top for more examples.
  """
  def render(_email, _template_name, _assigns) do
    raise "function implemented for documentation only, please call: use Bamboo.Phoenix"
  end

  @doc """
  Sets the layout when rendering HTML templates

  ## Example

      def html_email_layout do
        new_email
        # Will use MyApp.LayoutView with email.html template when rendering html emails
        |> put_html_layout({MyApp.LayoutView, "email.html"})
      end
  """
  def put_html_layout(email, layout) do
    email |> put_private(:html_layout, layout)
  end

  @doc """
  Sets the layout when rendering plain text templates

  ## Example

      def text_email_layout do
        new_email
        # Will use MyApp.LayoutView with email.text template when rendering text emails
        |> put_text_layout({MyApp.LayoutView, "email.text"})
      end
  """
  def put_text_layout(email, layout) do
    email |> put_private(:text_layout, layout)
  end

  @doc """
  Sets the layout for rendering plain text and HTML templates

  ## Example

      def text_and_html_email_layout do
        new_email
        # Will use MyApp.LayoutView with the email.html template for html emails
        # Will use MyApp.LayoutView with the text.html template for text emails
        |> put_layout({MyApp.LayoutView, :email})
      end
  """
  def put_layout(email, {layout, template}) do
    email
    |> put_text_layout({layout, to_string(template) <> ".text"})
    |> put_html_layout({layout, to_string(template) <> ".html"})
  end

  @doc """
  Sets an assign for the email. These will be availabe when rendering the email
  """
  def assign(%{assigns: assigns} = email, key, value) do
    %{email | assigns: Map.put(assigns, key, value)}
  end
{'fraction_non_alphanumeric': 0.08298458711850566, 'fraction_numerical': 0.0007145044401347351, 'mean_word_length': 2.6478034251675355, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2058906', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2850, 'n_words': 1018}