Skip to content

Commit

Permalink
~R is deprecated in favour of ~r (#476)
Browse files Browse the repository at this point in the history
* ~R is deprecated in favor of ~r

* map_join/3 refactor

* readability refactors

* changed with syntax, refactored cond -> if/else
  • Loading branch information
bradhanks authored Jan 18, 2024
1 parent 959d92e commit 8c0c9ae
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 127 deletions.
4 changes: 2 additions & 2 deletions lib/earmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ defmodule Earmark do
`iex` usage.
"""
def version() do
with {:ok, version} = :application.get_key(:earmark, :vsn),
do: to_string(version)
with {:ok, version} <- :application.get_key(:earmark, :vsn),
do: to_string(version)
end

defp _as_ast(lines, options)
Expand Down
5 changes: 2 additions & 3 deletions lib/earmark/cli/implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule Earmark.Cli.Implementation do
end

defp _format_errors(errors) do
"Illegal options #{errors |> Enum.map(fn {option, _} -> option end) |> Enum.join(", ")}"
"Illegal options #{errors |> Enum.map_join(", ", fn {option, _} -> option end)}"
end

defp _maybe_to_html!(output, %{file: nil}), do: Earmark.Internal.as_html!(output)
Expand Down Expand Up @@ -186,8 +186,7 @@ defmodule Earmark.Cli.Implementation do

defp _option_related_help do
@cli_options
|> Enum.map(&_specific_option_help/1)
|> Enum.join("\n")
|> Enum.map_join("\n", &_specific_option_help/1)
end

defp _specific_option_help(option) do
Expand Down
6 changes: 3 additions & 3 deletions lib/earmark_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ defmodule Earmark.Parser do
end

def as_ast(_, options) do
raise ArgumentError, "#{inspect options} not a legal options map or keyword list"
raise ArgumentError, "#{inspect(options)} not a legal options map or keyword list"
end

defp _as_ast(lines, options) do
Expand All @@ -627,8 +627,8 @@ defmodule Earmark.Parser do
`iex` usage.
"""
def version() do
with {:ok, version} = :application.get_key(:earmark_parser, :vsn),
do: to_string(version)
with {:ok, version} <- :application.get_key(:earmark_parser, :vsn),
do: to_string(version)
end
end

Expand Down
37 changes: 24 additions & 13 deletions lib/earmark_parser/helpers/ast_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Earmark.Parser.Helpers.AstHelpers do

@moduledoc false

import Earmark.Parser.Ast.Emitter
Expand All @@ -10,45 +9,57 @@ defmodule Earmark.Parser.Helpers.AstHelpers do
@doc false
def annotate(node, from_block)
def annotate(node, %{annotation: nil}), do: node

def annotate({tag, atts, children, meta}, %{annotation: annotation}),
do: {tag, atts, children, Map.put(meta, :annotation, annotation)}

def annotate({tag, atts, children, meta}, annotation),
do: {tag, atts, children, Map.put(meta, :annotation, annotation)}

@doc false
def attrs_to_string_keys(key_value_pair)

def attrs_to_string_keys({k, vs}) when is_list(vs) do
{to_string(k), Enum.join(vs, " ")}
end

def attrs_to_string_keys({k, vs}) do
{to_string(k),to_string(vs)}
{to_string(k), to_string(vs)}
end

@doc false
def augment_tag_with_ial(tags, ial)
def augment_tag_with_ial([{t, a, c, m}|tags], atts) do
[{t, merge_attrs(a, atts), c, m}|tags]

def augment_tag_with_ial([{t, a, c, m} | tags], atts) do
[{t, merge_attrs(a, atts), c, m} | tags]
end

def augment_tag_with_ial([], _atts) do
[]
[]
end

@doc false
def code_classes(language, prefix) do
classes =
["" | String.split(prefix || "")]
|> Enum.map(fn pfx -> "#{pfx}#{language}" end)
{"class", classes |> Enum.join(" ")}

{"class", classes |> Enum.join(" ")}
end

@doc false
def codespan(text) do
def codespan(text) do
emit("code", text, class: "inline")
end

@doc false
def render_footnote_link(ref, backref, number) do
emit("a", to_string(number), href: "##{ref}", id: backref, class: "footnote", title: "see footnote")
emit("a", to_string(number),
href: "##{ref}",
id: backref,
class: "footnote",
title: "see footnote"
)
end

@doc false
Expand All @@ -73,19 +84,17 @@ defmodule Earmark.Parser.Helpers.AstHelpers do
emit("a", text, href: _encode(url))
end


##############################################
# add attributes to the outer tag in a block #
##############################################


@verbatims ~r<%[\da-f]{2}>i
defp _encode(url) do
url
|> String.split(@verbatims, include_captures: true)
|> Enum.chunk_every(2)
|> Enum.map(&_encode_chunk/1)
|> IO.chardata_to_string
|> IO.chardata_to_string()
end

defp _encode_chunk([encodable, verbatim]), do: [URI.encode(encodable), verbatim]
Expand All @@ -94,6 +103,7 @@ defmodule Earmark.Parser.Helpers.AstHelpers do
@doc false
def merge_attrs(maybe_atts, new_atts)
def merge_attrs(nil, new_atts), do: new_atts

def merge_attrs(atts, new) when is_list(atts) do
atts
|> Enum.into(%{})
Expand All @@ -108,13 +118,14 @@ defmodule Earmark.Parser.Helpers.AstHelpers do
end

defp _value_merger(key, val1, val2)

defp _value_merger(_, val1, val2) when is_list(val1) do
val1 ++ [val2]
end

defp _value_merger(_, val1, val2) do
[val1, val2]
end


end

# SPDX-License-Identifier: Apache-2.0
25 changes: 13 additions & 12 deletions lib/earmark_parser/helpers/leex_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
defmodule Earmark.Parser.Helpers.LeexHelpers do

@moduledoc false
@doc """
Allows to lex an Elixir string with a leex lexer and returns
the tokens as needed for a yecc parser.
"""
def lex text, with: lexer do
def lex(text, with: lexer) do
case text
|> String.to_charlist()
|> lexer.string() do
{:ok, tokens, _} -> tokens
end
|> String.to_charlist()
|> lexer.string() do
{:ok, tokens, _} -> tokens
end
end

def tokenize line, with: lexer do
def tokenize(line, with: lexer) do
{:ok, tokens, _} =
line
|> to_charlist()
|> lexer.string()
elixirize_tokens(tokens,[])
line
|> to_charlist()
|> lexer.string()

elixirize_tokens(tokens, [])
|> Enum.reverse()
end

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])

