require 'yajl/json_gem' module Docs class SupportTables < Doc include Instrumentable self.name = 'Support Tables' self.slug = 'browser_support_tables' self.type = 'support_tables' def build_pages url = 'https://github.com/Fyrd/caniuse/raw/master/data.json' instrument 'running.scraper', urls: [url] response = Request.run(url) instrument 'process_response.scraper', response: response data = JSON.parse(response.body) instrument 'queued.scraper', urls: data['data'].keys data['agents']['and_chr']['browser'] = 'Android Chrome' data['agents']['and_ff']['browser'] = 'Android Firefox' data['agents']['and_uc']['browser'] = 'Android UC Browser' data['desktop_agents'] = data['agents'].select { |_, agent| agent['type'] == 'desktop' } data['mobile_agents'] = data['agents'].select { |–, agent| agent['type'] == 'mobile' } data['total_versions'] = data['agents']['firefox']['versions'].length index_page = { path: 'index', store_path: 'index.html', output: ERB.new(INDEX_PAGE_ERB).result(binding), entries: [Entry.new(nil, 'index', nil)] } yield index_page data['data'].each do |feature_id, feature| url = "https://github.com/Fyrd/caniuse/raw/master/features-json/#{feature_id}.json" response = Request.run(url) instrument 'process_response.scraper', response: response feature = JSON.parse(response.body) name = feature['title'] type = feature['categories'].find { |category| name.include?(category) } || feature['categories'].first page = { path: feature_id, store_path: "#{feature_id}.html", output: ERB.new(PAGE_ERB).result(binding), entries: [Entry.new(name, feature_id, type)] } yield page end end def md_to_html(str) str = str.strip str.gsub! %r{`(.*?)`}, '\1' str.gsub! %r{\n\s*\n}, '

' str.gsub! "\n", '
' str.gsub! %r{\[(.+?)\]\((.+?)\)}, '\1' str end def support_to_css_class(support) support.select { |s| s.length == 1 }.join(' ') end def support_to_note_indicators(support) notes = support.select { |s| s.start_with?('#') }.map { |s| s[1..-1] } notes << '*' if support.include?('x') "(#{notes.join(',')})" if notes.present? end INDEX_PAGE_ERB = <<-HTML.strip_heredoc

Browser support tables

HTML PAGE_ERB = <<-HTML.strip_heredoc

<%= feature['title'] %>

<%= md_to_html feature['description'] %>

<% if feature['spec'].present? %> <% end %> <% if feature['status'].present? %> <% end %>
Spec <%= feature['spec'] %>
Status <%= data['statuses'][feature['status']] %>
<% ['desktop', 'mobile'].each do |type| %> <% data["\#{type}_agents"].each do |agent_id, agent| %> <% end %> <% (0...(data['total_versions'])).reverse_each do |i| %> <% next if data["\#{type}_agents"].none? { |_, agent| agent['versions'][i] } %> <% if i == (data['total_versions'] - 8) %> <% end %> > <% data["\#{type}_agents"].each do |agent_id, agent| %> <% version = agent['versions'][i] %> <% if version %> <% support = feature['stats'][agent_id][version].split(' ') %> <% feature['prefix'] = true if support.include?('x') %> <% else %> <% end %> <% end %> <% end %>
<%= agent['browser'] %>
"> Show all
<%= version %> <%= support_to_note_indicators(support) %> 
<% end %>

Notes

<% if feature['notes'].present? %>

<%= md_to_html feature['notes'] %>

<% end %> <% if feature['notes_by_num'].present? %>
    <% feature['notes_by_num'].each do |num, note| %>
  1. <%= md_to_html note %>

  2. <% end %>
<% end %> <% if feature['prefix'] %>
* Partial support with prefix.
<% end %> <% if feature['bugs'].present? %>

Bugs

<% end %> <% if feature['links'].present? %>

Resources

<% end %>

Data by caniuse.com
Licensed under the Creative Commons Attribution License v4.0.
http://caniuse.com/#feat=<%= feature_id %>

HTML end end