2020-01-22 01:14:42 +00:00
|
|
|
defmodule Mobilizon.Service.Metadata.Utils do
|
|
|
|
@moduledoc """
|
|
|
|
Tools to convert tags to string.
|
|
|
|
"""
|
|
|
|
|
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
|
2020-07-09 15:24:28 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
@slice_limit 200
|
|
|
|
|
|
|
|
@spec stringify_tags(Enum.t()) :: String.t()
|
2020-01-22 01:14:42 +00:00
|
|
|
def stringify_tags(tags), do: Enum.reduce(tags, "", &stringify_tag/2)
|
|
|
|
|
|
|
|
defp stringify_tag(tag, acc) when is_tuple(tag), do: acc <> HTML.safe_to_string(tag)
|
|
|
|
defp stringify_tag(tag, acc) when is_binary(tag), do: acc <> tag
|
2020-07-09 15:24:28 +00:00
|
|
|
|
|
|
|
@spec strip_tags(String.t()) :: String.t()
|
|
|
|
def strip_tags(text), do: HTMLFormatter.strip_tags(text)
|
|
|
|
|
|
|
|
@spec process_description(String.t(), String.t(), integer()) :: String.t()
|
|
|
|
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
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
gettext("The event organizer didn't add any description.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_description(description, _locale, limit) do
|
|
|
|
description
|
|
|
|
|> HTMLFormatter.strip_tags()
|
|
|
|
|> String.slice(0..limit)
|
|
|
|
|> (&"#{&1}…").()
|
|
|
|
end
|
2020-01-22 01:14:42 +00:00
|
|
|
end
|