Skip to content

Commit

Permalink
Refs: #171; Typespecs for link_parser/inline [amend-me]
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Jul 22, 2018
1 parent a587ae9 commit 5ad3c86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
23 changes: 21 additions & 2 deletions lib/earmark/helpers/link_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ defmodule Earmark.Helpers.LinkParser do

use Earmark.Types

alias Earmark.Message

import Earmark.Helpers.LeexHelpers, only: [tokenize: 2]
import Earmark.Helpers.YeccHelpers, only: [parse!: 2]
import Earmark.Helpers.StringHelpers, only: [behead: 2]

@typep beheader_t :: String.t | pair( non_neg_integer )
@typep beheader_t :: String.t | pair( non_neg_integer )
@typep parsed_tuple :: maybe(triplet(String.t) | {String.t, String.t, String.t, Message.ts})
@typep result_tuple :: maybe({String.t, String.t, String.t, String.t} | {String.t, String.t, String.t, String.t, Message.ts})
@typep link_with_title :: {list(String.t), list(String.t), String.t, Message.ts}
@typep nil_tagged_parse :: { list(String.t), list(String.t), nil }
@typep stack :: list(atom)


# Hopfully this will go away in v1.3
# **********************************
Expand All @@ -30,6 +38,7 @@ defmodule Earmark.Helpers.LinkParser do
# non_quote -> . - quote

@doc false
@spec parse_link( String.t, non_neg_integer ) :: result_tuple
def parse_link( src, lnb ) do
with {link_text, parsed_text} <- parse!(src, lexer: :link_text_lexer, parser: :link_text_parser),
beheaded <- behead(src, to_string(parsed_text)),
Expand All @@ -38,10 +47,13 @@ defmodule Earmark.Helpers.LinkParser do
end
end

@spec p_url( tokens, non_neg_integer ) :: nil_tagged_parse
defp p_url([{:open_paren, _}|ts], lnb), do: url(ts, {[], [], nil}, [:close_paren], lnb)
defp p_url(_, _), do: nil


@spec url( tokens, nil_tagged_parse, stack, non_neg_integer ) :: nil_tagged_parse
defp url(tokens, result, needed, lnb)
# push one level
defp url([{:open_paren, text}|ts], result, needed, lnb), do: url(ts, add(result, text), [:close_paren|needed], lnb)
# pop last level
Expand All @@ -64,6 +76,7 @@ defmodule Earmark.Helpers.LinkParser do
# That is not good, actually this is not a legal url part of a link
defp url(_, _, _, _), do: nil

@spec bail_out_to_title( tokens, {list(String.t), list(String.t), nil}, non_neg_integer) :: maybe(link_with_title)
defp bail_out_to_title(ts, result, lnb) do
with remaining_text <- ts |> Enum.map(&text_of_token/1) |> Enum.join("") do
case title(remaining_text, lnb) do
Expand All @@ -73,6 +86,7 @@ defmodule Earmark.Helpers.LinkParser do
end
end

@spec text_of_token( token ) :: String.t
defp text_of_token(token)
defp text_of_token({:escaped, text}), do: "\\#{text}"
defp text_of_token({_, text}), do: text
Expand All @@ -87,17 +101,20 @@ defmodule Earmark.Helpers.LinkParser do
end
end

@spec deprecations( String.t, non_neg_integer ) :: Message.ts
defp deprecations(string, lnb) do
with stripped <- String.trim(string),
opening <- String.first(stripped),
closing <- String.last(stripped), do: _deprecations(opening, closing, lnb)
end

@spec _deprecations( String.t, String.t, non_neg_integer ) :: Message.ts
defp _deprecations(opening, closing, _lnb) when opening == closing, do: []
defp _deprecations(_opening, _closing, lnb) do
[ {:warning, lnb, "deprecated, mismatching quotes will not be parsed as matching in v1.3"} ]
end

@spec make_result( parsed_tuple, String.t, String.t ) :: result_tuple
defp make_result(nil, _, _), do: nil
defp make_result({parsed, url, title}, text, img), do: make_result({parsed, url, title, []}, text, img)
defp make_result({parsed, url, title, messages}, link_text, "!" <> _) do
Expand All @@ -107,9 +124,11 @@ defmodule Earmark.Helpers.LinkParser do
{ "[#{link_text}](#{list_to_text(parsed)})", link_text, list_to_text(url), title, messages }
end

@spec add( nil_tagged_parse, String.t ) :: nil_tagged_parse
defp add({parsed_text, url_text, nil}, text), do: {[text|parsed_text], [text|url_text], nil}

defp add_title({parsed_text, url_text, _}, {parsed,inner,messages}), do: {[parsed|parsed_text], url_text, inner, messages}
@spec add_title( nil_tagged_parse, {String.t, String.t, Message.ts} ) :: link_with_title
defp add_title({parsed_text, url_text, nil}, {parsed,inner,messages}), do: {[parsed|parsed_text], url_text, inner, messages}

@spec list_to_text( list ) :: String.t
defp list_to_text(lst), do: lst |> Enum.reverse() |> Enum.join("")
Expand Down
2 changes: 2 additions & 0 deletions lib/earmark/inline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,10 @@
end


@spec is_image?( {String.t, any, any, any} | {String.t, any, any, any, any} ) :: boolean
defp is_image?( {match_text, _, _, _} ), do: String.starts_with?(match_text, "!")
defp is_image?( {match_text, _, _, _, _} ), do: String.starts_with?(match_text, "!")

@trailing_newlines ~r{\n*\z}

defp update_lnb(data = {_, _, %{value: []}, _}), do: data
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"credo": {:hex, :credo, "0.8.10", "261862bb7363247762e1063713bb85df2bbd84af8d8610d1272cd9c1943bba63", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []},
"credo": {:hex, :credo, "0.8.10", "261862bb7363247762e1063713bb85df2bbd84af8d8610d1272cd9c1943bba63", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}]},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], []},
"floki": {:hex, :floki, "0.14.0", "91a6be57349e10a63cf52d7890479a19012cef9185fa93c305d4fe42e6a50dee", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, optional: false]}]},
"meck": {:hex, :meck, "0.8.4", "59ca1cd971372aa223138efcf9b29475bde299e1953046a0c727184790ab1520", [:make, :rebar], []},
"mochiweb": {:hex, :mochiweb, "2.15.0", "e1daac474df07651e5d17cc1e642c4069c7850dc4508d3db7263a0651330aacc", [:rebar3], []},
Expand Down

0 comments on commit 5ad3c86

Please sign in to comment.