1
0

support_tables.rb 5.9 KB

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