Sha256: af2ca74336ebd2f69f3e6e9ea61b202c4826e96a90540305667cce96d7dcfc30
Contents?: true
Size: 1.72 KB
Versions: 15
Compression:
Stored size: 1.72 KB
Contents
require 'nokogiri' require 'open-uri' require 'zomg' require 'activesupport' doc = Nokogiri::HTML(open('http://w3c-test.org/webperf/specs/NavigationTiming/')) idl = [] doc.css('pre.idl').each do |content| idl << content.text end nodes = [] idl.each do |spec| nodes << ZOMG::IDL.parse(spec) unless ZOMG::IDL.parse(spec).nil? end results = {} results[:class] = "Performance" results[:interfaces] = {} nodes.each do |node| interface_name = node.children.first.header.name.gsub("Performance","") results[:interfaces][interface_name] = {} results[:interfaces][interface_name][:attrs] = [] results[:interfaces][interface_name][:constants] = {} node.children.first.children.each do |child| case child.class.to_s when "ZOMG::IDL::Nodes::Attribute" results[:interfaces][interface_name][:attrs] << child.children.first.name.underscore when "ZOMG::IDL::Nodes::Constant" results[:interfaces][interface_name][:constants][child.name] = child.children.children end end end open(File.expand_path(File.dirname(__FILE__)) + "/#{results[:class].underscore}.rb", "w") do |f| f.puts "# auto-generated by idl_extractor.rb" f.puts "class #{results[:class]}" results[:interfaces].each_key do |interface_name| f.puts " " * 2 + "class #{interface_name}" results[:interfaces][interface_name][:constants].each do |k,v| f.puts " " * 4 + "#{k} = #{v.to_i}" end f.puts " " * 4 + "attr_reader " + results[:interfaces][interface_name][:attrs].collect! {|x| ":"+x}.join(", ") f.puts <<-'eos' def initialize args args.each do |k,v| instance_variable_set("@#{k}", v) if respond_to?(k.to_sym) unless v.nil? end end eos f.puts " " * 2 + "end" end f.puts "end" end
Version data entries
15 entries across 15 versions & 2 rubygems