defp elixirize_tokens([{token, _, text} | rest], result),
do: elixirize_tokens(rest, [{token, to_string(text)} | result])
end

# SPDX-License-Identifier: Apache-2.0
2 changes: 1 addition & 1 deletion lib/earmark_parser/helpers/pure_link_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ defmodule Earmark.Parser.Helpers.PureLinkHelpers do
end
end

defp balance_parens(<<_::utf8,rest::binary>>, trailing_paren_count, non_trailing_count) do
defp balance_parens(<<_::utf8, rest::binary>>, trailing_paren_count, non_trailing_count) do
balance_parens(rest, trailing_paren_count, non_trailing_count)
end
end
16 changes: 10 additions & 6 deletions lib/earmark_parser/line_scanner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ defmodule Earmark.Parser.LineScanner do
when is_boolean(recursive),
do: type_of(line, %Options{}, recursive)

def type_of({line, lnb}, options = %Options{annotations: annotations}, recursive) when is_binary(line) do
def type_of({line, lnb}, options = %Options{annotations: annotations}, recursive)
when is_binary(line) do
{line1, annotation} = line |> Helpers.expand_tabs() |> Helpers.remove_line_ending(annotations)
%{_type_of(line1, options, recursive) | annotation: annotation, lnb: lnb}
end

def type_of({line, lnb}, _, _) do
raise ArgumentError, "line number #{lnb} #{inspect line} is not a binary"
raise ArgumentError, "line number #{lnb} #{inspect(line)} is not a binary"
end

defp _type_of(line, options = %Options{}, recursive) do
Expand All @@ -84,7 +85,7 @@ defmodule Earmark.Parser.LineScanner do
lt_four? && Regex.run(~r/\A (?:_\s?){3,} \z/x, content) ->
%Line.Ruler{type: "_", indent: indent, line: line}

match = Regex.run(~R/^(#{1,6})\s+(?|(.*?)\s*#*\s*$|(.*))/u, stripped_line) ->
match = Regex.run(~r/^(\#{1,6})\s+(?|(.*?)\s*#*\s*$|(.*))/u, stripped_line) ->
[_, level, heading] = match

%Line.Heading{
Expand Down Expand Up @@ -144,7 +145,7 @@ defmodule Earmark.Parser.LineScanner do

match = lt_four? && Regex.run(@id_re, content) ->
[_, id, url | title] = match
title = if(length(title) == 0, do: "", else: hd(title))
title = if(Enum.empty?(title), do: "", else: hd(title))
%Line.IdDef{id: id, url: url, title: title, indent: indent, line: line}

match = options.footnotes && Regex.run(~r/\A\[\^([^\s\]]+)\]:\s+(.*)/, line) ->
Expand Down Expand Up @@ -184,7 +185,9 @@ defmodule Earmark.Parser.LineScanner do
line: line
}

line |> String.replace(~r/\[\[ .*? \]\]/x, "") |> String.match?(~r/\A (\s*) .* \s \| \s /x) ->
line
|> String.replace(~r/\[\[ .*? \]\]/x, "")
|> String.match?(~r/\A (\s*) .* \s \| \s /x) ->
columns = _split_table_columns(line)

%Line.TableLine{
Expand All @@ -195,7 +198,8 @@ defmodule Earmark.Parser.LineScanner do
line: line
}

options.gfm_tables && line |> String.replace(~r/\[\[ .*? \]\]/x, "") |> String.match?(~r/\A (\s*) .* \| /x) ->
options.gfm_tables &&
line |> String.replace(~r/\[\[ .*? \]\]/x, "") |> String.match?(~r/\A (\s*) .* \| /x) ->
columns = _split_table_columns(line)

%Line.TableLine{
Expand Down
2 changes: 1 addition & 1 deletion lib/earmark_parser/parser/link_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule Earmark.Parser.Parser.LinkParser do
defp url(_, _, _, _), do: nil

defp bail_out_to_title(ts, result) do
with remaining_text <- ts |> Enum.map(&text_of_token/1) |> Enum.join("") do
with remaining_text <- ts |> Enum.map_join("", &text_of_token/1) do
case title(remaining_text) do
nil ->
nil
Expand Down
Loading

0 comments on commit 8c0c9ae

Please sign in to comment.