Skip to content

Commit

Permalink
Cosmetics + several classes for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed Aug 27, 2023
1 parent 9739e6d commit 74c6515
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
## Refactoring Opportunities
#
{Credo.Check.Refactor.CondStatements, []},
{Credo.Check.Refactor.CyclomaticComplexity, max_complexity: 42},
{Credo.Check.Refactor.CyclomaticComplexity, max_complexity: 50},
{Credo.Check.Refactor.FunctionArity, []},
{Credo.Check.Refactor.LongQuoteBlocks, max_line_count: 300},
{Credo.Check.Refactor.MapInto, false},
Expand Down
1 change: 1 addition & 0 deletions lib/md/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ defmodule Md.Parser do
{ast, acc} =
case walker do
nil -> {ast, nil}
fun when is_function(fun, 1) -> {XmlBuilder.prewalk(ast, fun), nil}
fun when is_function(fun, 2) -> XmlBuilder.prewalk(ast, %{}, fun)
{:pre, fun} when is_function(fun, 1) -> {XmlBuilder.prewalk(ast, fun), nil}
{:post, fun} when is_function(fun, 1) -> {XmlBuilder.postwalk(ast, fun), nil}
Expand Down
15 changes: 12 additions & 3 deletions lib/md/parser/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1610,8 +1610,17 @@ defmodule Md.Engine do

defp update_attrs({tag, attrs, [value | rest]} = full_tag, pop) do
case pop do
%{^tag => attr} -> {tag, Map.put(attrs || %{}, attr, value), rest}
_ -> full_tag
%{^tag => attr} when is_atom(attr) ->
{tag, Map.put(attrs || %{}, attr, value), rest}

%{^tag => attr} when is_list(attr) ->
attr_value =
attr |> Keyword.get(:prefixes, [""]) |> Enum.map_join(" ", &(&1 <> value))

{tag, Map.put(attrs || %{}, attr[:attribute], attr_value), rest}

_ ->
full_tag
end
end

Expand Down Expand Up @@ -1672,7 +1681,7 @@ defmodule Md.Engine do
do: {nest(:span), %{class: "empty-anchor"}, content}

defp maybe_hide({tag, attrs, []}),
do: {tag, Map.put(attrs || %{}, :class, :"empty-tag"), []}
do: {tag, Map.put(attrs || %{}, :class, "empty-tag"), []}

defp maybe_hide({_tag, _attrs, _branch} = trace), do: trace

Expand Down
2 changes: 1 addition & 1 deletion lib/md/parser/syntax/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Md.Parser.Syntax.Default do
{"http://", %{transform: Anchor, terminators: [], greedy: :left}}
],
block: [
{"```", %{tag: [:pre, :code], pop: %{code: :class}}}
{"```", %{tag: [:pre, :code], pop: %{code: [attribute: :class, prefixes: ["", "lang-"]]}}}
],
shift: [
{" ", %{tag: [:div, :code], attributes: %{class: "pre"}}}
Expand Down
16 changes: 10 additions & 6 deletions test/md_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ defmodule MdTest do
{:p, nil, ["foo"]},
{:pre, nil,
[
{:code, %{class: "elixir"}, ["def foo, do: :ok\n\ndef bar, do: :error\n"]}
{:code, %{class: "elixir lang-elixir"},
["def foo, do: :ok\n\ndef bar, do: :error\n"]}
]}
] ==
Md.parse(input).ast
Expand Down Expand Up @@ -405,15 +406,15 @@ defmodule MdTest do

assert [
{:blockquote, nil,
[{:p, nil, ["line 1", "\n", {:i, %{class: :"empty-tag"}, []}, "line 2"]}]}
[{:p, nil, ["line 1", "\n", {:i, %{class: "empty-tag"}, []}, "line 2"]}]}
] ==
Md.parse(">line 1\n>_\n>line 2").ast

assert [
{:blockquote, nil,
[
{:p, nil,
["line 1", "\n", {:em, nil, [{:i, %{class: :"empty-tag"}, []}]}, "line 2"]}
["line 1", "\n", {:em, nil, [{:i, %{class: "empty-tag"}, []}]}, "line 2"]}
]}
] ==
Md.parse(">line 1\n>___\n>line 2").ast
Expand All @@ -425,8 +426,8 @@ defmodule MdTest do
[
"line 1",
"\n",
{:em, %{class: :"empty-tag"}, []},
{:em, nil, [{:i, %{class: :"empty-tag"}, []}]},
{:em, %{class: "empty-tag"}, []},
{:em, nil, [{:i, %{class: "empty-tag"}, []}]},
"line 2"
]}
]}
Expand Down Expand Up @@ -691,7 +692,10 @@ defmodule MdTest do
]},
{:p, nil, ["Hi ", {:a, %{href: "https://anchor.com"}, ["anchor"]}, " 1!"]},
{:pre, nil,
[{:code, %{class: "elixir"}, ["def foo, do: :ok\n\ndef bar, do: :error\n"]}]},
[
{:code, %{class: "elixir lang-elixir"},
["def foo, do: :ok\n\ndef bar, do: :error\n"]}
]},
{:ul, nil,
[
{:li, nil,
Expand Down

0 comments on commit 74c6515

Please sign in to comment.