Skip to content

Instantly share code, notes, and snippets.

@maxjacobson
Created January 22, 2016 03:29
Show Gist options
  • Save maxjacobson/b4796a2ebca1c7d6ba33 to your computer and use it in GitHub Desktop.
Save maxjacobson/b4796a2ebca1c7d6ba33 to your computer and use it in GitHub Desktop.
This is an idea to help compare two HTML files which may have a lot of superficial differences but are structurally the same. Specifically, it's meant to reduce an HTML file to its essence... and reveal just the elements and classes
require 'nokogiri'
failing = Nokogiri::HTML(File.read(ARGV.first))
def node_to_just_classes(node)
{
'name' => node.name,
'classes' => (node.attributes["class"]&.value&.split(" ")&.compact || []),
'children' => node.children.map { |child| node_to_just_classes(child) }
}
end
just_classes = failing.children.map do |node|
node_to_just_classes(node)
end
puts just_classes.to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment