Skip to content

Commit

Permalink
Refs: #171; Typespecs for Earmark.Helpers.{HtmlHelpers,LeexHelpers}
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Apr 4, 2018
1 parent d40e81c commit 05efb42
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/earmark/helpers/attr_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Earmark.Helpers.AttrParser do

@typep errorlist :: list(String.t)

@spec parse_attrs(Options.t, String.t, non_neg_integer) :: {Options.t, map()}
@spec parse_attrs(Options.t, String.t, non_neg_integer) :: {Earmark.Message.container_type, map()}
def parse_attrs(context, attrs, lnb) do
{ attrs, errors } = _parse_attrs(%{}, attrs, [], lnb)
{ add_errors(context, errors, lnb), attrs }
Expand Down
25 changes: 14 additions & 11 deletions lib/earmark/helpers/html_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
defmodule Earmark.Helpers.HtmlHelpers do

use Earmark.Types

alias Earmark.Context

import Earmark.Helpers.AttrParser

@simple_tag ~r{^<(.*?)\s*>}

@doc false

@spec augment_tag_with_ial( Context.t, String.t, String.t, non_neg_integer ) :: maybe({Context.t, String.t})
def augment_tag_with_ial(context, tag, ial, lnb) do
case Regex.run( @simple_tag, tag) do
nil -> nil
Expand All @@ -19,37 +24,35 @@ defmodule Earmark.Helpers.HtmlHelpers do
# add attributes to the outer tag in a block #
##############################################

@doc false
def add_attrs!(context, text, attrs_as_string_or_map, default_attrs, lnb ) do
with {context, {text, _errors}} <- add_attrs(context, text, attrs_as_string_or_map, default_attrs, lnb) do
{context, text}
end
end

defp add_attrs(context, text, attrs_as_string_or_map, default_attrs, lnb )
@typep container_t :: Earmark.Message.container_type
@spec add_attrs( container_t, String.t, maybe(String.t|map), list(tuple), non_neg_integer ) :: {container_t, String.t}
def add_attrs(context, text, attrs_as_string_or_map, default_attrs, lnb )

defp add_attrs(context, text, nil, [], _lnb), do: {context, text}
def add_attrs(context, text, nil, [], _lnb), do: {context, text}

defp add_attrs(context, text, nil, default, lnb), do: add_attrs(context, text, %{}, default, lnb)
def add_attrs(context, text, nil, default, lnb), do: add_attrs(context, text, %{}, default, lnb)

defp add_attrs(context, text, attrs, default, lnb) when is_binary(attrs) do
def add_attrs(context, text, attrs, default, lnb) when is_binary(attrs) do
{context1, attrs} = parse_attrs( context, attrs, lnb )
add_attrs(context1, text, attrs, default, lnb)
end

defp add_attrs(context, text, attrs, default, _lnb) do
def add_attrs(context, text, attrs, default, _lnb) do
{context,
default
|> Enum.into(attrs)
|> attrs_to_string()
|> add_to(text)}
end

