Created
January 22, 2016 03:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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