Skip to content

Instantly share code, notes, and snippets.

@dchacke
Last active May 4, 2021 05:15
Show Gist options
  • Save dchacke/277786be964d1ca3a2c8e9412bf4b618 to your computer and use it in GitHub Desktop.
Save dchacke/277786be964d1ca3a2c8e9412bf4b618 to your computer and use it in GitHub Desktop.
def to_html structure
if structure.is_a? Hash
structure.reduce([]) do |acc, curr|
acc + [[curr[0].to_s, '="', curr[1].to_s, '"'].join]
end.join(' ')
elsif structure.is_a? Array
tag = to_html structure[0]
attrs = to_html structure[1]
children = structure.drop(2).map { |s| to_html s }.join
"<#{tag} #{attrs}>#{children}</#{tag}>"
else
structure.to_s
end
end
# to_html [:html, {lang: 'en'}, [:h1, {class: 'heading big'}, 'some header']]
# => "<html lang=\"en\"><h1 class=\"heading big\">some header</h1></html>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment