Forráskód Böngészése

Merge pull request #1514 from MasterEnoc/compat-data

Add support of compatibility tables
Bryan Hernández 4 éve
szülő
commit
ec612df3d6

+ 212 - 0
lib/docs/filters/mdn/compat_tables.rb

@@ -0,0 +1,212 @@
+module Docs
+  class Mdn
+    class CompatTablesFilter < Filter
+
+      def call
+        if at_css('#browser_compatibility') \
+          and not at_css('#browser_compatibility').next_sibling.classes.include?('warning') \
+          and not at_css('#browser_compatibility').next_sibling.content.match?('Supported')
+
+          at_css('#browser_compatibility').next_sibling.remove
+
+          compatibility_tables = generate_compatibility_table()
+          compatibility_tables.each do |table|
+            at_css('#browser_compatibility').add_next_sibling(table)
+          end
+        end
+
+        doc
+      end
+
+      BROWSERS = {
+        'chrome' => 'Chrome',
+        'edge' => 'Edge',
+        'firefox' => 'Firefox',
+        'ie' => 'Internet Explorer',
+        'opera' => 'Opera',
+        'safari' => 'Safari',
+        'webview_android' => 'WebView Android',
+        'chrome_android' => 'Chrome Android',
+        'firefox_android' => 'Firefox for Android',
+        'opera_android' => 'Opera Android',
+        'safari_ios' => 'Safari on IOS',
+        'samsunginternet_android' => 'Samsung Internet'
+      }
+
+      def generate_compatibility_table()
+        json_files_uri = request_bcd_uris()
+
+        compat_tables = []
+
+        json_files_uri.each do |uri|
+          compat_tables.push(generate_compatibility_table_wrapper(uri))
+        end
+
+        return compat_tables
+      end
+
+      def request_bcd_uris
+        index_json = JSON.load(Net::HTTP.get(URI(current_url.to_s + '/index.json')))
+
+        uris = []
+
+        index_json['doc']['body'].each do |element|
+          uris.push(element['value']['dataURL']) if element['type'] == 'browser_compatibility' and element['value']['dataURL']
+        end
+
+        uris.map! do |uri|
+          tmp_uri = URI.parse(base_url.to_s)
+          tmp_uri.path = uri
+          uri = tmp_uri.to_s
+        end
+
+        return uris
+      end
+
+      def generate_compatibility_table_wrapper(uri)
+
+        @json_data = JSON.load(Net::HTTP.get(URI(uri)))['data']
+
+        html_table = generate_basic_html_table()
+
+        @json_data.keys.each do |key|
+          if key == '__compat' or @json_data[key]['__compat']
+            add_entry_to_table(html_table, key)
+          else
+          end
+        end
+
+        return html_table
+      end
+
+      def generate_basic_html_table
+        table = Nokogiri::XML::Node.new('table', doc)
+
+        table.add_child('<thead><tr id=bct-browser-type><tr id=bct-browsers><tbody>')
+
+        table.css('#bct-browser-type').each do |node|
+          node.add_child('<th>')
+          %w(Desktop Mobile).each do |browser_type|
+            node.add_child("<th colspan=6>#{browser_type}")
+          end
+        end
+
+        table.css('#bct-browsers').each do |node|
+          node.add_child('<th>')
+
+          BROWSERS.values.each do |browser|
+            node.add_child("<th>#{browser}")
+          end
+        end
+
+        return table
+      end
+
+      def add_entry_to_table(html_table, key)
+        json = @json_data[key]
+
+        html_table.at_css('tbody').add_child('<tr>')
+
+        last_table_entry = html_table.at_css('tbody').last_element_child
+
+        if key == '__compat'
+          tmp = slug.split('/')
+          last_table_entry.add_child("<th><code>#{tmp.last}")
+        else
+          last_table_entry.add_child("<th><code>#{key}")
+        end
+
+
+        BROWSERS.keys.each do |browser_key|
+          if key == '__compat'
+            add_data_to_entry(json['support'][browser_key], last_table_entry)
+          else
+            add_data_to_entry(json['__compat']['support'][browser_key], last_table_entry)
+          end
+
+        end
+      end
+
+      def add_data_to_entry(json, entry)
+        version_added = []
+        version_removed = []
+        notes = []
+
+        if json
+          if json.is_a?(Array)
+            json.each do |element|
+
+              if element['version_added']
+                version_added.push(element['version_added'])
+              else
+                version_added.push(false)
+              end
+
+              if element['version_removed']
+                version_removed.push(element['version_removed'])
+              else
+                version_removed.push(false)
+              end
+
+              if element['notes']
+                notes.push(element['notes'])
+              else
+                notes.push(false)
+              end
+
+            end
+          else
+            version_added.push(json['version_added'])
+            version_removed.push(json['version_removed'])
+            notes.push(json['notes'])
+          end
+
+          version_added.map! do |version|
+            if version == true
+              version = 'Yes'
+            elsif version == false
+              version = 'No'
+            elsif version.is_a?(String)
+            else
+              version = '?'
+            end
+
+            version
+          end
+
+          if version_removed[0]
+            format_string = "<td class=bc-supports-no>"
+          else
+            if version_added[0] == 'No'
+              format_string = "<td class=bc-supports-no>"
+            else
+              format_string = "<td class=bc-supports-yes>"
+            end
+          end
+
+          for value in (0..version_added.length-1) do
+            if version_removed[value]
+              format_string += "<div>#{version_added[value]}-#{version_removed[value]}</div>"
+            else
+              if version_added[value] == 'No'
+                format_string += "<div>#{version_added[value]}</div>"
+              else
+                format_string += "<div>#{version_added[value]}</div>"
+              end
+            end
+
+            if notes[value]
+              format_string += "<div>#{notes[value]}</div>"
+            end
+          end
+
+        else
+          format_string = "<td class=bc-supports-no><div>?</div></td>"
+        end
+
+        entry.add_child(format_string)
+      end
+
+    end
+  end
+end

