Skip to content

Instantly share code, notes, and snippets.

@stephenhowells
Created December 23, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenhowells/6242296699a3d86f6f3b to your computer and use it in GitHub Desktop.
Save stephenhowells/6242296699a3d86f6f3b to your computer and use it in GitHub Desktop.
Remove widows from blog post titles by adding a non breaking space in Elixir.
defmodule Text do
def widows(some_str) do
title_list = some_str |> String.split(" ") |> Enum.map(&String.capitalize/1)
title_str = Enum.join(title_list, " ")
if Enum.count(title_list) > 2 do
String.replace(title_str, ~r/\s+\S*$/, "&nbsp;" <> List.last(title_list))
else
title_str
end
end
end
IO.inspect Text.widows "hello there world"
# "Hello There&nbsp;World"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment