Skip to content

Commit cbe35b1

Browse files
committed
Simplify and optimize to_string
1 parent d9d8fff commit cbe35b1

File tree

2 files changed

+14
-40
lines changed

2 files changed

+14
-40
lines changed

lib/ex_doc/doc_ast.ex

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,32 @@ defmodule ExDoc.DocAST do
3131
@doc """
3232
Transform AST into string.
3333
"""
34-
def to_string(ast, fun \\ fn _ast, string -> string end)
34+
def to_string(binary) do
35+
IO.iodata_to_binary(to_iodata(binary))
36+
end
3537

36-
def to_string(binary, _fun) when is_binary(binary) do
38+
defp to_iodata(binary) when is_binary(binary) do
3739
ExDoc.Utils.h(binary)
3840
end
3941

40-
def to_string(list, fun) when is_list(list) do
41-
result = Enum.map_join(list, "", &to_string(&1, fun))
42-
fun.(list, result)
42+
defp to_iodata(list) when is_list(list) do
43+
Enum.map(list, &to_iodata/1)
4344
end
4445

45-
def to_string({:comment, _attrs, inner, _meta} = ast, fun) do
46-
fun.(ast, "<!--#{inner}-->")
46+
defp to_iodata({:comment, _attrs, inner, _meta}) do
47+
["<!--", inner, "-->"]
4748
end
4849

49-
def to_string({tag, attrs, _inner, _meta} = ast, fun) when tag in @void_elements do
50-
result = "<#{tag}#{ast_attributes_to_string(attrs)}/>"
51-
fun.(ast, result)
50+
defp to_iodata({tag, attrs, _inner, _meta}) when tag in @void_elements do
51+
"<#{tag}#{ast_attributes_to_string(attrs)}/>"
5252
end
5353

54-
def to_string({tag, attrs, inner, %{verbatim: true}} = ast, fun) do
55-
inner = Enum.join(inner, "")
56-
result = "<#{tag}#{ast_attributes_to_string(attrs)}>" <> inner <> "</#{tag}>"
57-
fun.(ast, result)
54+
defp to_iodata({tag, attrs, inner, %{verbatim: true}}) do
55+
["<#{tag}#{ast_attributes_to_string(attrs)}>", inner, "</#{tag}>"]
5856
end
5957

60-
def to_string({tag, attrs, inner, _meta} = ast, fun) do
61-
result = "<#{tag}#{ast_attributes_to_string(attrs)}>" <> to_string(inner, fun) <> "</#{tag}>"
62-
fun.(ast, result)
58+
defp to_iodata({tag, attrs, inner, _meta}) do
59+
["<#{tag}#{ast_attributes_to_string(attrs)}>", to_iodata(inner), "</#{tag}>"]
6360
end
6461

6562
defp ast_attributes_to_string(attrs) do
@@ -200,10 +197,6 @@ defmodule ExDoc.DocAST do
200197
defp pivot([head | tail], acc, headers), do: pivot(tail, [head | acc], headers)
201198
defp pivot([], acc, _headers), do: Enum.reverse(acc)
202199

203-
@doc """
204-
Highlights a DocAST converted to string.
205-
"""
206-
# TODO: Could this be done over the AST instead?
207200
def highlight(html, language, opts \\ []) do
208201
highlight_info = language.highlight_info()
209202

test/ex_doc/doc_ast_test.exs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,6 @@ defmodule ExDoc.DocASTTest do
8383
assert DocAST.to_string(ast) == ~s{<span><i></i>\n<i></i></span>}
8484
end
8585

86-
test "with fun" do
87-
markdown = """
88-
foo **bar** baz
89-
"""
90-
91-
ast = DocAST.parse!(markdown, "text/markdown")
92-
93-
f = fn
94-
{:strong, _, _, _}, string ->
95-
String.upcase(string)
96-
97-
_ast, string ->
98-
string
99-
end
100-
101-
assert DocAST.to_string(ast, f) ==
102-
"<p>foo <STRONG>BAR</STRONG> baz</p>"
103-
end
104-
10586
test "void elements" do
10687
markdown = """
10788
foo\s\s

0 commit comments

Comments
 (0)