+ 0 - 1
lib/docs/filters/svg/clean_html.rb

@@ -7,7 +7,6 @@ module Docs
       end
 
       def root
-        doc.inner_html = doc.at_css('#Documentation + dl').to_html
       end
 
       def other

+ 3 - 2
lib/docs/filters/xslt_xpath/entries.rb

@@ -6,13 +6,14 @@ module Docs
         name.remove! 'XPath.'
         name.remove! 'XSLT.'
         name.remove! 'Axes.'
-        name.prepend 'xsl:' if slug =~ /\AXSLT\/[a-z]/
+        name.remove! 'Element.'
+        name.prepend 'xsl:' if slug =~ /XSLT\/Element/
         name << '()' if name.gsub!('Functions.', '')
         name
       end
 
       def get_type
-        if slug =~ /\AXSLT\/[a-z]/
+        if slug =~ /XSLT\/Element/
           'XSLT Elements'
         elsif slug.start_with?('XPath/Axes')
           'XPath Axes'

+ 1 - 1
lib/docs/scrapers/mdn/css.rb

@@ -4,7 +4,7 @@ module Docs
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/CSS'
     self.root_path = '/Reference'
 
-    html_filters.push 'css/clean_html', 'css/entries', 'title'
+    html_filters.push 'css/clean_html', 'css/entries'
 
     options[:root_title] = 'CSS'
 

+ 1 - 3
lib/docs/scrapers/mdn/dom.rb

@@ -4,11 +4,9 @@ module Docs
     self.name = 'DOM'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/API'
 
-    html_filters.push 'dom/clean_html', 'dom/entries', 'title'
+    html_filters.push 'dom/clean_html', 'dom/entries'
 
     options[:root_title] = 'DOM'
 
-    # self.release = '2021-04-29 21:55'
-
   end
 end

+ 0 - 2
lib/docs/scrapers/mdn/html.rb

@@ -29,7 +29,5 @@ module Docs
       url
     end
 
-    # self.release = '2021-04-29 23:00'
-
   end
 end

+ 1 - 1
lib/docs/scrapers/mdn/javascript.rb

@@ -6,7 +6,7 @@ module Docs
     self.name = 'JavaScript'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference'
 
-    html_filters.push 'javascript/clean_html', 'javascript/entries', 'title'
+    html_filters.push 'javascript/clean_html', 'javascript/entries'
 
     options[:root_title] = 'JavaScript'
 

+ 1 - 1
lib/docs/scrapers/mdn/mdn.rb

@@ -3,7 +3,7 @@ module Docs
     self.abstract = true
     self.type = 'mdn'
 
-    html_filters.push 'mdn/clean_html'
+    html_filters.push 'mdn/clean_html', 'mdn/compat_tables'
 
     options[:container] = '#content > .main-page-content'
     options[:trailing_slash] = false

+ 1 - 1
lib/docs/scrapers/mdn/svg.rb

@@ -6,7 +6,7 @@ module Docs
     self.name = 'SVG'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/SVG'
 
-    html_filters.push 'svg/clean_html', 'svg/entries', 'title'
+    html_filters.push 'svg/clean_html', 'svg/entries'
 
     options[:root_title] = 'SVG'
 

+ 1 - 5
lib/docs/scrapers/mdn/xslt_xpath.rb

@@ -6,15 +6,11 @@ module Docs
     self.root_path = '/XSLT'
     self.initial_paths = %w(/XPath)
 
-    html_filters.push 'xslt_xpath/clean_html', 'xslt_xpath/entries', 'title'
+    html_filters.push 'xslt_xpath/clean_html', 'xslt_xpath/entries'
 
     options[:root_title] = 'XSLT'
 
     options[:only_patterns] = [/\A\/XSLT/, /\A\/XPath/]
 
-    options[:fix_urls] = ->(url) do
-      url.sub! 'https://developer.mozilla.org/en-US/docs/Web/XSLT/Element', "#{XsltXpath.base_url}/XSLT"
-      url
-    end
   end
 end