From 74c65157407af9063a4709f83aa51af04e0b96cc Mon Sep 17 00:00:00 2001 From: Aleksei Matiushkin Date: Sun, 27 Aug 2023 08:54:41 +0200 Subject: [PATCH] Cosmetics + several classes for blocks --- .credo.exs | 2 +- lib/md/parser.ex | 1 + lib/md/parser/engine.ex | 15 ++++++++++++--- lib/md/parser/syntax/default.ex | 2 +- test/md_test.exs | 16 ++++++++++------ 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.credo.exs b/.credo.exs index 6c15930..d95867b 100644 --- a/.credo.exs +++ b/.credo.exs @@ -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}, diff --git a/lib/md/parser.ex b/lib/md/parser.ex index 11583a2..7641bb6 100644 --- a/lib/md/parser.ex +++ b/lib/md/parser.ex @@ -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} diff --git a/lib/md/parser/engine.ex b/lib/md/parser/engine.ex index b3f6239..ad6913a 100644 --- a/lib/md/parser/engine.ex +++ b/lib/md/parser/engine.ex @@ -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 @@ -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 diff --git a/lib/md/parser/syntax/default.ex b/lib/md/parser/syntax/default.ex index 337bfbe..0d893f3 100644 --- a/lib/md/parser/syntax/default.ex +++ b/lib/md/parser/syntax/default.ex @@ -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"}}} diff --git a/test/md_test.exs b/test/md_test.exs index aaf5381..6dc3e0b 100644 --- a/test/md_test.exs +++ b/test/md_test.exs @@ -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 @@ -405,7 +406,7 @@ 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 @@ -413,7 +414,7 @@ defmodule MdTest do {: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 @@ -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" ]} ]} @@ -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,