Skip to content

Commit

Permalink
first sketch of replacemet
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Apr 30, 2022
1 parent 0b0ccb9 commit 4d142c8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/earmark/transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,10 @@
_walk_ast(rest, fun, ignore_strings, [new|result])
end
defp _walk_ast([{_, _, content, _}=tuple|rest], fun, ignore_strings, result) do
{new_tag, new_atts, _, new_meta} = fun.(tuple)
_walk_ast([content|rest], fun, ignore_strings, [@pop, {new_tag, new_atts, [], new_meta}|result])
case fun.(tuple) do
{new_tag, new_atts, _, new_meta} -> _walk_ast([content|rest], fun, ignore_strings, [@pop, {new_tag, new_atts, [], new_meta}|result])
{:replace, content} -> _walk_ast(rest, fun, ignore_strings, [content|result])
end
end
defp _walk_ast([[h|t]|rest], fun, ignore_strings, result) do
_walk_ast([h, t|rest], fun, ignore_strings, result)
Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule Earmark.Mixfile do
use Mix.Project

@version "1.4.23"
@version "1.4.25"

@url "https://github.com/pragdave/earmark"


@deps [
{:earmark_parser, "~> 1.4.24" },
{:earmark_parser, "~> 1.4.25" },
# {:earmark_parser, path: "../earmark_parser"},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:benchfella, "~> 0.3.0", only: [:dev]},
{:earmark_ast_dsl, "~> 0.2.5", only: [:test]},
{:earmark_ast_dsl, "~> 0.3.6", only: [:dev, :test]},
{:excoveralls, "~> 0.14.4", only: [:test]},
# {:extractly, "~> 0.5.0", git: "https://github.com/RobertDober/extractly.git", tag: "v0.5.0-pre1", only: [:dev]},
{:extractly, "~> 0.5.3", only: [:dev]},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"benchfella": {:hex, :benchfella, "0.3.5", "b2122c234117b3f91ed7b43b6e915e19e1ab216971154acd0a80ce0e9b8c05f5", [:mix], [], "hexpm", "23f27cbc482cbac03fc8926441eb60a5e111759c17642bac005c3225f5eb809d"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
"earmark_ast_dsl": {:hex, :earmark_ast_dsl, "0.2.5", "e44146944eebe83b33e3597da27fb9b37af7287c55e8cc3bd014cfcd5e57c355", [:mix], [], "hexpm", "da14595fcc283fa8d3ada8adb0b934cab791917b82cc5149cc4c404545cdc2b1"},
"earmark_parser": {:hex, :earmark_parser, "1.4.24", "344f8d2a558691d3fcdef3f9400157d7c4b3b8e58ee5063297e9ae593e8326d9", [:mix], [], "hexpm", "1f6451b0116dd270449c8f5b30289940ee9c0a39154c783283a08e55af82ea34"},
"earmark_ast_dsl": {:hex, :earmark_ast_dsl, "0.3.6", "bc3b0f06bdf89532cd039ea5b4e0df3839395a8197e46a7d7daa0bbd801202a9", [:mix], [], "hexpm", "47cc790831d41843d793fe0b6462036f2c615bb6a7b3874282431928c3824d07"},
"earmark_parser": {:hex, :earmark_parser, "1.4.25", "2024618731c55ebfcc5439d756852ec4e85978a39d0d58593763924d9a15916f", [:mix], [], "hexpm", "56749c5e1c59447f7b7a23ddb235e4b3defe276afc220a6227237f3efe83f51e"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"excoveralls": {:hex, :excoveralls, "0.14.4", "295498f1ae47bdc6dce59af9a585c381e1aefc63298d48172efaaa90c3d251db", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e3ab02f2df4c1c7a519728a6f0a747e71d7d6e846020aae338173619217931c1"},
"extractly": {:hex, :extractly, "0.5.3", "ca6afc3430e63cc9017d50eb03bdf1a3499c7e888037d2279b0ec0ea393af390", [:mix], [], "hexpm", "858f7a285ffb767937e75bb8866f03fecb3b7e5b42728a9677d2ebb6ea885503"},
Expand Down
53 changes: 53 additions & 0 deletions test/acceptance/earmark/postprocessor_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Test.Acceptance.Earmark.PostprocessorTest do
use ExUnit.Case

describe "use case from Issue 446" do
@no_code """
just a line
"""
test "no code" do
assert parse(@no_code) == "<p>\njust a line</p>\n"
end

@inline_code """
`alpha`
``beta
` ``
"""
test "inline code" do
expected = "<p>\n<code class=\"inline\">alpha</code>\n<code class=\"inline\">beta `</code></p>\n"
assert parse(@inline_code) == expected
end

@code_to_be_replaced """
pre
```
immaterial
```
post
"""
test "replacing" do
expected = "<p>\npre</p>\n<pre><code>xxx</code></pre>\n<p>\npost</p>\n"
assert parse(@code_to_be_replaced) == expected
end
end

defp parse(markdown) do
options = [
registered_processors: [
{"code", &render_code_node/1}
]
]
{:ok, post_html, _} = Earmark.as_html(markdown, options)
post_html
end

defp render_code_node({"code", attrs, _content, meta} = node) do
classes = Earmark.AstTools.find_att_in_node(node, "class") || ""
cond do
classes =~ "inline" -> node
true -> {:replace, {"code", attrs, "xxx", meta}}
end
end
end
# SPDX-License-Identifier: Apache-2.0
11 changes: 11 additions & 0 deletions test/acceptance/transform/map_ast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Test.Acceptance.Transform.MapAstTest do
use ExUnit.Case

import Earmark.Transform
import EarmarkAstDsl

describe "adding a count" do
@ast [
Expand Down Expand Up @@ -104,5 +105,15 @@ defmodule Test.Acceptance.Transform.MapAstTest do
defp push_annotation(text, anno), do: {text <> anno, nil}

end

describe "change content" do
test "replace content of a node" do
original = [div([p("x"), p("y")])]
new = [div([div(p("x")), p("y")])]
transformer = fn {"p", _, ["x"], _} = node -> {:replace, {"div", [], [node], %{}}}
other -> other end
assert map_ast(original, transformer) == new
end
end
end
# SPDX-License-Identifier: Apache-2.0

0 comments on commit 4d142c8

Please sign in to comment.