@spec attrs_to_string( map ) :: String.t
defp attrs_to_string(attrs) do
(for { name, value } <- attrs, do: ~s/#{name}="#{Enum.join(value, " ")}"/)
|> Enum.join(" ")
end

@spec add_to( String.t, String.t ) :: String.t
defp add_to(attrs, text) do
attrs = if attrs == "", do: "", else: " #{attrs}"
String.replace(text, ~r{\s?/?>}, "#{attrs}\\0", global: false)
Expand Down
3 changes: 3 additions & 0 deletions lib/earmark/helpers/leex_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Earmark.Helpers.LeexHelpers do
Allows to lex an Elixir string with a leex lexer and returns
the tokens as needed for a yecc parser.
"""
@spec lex( String.t, Keyword.t(atom) ) :: Keyword.t(list)
def lex text, with: lexer do
case text
|> String.to_charlist()
Expand All @@ -11,6 +12,7 @@ defmodule Earmark.Helpers.LeexHelpers do
end
end

@spec tokenize( String.t, Keyword.t(atom) ) :: Keyword.t(String.t)
def tokenize line, with: lexer do
{:ok, tokens, _} =
line
Expand All @@ -20,6 +22,7 @@ defmodule Earmark.Helpers.LeexHelpers do
|> Enum.reverse()
end

@spec elixirize_tokens( Keyword.t(list), Keyword.t(String.t) ) :: Keyword.t(String.t)
defp elixirize_tokens(tokens, rest)
defp elixirize_tokens([], result), do: result
defp elixirize_tokens([{token, _, text}|rest], result), do: elixirize_tokens(rest, [{token,to_string(text)}|result])
Expand Down
22 changes: 11 additions & 11 deletions lib/earmark/html_renderer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Earmark.HtmlRenderer do
#############
defp render_block(%Block.Para{lnb: lnb, lines: lines, attrs: attrs}, context) do
lines = convert(lines, lnb, context)
add_attrs!(lines, "<p>#{lines.value}</p>\n", attrs, [], lnb)
add_attrs(lines, "<p>#{lines.value}</p>\n", attrs, [], lnb)
end

########
Expand All @@ -45,15 +45,15 @@ defmodule Earmark.HtmlRenderer do
# Ruler #
#########
defp render_block(%Block.Ruler{lnb: lnb, type: "-", attrs: attrs}, context) do
add_attrs!(context, "<hr/>\n", attrs, [{"class", ["thin"]}], lnb)
add_attrs(context, "<hr/>\n", attrs, [{"class", ["thin"]}], lnb)
end

defp render_block(%Block.Ruler{lnb: lnb, type: "_", attrs: attrs}, context) do
add_attrs!(context, "<hr/>\n", attrs, [{"class", ["medium"]}], lnb)
add_attrs(context, "<hr/>\n", attrs, [{"class", ["medium"]}], lnb)
end

defp render_block(%Block.Ruler{lnb: lnb, type: "*", attrs: attrs}, context) do
add_attrs!(context, "<hr/>\n", attrs, [{"class", ["thick"]}], lnb)
add_attrs(context, "<hr/>\n", attrs, [{"class", ["thick"]}], lnb)
end

###########
Expand All @@ -62,7 +62,7 @@ defmodule Earmark.HtmlRenderer do
defp render_block(%Block.Heading{lnb: lnb, level: level, content: content, attrs: attrs}, context) do
converted = convert(content, lnb, context)
html = "<h#{level}>#{converted.value}</h#{level}>\n"
add_attrs!(converted, html, attrs, [], lnb)
add_attrs(converted, html, attrs, [], lnb)
end

##############
Expand All @@ -72,7 +72,7 @@ defmodule Earmark.HtmlRenderer do
defp render_block(%Block.BlockQuote{lnb: lnb, blocks: blocks, attrs: attrs}, context) do
{context1, body} = render(blocks, context)
html = "<blockquote>#{body}</blockquote>\n"
add_attrs!(context1, html, attrs, [], lnb)
add_attrs(context1, html, attrs, [], lnb)
end

#########
Expand All @@ -81,7 +81,7 @@ defmodule Earmark.HtmlRenderer do

defp render_block(%Block.Table{lnb: lnb, header: header, rows: rows, alignments: aligns, attrs: attrs}, context) do
cols = for _align <- aligns, do: "<col>\n"
{context1, html} = add_attrs!(context, "<table>\n", attrs, [], lnb)
{context1, html} = add_attrs(context, "<table>\n", attrs, [], lnb)
html = [ html , "<colgroup>\n", cols, "</colgroup>\n" ]
context2 = set_value( context1, html )

Expand All @@ -106,7 +106,7 @@ defmodule Earmark.HtmlRenderer do
tag = ~s[<pre><code#{class}>]
lines = options.render_code.(block)
html = ~s[#{tag}#{lines}</code></pre>\n]
add_attrs!(context, html, attrs, [], lnb)
add_attrs(context, html, attrs, [], lnb)
end

#########
Expand All @@ -116,7 +116,7 @@ defmodule Earmark.HtmlRenderer do
defp render_block(%Block.List{lnb: lnb, type: type, blocks: items, attrs: attrs, start: start}, context) do
{context1, content} = render(items, context)
html = "<#{type}#{start}>\n#{content}</#{type}>\n"
add_attrs!(context1, html, attrs, [], lnb)
add_attrs(context1, html, attrs, [], lnb)
end

# format a single paragraph list item, and remove the para tags
Expand All @@ -125,14 +125,14 @@ defmodule Earmark.HtmlRenderer do
{context1, content} = render(blocks, context)
content = Regex.replace(~r{</?p>}, content, "")
html = "<li>#{content}</li>\n"
add_attrs!(context1, html, attrs, [], lnb)
add_attrs(context1, html, attrs, [], lnb)
end

# format a spaced list item
defp render_block(%Block.ListItem{lnb: lnb, blocks: blocks, attrs: attrs}, context) do
{context1, content} = render(blocks, context)
html = "<li>#{content}</li>\n"
add_attrs!(context1, html, attrs, [], lnb)
add_attrs(context1, html, attrs, [], lnb)
end

##################
Expand Down

0 comments on commit 05efb42

Please sign in to comment.