@@ -31,35 +31,32 @@ defmodule ExDoc.DocAST do
31
31
@ doc """
32
32
Transform AST into string.
33
33
"""
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
35
37
36
- def to_string ( binary , _fun ) when is_binary ( binary ) do
38
+ defp to_iodata ( binary ) when is_binary ( binary ) do
37
39
ExDoc.Utils . h ( binary )
38
40
end
39
41
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 )
43
44
end
44
45
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 , " -->"]
47
48
end
48
49
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 ) } />"
52
52
end
53
53
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 } >" ]
58
56
end
59
57
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 } >" ]
63
60
end
64
61
65
62
defp ast_attributes_to_string ( attrs ) do
@@ -200,10 +197,6 @@ defmodule ExDoc.DocAST do
200
197
defp pivot ( [ head | tail ] , acc , headers ) , do: pivot ( tail , [ head | acc ] , headers )
201
198
defp pivot ( [ ] , acc , _headers ) , do: Enum . reverse ( acc )
202
199
203
- @ doc """
204
- Highlights a DocAST converted to string.
205
- """
206
- # TODO: Could this be done over the AST instead?
207
200
def highlight ( html , language , opts \\ [ ] ) do
208
201
highlight_info = language . highlight_info ( )
209
202
0 commit comments