Skip to content

Instantly share code, notes, and snippets.

@dchacke
Last active May 4, 2021 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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, (key, val)|
acc + [[key.to_s, '="', val.to_s, '"'].join]
end.join(' ')
elsif structure.is_a? Array
if structure[1].is_a? Hash
tag, attrs, *children = structure.map { |s| to_html s}
tag_and_attrs = structure[1].any? ? [tag, ' ', attrs].join : tag
else
tag, *children = structure.map { |s| to_html s}
end
['<', tag_and_attrs || tag, '>', children.join, '</', tag, '>'].join
else
structure.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment