| @@ -0,0 +1,182 @@ |
| | 1 | require 'yajl/json_gem' |
| | 2 | |
| | 3 | module Docs |
| | 4 | class SupportTables < Doc |
| | 5 | include Instrumentable |
| | 6 | |
| | 7 | self.name = 'Support Tables' |
| | 8 | self.slug = 'browser_support_tables' |
| | 9 | self.type = 'support_tables' |
| | 10 | |
| | 11 | def build_pages |
| | 12 | url = 'https://github.com/Fyrd/caniuse/raw/master/data.json' |
| | 13 | instrument 'running.scraper', urls: [url] |
| | 14 | |
| | 15 | response = Request.run(url) |
| | 16 | instrument 'process_response.scraper', response: response |
| | 17 | |
| | 18 | data = JSON.parse(response.body) |
| | 19 | instrument 'queued.scraper', urls: data['data'].keys |
| | 20 | |
| | 21 | data['agents']['and_chr']['browser'] = 'Android Chrome' |
| | 22 | data['agents']['and_ff']['browser'] = 'Android Firefox' |
| | 23 | data['agents']['and_uc']['browser'] = 'Android UC Browser' |
| | 24 | data['desktop_agents'] = data['agents'].select { |_, agent| agent['type'] == 'desktop' } |
| | 25 | data['mobile_agents'] = data['agents'].select { |–, agent| agent['type'] == 'mobile' } |
| | 26 | data['total_versions'] = data['agents']['firefox']['versions'].length |
| | 27 | |
| | 28 | index_page = { |
| | 29 | path: 'index', |
| | 30 | store_path: 'index.html', |
| | 31 | output: ERB.new(INDEX_PAGE_ERB).result(binding), |
| | 32 | entries: [Entry.new(nil, 'index', nil)] |
| | 33 | } |
| | 34 | |
| | 35 | yield index_page |
| | 36 | |
| | 37 | data['data'].each do |feature_id, feature| |
| | 38 | url = "https://github.com/Fyrd/caniuse/raw/master/features-json/#{feature_id}.json" |
| | 39 | |
| | 40 | response = Request.run(url) |
| | 41 | instrument 'process_response.scraper', response: response |
| | 42 | |
| | 43 | feature = JSON.parse(response.body) |
| | 44 | |
| | 45 | name = feature['title'] |
| | 46 | type = feature['categories'].find { |category| name.include?(category) } || feature['categories'].first |
| | 47 | |
| | 48 | page = { |
| | 49 | path: feature_id, |
| | 50 | store_path: "#{feature_id}.html", |
| | 51 | output: ERB.new(PAGE_ERB).result(binding), |
| | 52 | entries: [Entry.new(name, feature_id, type)] |
| | 53 | } |
| | 54 | |
| | 55 | yield page |
| | 56 | end |
| | 57 | end |
| | 58 | |
| | 59 | def md_to_html(str) |
| | 60 | str = str.strip |
| | 61 | str.gsub! %r{`(.*?)`}, '<code>\1</code>' |
| | 62 | str.gsub! %r{\n\s*\n}, '</p><p>' |
| | 63 | str.gsub! "\n", '<br>' |
| | 64 | str.gsub! %r{\[(.+?)\]\((.+?)\)}, '<a href="\2">\1</a>' |
| | 65 | str |
| | 66 | end |
| | 67 | |
| | 68 | def support_to_css_class(support) |
| | 69 | support.select { |s| s.length == 1 }.join(' ') |
| | 70 | end |
| | 71 | |
| | 72 | def support_to_note_indicators(support) |
| | 73 | notes = support.select { |s| s.start_with?('#') }.map { |s| s[1..-1] } |
| | 74 | notes << '*' if support.include?('x') |
| | 75 | "<sup>(#{notes.join(',')})</sup>" if notes.present? |
| | 76 | end |
| | 77 | |
| | 78 | INDEX_PAGE_ERB = <<-HTML.strip_heredoc |
| | 79 | <h1>Browser support tables</h1> |
| | 80 | HTML |
| | 81 | |
| | 82 | PAGE_ERB = <<-HTML.strip_heredoc |
| | 83 | <h1><%= feature['title'] %></h1> |
| | 84 | |
| | 85 | <p><%= md_to_html feature['description'] %></p> |
| | 86 | |
| | 87 | <table> |
| | 88 | <% if feature['spec'].present? %> |
| | 89 | <tr> |
| | 90 | <th>Spec</th> |
| | 91 | <td><a href="<%= feature['spec'] %>"><%= feature['spec'] %></a></td> |
| | 92 | </tr> |
| | 93 | <% end %> |
| | 94 | |
| | 95 | <% if feature['status'].present? %> |
| | 96 | <tr> |
| | 97 | <th>Status</th> |
| | 98 | <td><%= data['statuses'][feature['status']] %></td> |
| | 99 | </tr> |
| | 100 | <% end %> |
| | 101 | </table> |
| | 102 | |
| | 103 | <% ['desktop', 'mobile'].each do |type| %> |
| | 104 | <table class="stats"> |
| | 105 | <tr> |
| | 106 | <% data["\#{type}_agents"].each do |agent_id, agent| %> |
| | 107 | <th><%= agent['browser'] %></th> |
| | 108 | <% end %> |
| | 109 | </tr> |
| | 110 | <% (0...(data['total_versions'])).reverse_each do |i| %> |
| | 111 | <% next if data["\#{type}_agents"].none? { |_, agent| agent['versions'][i] } %> |
| | 112 | <% if i == (data['total_versions'] - 8) %> |
| | 113 | <tr class="show-all"> |
| | 114 | <th class="show-all" colspan="<%= data["\#{type}_agents"].length %>"> |
| | 115 | <a href="#" class="show-all">Show all</a> |
| | 116 | </th> |
| | 117 | </tr> |
| | 118 | <% end %> |
| | 119 | <tr<%= ' class="current"' if i == (data['total_versions'] - 4) %>> |
| | 120 | <% data["\#{type}_agents"].each do |agent_id, agent| %> |
| | 121 | <% version = agent['versions'][i] %> |
| | 122 | <% if version %> |
| | 123 | <% support = feature['stats'][agent_id][version].split(' ') %> |
| | 124 | <% feature['prefix'] = true if support.include?('x') %> |
| | 125 | <td class="<%= support_to_css_class(support) %>"><%= version %> <%= support_to_note_indicators(support) %></td> |
| | 126 | <% else %> |
| | 127 | <td> </td> |
| | 128 | <% end %> |
| | 129 | <% end %> |
| | 130 | </tr> |
| | 131 | <% end %> |
| | 132 | </table> |
| | 133 | <% end %> |
| | 134 | |
| | 135 | <h2>Notes</h2> |
| | 136 | |
| | 137 | <% if feature['notes'].present? %> |
| | 138 | <p><%= md_to_html feature['notes'] %></p> |
| | 139 | <% end %> |
| | 140 | |
| | 141 | <% if feature['notes_by_num'].present? %> |
| | 142 | <ol> |
| | 143 | <% feature['notes_by_num'].each do |num, note| %> |
| | 144 | <li><p><%= md_to_html note %></p></li> |
| | 145 | <% end %> |
| | 146 | </ol> |
| | 147 | <% end %> |
| | 148 | |
| | 149 | <% if feature['prefix'] %> |
| | 150 | <dl> |
| | 151 | <dd><sup>*</sup> Partial support with prefix.</dd> |
| | 152 | </dl> |
| | 153 | <% end %> |
| | 154 | |
| | 155 | <% if feature['bugs'].present? %> |
| | 156 | <h2>Bugs</h2> |
| | 157 | <ul> |
| | 158 | <% feature['bugs'].each do |bug| %> |
| | 159 | <li><p><%= md_to_html bug['description'] %></p></li> |
| | 160 | <% end %> |
| | 161 | </ul> |
| | 162 | <% end %> |
| | 163 | |
| | 164 | <% if feature['links'].present? %> |
| | 165 | <h2>Resources</h2> |
| | 166 | <ul> |
| | 167 | <% feature['links'].each do |link| %> |
| | 168 | <li><a href="<%= link['url'] %>"><%= link['title'] %></a></li> |
| | 169 | <% end %> |
| | 170 | </ul> |
| | 171 | <% end %> |
| | 172 | |
| | 173 | <div class="_attribution"> |
| | 174 | <p class="_attribution-p"> |
| | 175 | Data by caniuse.com<br> |
| | 176 | Licensed under the Creative Commons Attribution License v4.0.<br> |
| | 177 | <a href="http://caniuse.com/#feat=<%= feature_id %>" class="_attribution-link">http://caniuse.com/#feat=<%= feature_id %></a> |
| | 178 | </p> |
| | 179 | </div> |
| | 180 | HTML |
| | 181 | end |
| | 182 | end |