Skip to content

Commit

Permalink
Refs: #171; Typespecs for Earmark.Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Apr 3, 2018
1 parent bf5a268 commit 3f65b48
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/earmark/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ defmodule Earmark.Helpers do
@doc """
Expand tabs to multiples of 4 columns
"""
@spec expand_tabs( String.t ) :: String.t
def expand_tabs(line) do
Regex.replace(~r{(.*?)\t}, line, &expander/2)
end

@spec expander( any, String.t ) :: String.t
defp expander(_, leader) do
extra = 4 - rem(String.length(leader), 4)
leader <> pad(extra)
Expand All @@ -15,10 +17,12 @@ defmodule Earmark.Helpers do
@doc """
Remove newlines at end of line
"""
@spec remove_line_ending( String.t ) :: String.t
def remove_line_ending(line) do
line |> String.trim_trailing("\n") |> String.trim_trailing("\r")
end

@spec pad( pos_integer ) :: String.t
defp pad(1), do: " "
defp pad(2), do: " "
defp pad(3), do: " "
Expand All @@ -28,6 +32,7 @@ defmodule Earmark.Helpers do
`Regex.replace` with the arguments in the correct order
"""

@spec replace( String.t, Regex.t, String.t, Keyword.t ) :: String.t
def replace(text, regex, replacement, options \\ []) do
Regex.replace(regex, text, replacement, options)
end
Expand All @@ -38,6 +43,7 @@ defmodule Earmark.Helpers do
Percent-escapes a URI, and after that escapes any
`&`, `<`, `>`, `"`, `'`.
"""
@spec encode( String.t ) :: String.t
def encode(html) do
URI.encode(html) |> escape(true)
end
Expand All @@ -48,6 +54,7 @@ defmodule Earmark.Helpers do
convert non-entity ampersands.
"""

@spec escape( String.t, boolean ) :: String.t
def escape(html, encode \\ false)

def escape(html, false), do: _escape(Regex.replace(~r{&(?!#?\w+;)}, html, "&amp;"))
Expand Down

0 comments on commit 3f65b48

Please sign in to comment.