I am trying to parse a nested array of Symbols into HTML my input is:
[[:html,
[:head, [:title, "Hello World"]],
[:body, [:h1, "Hello World"], [:p, "This is your parser"]]]]
My intended Output is:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1> Hello World </h1>
<p> This is your parser</p>
</body>
</html>
My Method is:
def to_html(data)
if( data.is_a?(Array))
open_map = data.map do |item|
if( item.is_a?(Array))
to_html(item)
else
if( item.is_a?(Symbol))
"<"+ item.to_s + ">"
else
item.to_s
end
end
end
close_map = data.map do |item|
if( item.is_a?(Array))
to_html(item)
else
if( item.is_a?(Symbol))
"</"+ item.to_s + ">"
end
end
end
open_map.join(' ') + close_map.join(' ')
else
data.to_s
end
end
This basically works except that it recurses too much in that I think the two recursive calls to to_html results in double output
ie:
<html>
<head>
<title> Hello World
</title>
</head>
<title> Hello World
</title>
<body>
. . . .
and so on
I think I need to nest the recursions or else filter the results
How can I fix this?
[]