2020-01-22 01:14:42 +00:00
|
|
|
defmodule Mobilizon.Service.Metadata.Utils do
|
|
|
|
@moduledoc """
|
|
|
|
Tools to convert tags to string.
|
|
|
|
"""
|
|
|
|
|
2021-06-22 11:15:21 +00:00
|
|
|
alias Mobilizon.Service.{Address, DateTime}
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Service.Formatter.HTML, as: HTMLFormatter
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Phoenix.HTML
|
2021-07-27 17:47:54 +00:00
|
|
|
import Mobilizon.Web.Gettext, only: [gettext: 1]
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
@slice_limit 200
|
|
|
|
|
2020-11-17 14:45:08 +00:00
|
|
|
@doc """
|
|
|
|
Converts list of tags, containing either `t:Phoenix.HTML.safe/0` or strings, to a concatenated string listing the tags
|
|
|
|
"""
|
|
|
|
@spec stringify_tags(list(Phoenix.HTML.safe() | String.t())) :: String.t()
|
2020-01-22 01:14:42 +00:00
|
|
|
def stringify_tags(tags), do: Enum.reduce(tags, "", &stringify_tag/2)
|
|
|
|
|
2020-11-17 14:45:08 +00:00
|
|
|
@doc """
|
|
|
|
Removes the HTML tags from a text
|
|
|
|
"""
|
2020-07-09 15:24:28 +00:00
|
|
|
@spec strip_tags(String.t()) :: String.t()
|
2020-11-17 14:45:08 +00:00
|
|
|
def strip_tags(text), do: HTMLFormatter.strip_tags_and_insert_spaces(text)
|
2020-07-09 15:24:28 +00:00
|
|
|
|
2020-11-17 14:45:08 +00:00
|
|
|
@doc """
|
|
|
|
Processes a text and limits it.
|
|
|
|
|
|
|
|
* Removes the HTML tags from a text
|
|
|
|
* Slices it to a limit and add an ellipsis character
|
|
|
|
* Returns a default description if text is empty
|
|
|
|
"""
|
2021-11-15 11:11:29 +00:00
|
|
|
@spec process_description(String.t(), String.t(), integer() | nil) :: String.t()
|
2020-07-09 15:24:28 +00:00
|
|
|
def process_description(description, locale \\ "en", limit \\ @slice_limit)
|
|
|
|
def process_description(nil, locale, limit), do: process_description("", locale, limit)
|
|
|
|
|
|
|
|
def process_description("", locale, _limit) do
|
2020-11-17 14:45:08 +00:00
|
|
|
default_description(locale)
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_description(description, _locale, limit) do
|
|
|
|
description
|
2020-11-17 14:45:08 +00:00
|
|
|
|> HTMLFormatter.strip_tags_and_insert_spaces()
|
|
|
|
|> String.trim()
|
|
|
|
|> maybe_slice(limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the default description for a text
|
|
|
|
"""
|
|
|
|
@spec default_description(String.t()) :: String.t()
|
|
|
|
def default_description(locale \\ "en") do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
gettext("The event organizer didn't add any description.")
|
|
|
|
end
|
|
|
|
|
2021-06-22 11:15:21 +00:00
|
|
|
defdelegate datetime_to_string(datetime, locale \\ "en", format \\ :medium), to: DateTime
|
|
|
|
defdelegate render_address(address), to: Address
|
|
|
|
|
2021-11-15 11:11:29 +00:00
|
|
|
defp maybe_slice(description, nil), do: description
|
|
|
|
|
2020-11-17 14:45:08 +00:00
|
|
|
defp maybe_slice(description, limit) do
|
|
|
|
if String.length(description) > limit do
|
|
|
|
description
|
|
|
|
|> String.slice(0..limit)
|
|
|
|
|> String.trim()
|
|
|
|
|> (&"#{&1}…").()
|
|
|
|
else
|
|
|
|
description
|
|
|
|
end
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
2020-11-17 14:45:08 +00:00
|
|
|
|
|
|
|
@spec stringify_tag(Phoenix.HTML.safe(), String.t()) :: String.t()
|
|
|
|
defp stringify_tag(tag, acc) when is_tuple(tag), do: acc <> HTML.safe_to_string(tag)
|
|
|
|
|
|
|
|
@spec stringify_tag(String.t(), String.t()) :: String.t()
|
|
|
|
defp stringify_tag(tag, acc) when is_binary(tag), do: acc <> tag
|
2020-01-22 01:14:42 +00:00
|
|
|
end
|