Packages
earmark_parser
1.4.42
1.4.46
1.4.45
1.4.44
1.4.43
1.4.42
1.4.41
1.4.40
1.4.39
1.4.38
1.4.37
1.4.36
1.4.35
1.4.34
1.4.33
1.4.32
1.4.31
1.4.30
1.4.29
1.4.28
1.4.27
1.4.26
1.4.25
1.4.24
1.4.23
1.4.22
1.4.21
1.4.20
1.4.20-pre
1.4.19
1.4.18
1.4.17
1.4.16
1.4.16-pre2
1.4.16-pre1
1.4.16-pre
1.4.15
1.4.13
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
AST parser and generator for Markdown
Current section
Files
Jump to
Current section
Files
lib/earmark_parser/ast/renderer/ast_walker.ex
defmodule EarmarkParser.Ast.Renderer.AstWalker do
@moduledoc false
def walk(anything, fun, ignore_map_keys \\ false), do: _walk(anything, fun, ignore_map_keys, false)
def walk_ast(ast, fun), do: _walk_ast(ast, fun, [])
defp _walk(ast, fun, ignore_map_keys, child_of_map)
defp _walk([], _fun, _ignore_map_keys, _child_of_map), do: []
defp _walk(list, fun, ignore_map_keys, _child_of_map) when is_list(list) do
Enum.map(list, &(_walk(&1, fun, ignore_map_keys, false)))
end
defp _walk(map, fun, ignore_map_keys, _child_of_map) when is_map(map) do
map
|> Enum.into(%{}, &(_walk(&1, fun, ignore_map_keys, true)))
end
defp _walk(tuple, fun, ignore_map_keys, child_of_map) when is_tuple(tuple) do
if child_of_map && ignore_map_keys do
_walk_map_element(tuple, fun, ignore_map_keys)
else
tuple
|> Tuple.to_list
|> Enum.map(&(_walk(&1, fun, ignore_map_keys, false)))
|> List.to_tuple
end
end
defp _walk(ele, fun, _ignore_map_keys, _child_of_map), do: fun.(ele)
defp _walk_map_element({key, value}, fun, ignore_map_keys) do
{key, _walk(value, fun, ignore_map_keys, false)}
end
defp _walk_ast(ast, fun, res)
defp _walk_ast([], _fun, res), do: Enum.reverse(res)
defp _walk_ast(stringy, fun, res) when is_binary(stringy), do: _walk_ast([stringy], fun, res)
defp _walk_ast([stringy|rest], fun, res) when is_binary(stringy) do
res1 =
case fun.(stringy) do
[] -> res
[_|_]=trans -> List.flatten([Enum.reverse(trans)|res])
stringy1 -> [stringy1|res]
end
_walk_ast(rest, fun, res1)
end
defp _walk_ast([{tag, atts, content, meta}|rest], fun, res) do
_walk_ast(rest, fun, [{tag, atts, _walk_ast(content, fun, []), meta}|res])
end
defp _walk_ast([list|rest], fun, res) when is_list(list) do
_walk_ast(rest, fun, [_walk_ast(list, fun, [])|res])
end
end