Skip to content

Commit

Permalink
Refs: #288; Adding metadata for parsed html to AST and adapting Trans…
Browse files Browse the repository at this point in the history
…form WIP [amend-me]
  • Loading branch information
RobertDober committed Sep 27, 2019
1 parent 9ea31f4 commit efcc926
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 214 deletions.
7 changes: 4 additions & 3 deletions lib/earmark/helpers/html_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule Earmark.Helpers.HtmlParser do
# Iterate over lines inside a tag
# -------------------------------

@verbatim %{meta: %{verbatim: true}}
defp _parse_rest(rest, tag_tpl, lines)
# Hu? That should never have happened but let us return some
# sensible data, allowing rendering after the corruped block
Expand All @@ -74,9 +75,9 @@ defmodule Earmark.Helpers.HtmlParser do
end
defp _parse_rest([last_line], {tag, _}=tag_tpl, lines) do
case Regex.run(~r{\A</#{tag}>\s*(.*)}, last_line) do
nil -> tag_tpl |> Tuple.append(Enum.reverse([last_line|lines]))
[_, ""] -> tag_tpl |> Tuple.append(Enum.reverse(lines))
[_, suffix] -> [tag_tpl |> Tuple.append(Enum.reverse(lines)), suffix]
nil -> tag_tpl |> Tuple.append(Enum.reverse([last_line|lines])) |> Tuple.append(@verbatim)
[_, ""] -> tag_tpl |> Tuple.append(Enum.reverse(lines)) |> Tuple.append(@verbatim)
[_, suffix] -> [tag_tpl |> Tuple.append(Enum.reverse(lines)) |> Tuple.append(@verbatim), suffix]
end
end
defp _parse_rest([inner_line|rest], tag_tpl, lines) do
Expand Down
26 changes: 14 additions & 12 deletions test/acceptance/ast/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ defmodule Acceptance.Ast.HtmlBlocksTest do

@moduletag :ast

@verbatim %{meta: %{verbatim: true}}

describe "HTML blocks" do
test "tables are just tables again (or was that mountains?)" do
markdown = "<table>\n <tr>\n <td>\n hi\n </td>\n </tr>\n</table>\n\nokay.\n"
ast = [
{"table", [], [" <tr>", " <td>", " hi", " </td>", " </tr>"]},
{"table", [], [" <tr>", " <td>", " hi", " </td>", " </tr>"], @verbatim},
{"p", [], ["okay."]}]
messages = []

Expand All @@ -18,23 +20,23 @@ defmodule Acceptance.Ast.HtmlBlocksTest do

test "div (ine?)" do
markdown = "<div>\n *hello*\n <foo><a>\n</div>\n"
ast = [{"div", [], [" *hello*", " <foo><a>"]}]
ast = [{"div", [], [" *hello*", " <foo><a>"], @verbatim}]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end

test "we are leaving html alone" do
markdown = "<div>\n*Emphasized* text.\n</div>"
ast = [{"div", [], ["*Emphasized* text."]}]
ast = [{"div", [], ["*Emphasized* text."], @verbatim}]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end

test "even block elements" do
markdown = "<div>\n```elixir\ndefmodule Mine do\n```\n</div>"
ast = [{"div", [], ["```elixir", "defmodule Mine do", "```"]}]
ast = [{"div", [], ["```elixir", "defmodule Mine do", "```"], @verbatim}]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand Down Expand Up @@ -190,57 +192,57 @@ defmodule Acceptance.Ast.HtmlBlocksTest do
describe "multiple tags in closing line" do
test "FTF" do
markdown = "<div class=\"my-div\">\nline\n</div>"
ast = [{"div", [{"class", "my-div"}], ["line"]}]
ast = [{"div", [{"class", "my-div"}], ["line"], @verbatim}]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end
test "this is not closing" do
markdown = "<div>\nline\n</hello></div>"
ast = [{"div", [], ["line", "</hello></div>"]}]
ast = [{"div", [], ["line", "</hello></div>"], @verbatim}]
messages = [{:warning, 1, "Failed to find closing <div>"}]

assert as_ast(markdown) == {:error, ast, messages}
end
test "therefore the div continues" do
markdown = "<div>\nline\n</hello></div>\n</div>"
ast = [{"div", [], ["line", "</hello></div>"]}]
ast = [{"div", [], ["line", "</hello></div>"], @verbatim}]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end
test "...nor is this" do
markdown = "<div>\nline\n<hello></div>"
ast = [{"div", [], ["line", "<hello></div>"]}]
ast = [{"div", [], ["line", "<hello></div>"], @verbatim}]
messages = [{:warning, 1, "Failed to find closing <div>"},
{:warning, 3, "Failed to find closing <hello>"}]

assert as_ast(markdown) == {:error, ast, messages}
end
test "however, this closes and keeps the garbage" do
markdown = "<div>\nline\n</div><hello>"
ast = [{"div", [], ["line"]}, "<hello>"]
ast = [{"div", [], ["line"], @verbatim}, "<hello>"]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end
test "however, this closes and keeps **whatever** garbage" do
markdown = "<div>\nline\n</div> `garbage`"
ast = [{"div", [], ["line"]}, "`garbage`"]
ast = [{"div", [], ["line"], @verbatim}, "`garbage`"]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end
test "however, this closes and keeps not even warnings" do
markdown = "<div>\nline\n</div> `garbage"
ast = [{"div", [], ["line"]}, "`garbage"]
ast = [{"div", [], ["line"], @verbatim}, "`garbage"]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
end
test "however, this closes and kept garbage is not even inline formatted" do
markdown = "<div>\nline\n</div> _garbage_"
ast = [{"div", [], ["line"]}, "_garbage_"]
ast = [{"div", [], ["line"], @verbatim}, "_garbage_"]
messages = []

assert as_ast(markdown) == {:ok, ast, messages}
Expand Down
Loading

0 comments on commit efcc926

Please sign in to comment.