Skip to content

Instantly share code, notes, and snippets.

@sofaking
Last active October 11, 2016 07:31
Show Gist options
  • Save sofaking/1f53d4383f452c110e6347118dc8d0af to your computer and use it in GitHub Desktop.
Save sofaking/1f53d4383f452c110e6347118dc8d0af to your computer and use it in GitHub Desktop.
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
@plus3x
Copy link

plus3x commented Oct 11, 2016

Booooooo)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment