Packages
Some view helpers for phoenix html( truncate, time_ago_in_words, number_with_delimiter, url_for, current_page? )
Current section
Files
Jump to
Current section
Files
lib/phoenix_html_simplified_helpers/truncate.ex
defmodule Phoenix.HTML.SimplifiedHelpers.Truncate do
def truncate(text, options \\ []) do
len = options[:length] || 30
omi = options[:omission] || "..."
cond do
!String.valid?(text) -> text
String.length(text) < len -> text
true ->
len_with_omi = len - String.length(omi)
stop =
if options[:separator] do
rindex(text, options[:separator], len_with_omi) || len_with_omi
else
len_with_omi
end
"#{String.slice(text, 0, stop)}#{omi}"
end
end
defp rindex(text, str, offset) do
text =
if offset do
String.slice(text, 0, offset)
else
text
end
revesed = text |> String.reverse
matchword = String.reverse(str)
case :binary.match(revesed, matchword) do
{at, strlen} ->
String.length(text) - at - strlen
:nomatch ->
nil
end
end
end