Last active
October 11, 2016 07:31
-
-
Save sofaking/1f53d4383f452c110e6347118dc8d0af to your computer and use it in GitHub Desktop.
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 'rexml/sax2listener' | |
require 'rexml/parsers/sax2parser' | |
class MrProper | |
include REXML::SAX2Listener | |
BORING_ATTRS = %w(class index package | |
checkable checked clickable | |
enabled focusable focused | |
scrollable long-clickable password | |
selected bounds instance) | |
INDENT_WIDTH = 2 | |
ATTRIBUTES_INDENT = 4 | |
def initialize | |
@indent = 0 | |
end | |
def start_element(uri, localname, qname, attributes) | |
return if localname == 'hierarchy' | |
@indent += 1 | |
node = ' ' * INDENT_WIDTH * @indent | |
node << "class_name: '#{localname}'" | |
clean_attributes = attributes.delete_if { |k, v| BORING_ATTRS.include?(k) || v.empty? } | |
if value = clean_attributes.delete('resource-id') | |
clean_attributes[:id] = value | |
end | |
unless clean_attributes.empty? | |
node << ' ' * ATTRIBUTES_INDENT | |
node << clean_attributes.map { |k, v| "#{k}: '#{v}'" }.join(', ') | |
end | |
puts node | |
end | |
def end_element(uri, localname, qname) | |
@indent -= 1 | |
end | |
end | |
p = REXML::Parsers::SAX2Parser.new(File.open('xml')) | |
p.listen(MrProper.new) | |
p.parse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Booooooo)