فهرست منبع

Decouple React Native scraper from React scraper

Thibaut Courouble 8 سال پیش
والد
کامیت
1181bd7498

+ 1 - 1
assets/stylesheets/application-dark.css.scss

@@ -79,7 +79,7 @@
         'pages/pug',
         'pages/ramda',
         'pages/rdoc',
-        'pages/react',
+        'pages/react_native',
         'pages/redis',
         'pages/rethinkdb',
         'pages/rfc',

+ 1 - 1
assets/stylesheets/application.css.scss

@@ -79,7 +79,7 @@
         'pages/pug',
         'pages/ramda',
         'pages/rdoc',
-        'pages/react',
+        'pages/react_native',
         'pages/redis',
         'pages/rethinkdb',
         'pages/rfc',

+ 0 - 13
assets/stylesheets/pages/_react.scss

@@ -1,13 +0,0 @@
-._react {
-  > h2 { @extend %block-heading; }
-  > h3 { @extend %block-label, %label-blue; }
-  > h4 { @extend %block-label; }
-
-  p code, li code { @extend %label; }
-  blockquote, .deprecated { @extend %note; }
-  .deprecated { @extend %note-orange; }
-  .deprecatedTitle { font-weight: $boldFontWeight; }
-
-  span.platform { float: right; }
-  span.propType, span.platform { font-weight: normal; }
-}

+ 9 - 0
assets/stylesheets/pages/_react_native.scss

@@ -0,0 +1,9 @@
+._react_native {
+  @extend %simple;
+
+  .deprecated { @extend %note, %note-orange; }
+  .deprecatedTitle { font-weight: $boldFontWeight; }
+
+  span.platform { float: right; }
+  span.propType, span.platform { font-weight: normal; }
+}

+ 51 - 0
lib/docs/filters/react_native/clean_html.rb

@@ -2,10 +2,61 @@ module Docs
   class ReactNative
     class CleanHtmlFilter < Filter
       def call
+        @doc = at_css('.inner-content, article.withtoc')
+
         if root_page?
+          at_css('h1').content = 'React Native Documentation'
           css('h1 ~ *').remove
         end
 
+        css('.docs-prevnext', '.hash-link', '.edit-page-link', '.edit-github', 'a.hash', '.edit-page-block', 'a.show', 'a.hide', 'hr').remove
+
+        css('table h1', 'table h2', 'table h3').each do |node|
+          table = node
+          table = table.parent until table.name == 'table'
+          table.replace(node)
+        end
+
+        css('a.anchor', 'a.hashref').each do |node|
+          node.parent['id'] ||= node['name'] || node['id']
+        end
+
+        css('.highlight').each do |node|
+          node.name = 'pre'
+          node.css('.gutter').remove
+          node['data-language'] = node.at_css('[data-lang]').try(:[], 'data-lang') || 'js'
+          node.content = node.content.strip
+        end
+
+        css('table.highlighttable').each do |node|
+          node.replace(node.at_css('pre.highlight'))
+        end
+
+        css('.prism').each do |node|
+          node.name = 'pre'
+          node['data-language'] = node['class'][/(?<=language\-)(\w+)/]
+          node.content = node.content
+        end
+
+        css('blockquote > p:first-child').each do |node|
+          node.remove if node.content.strip == 'Note:'
+        end
+
+        css('h3#props', 'h3#methods').each { |node| node.name = 'h2' }
+        css('h4.propTitle').each { |node| node.name = 'h3' }
+
+        css('> div > div', '> div', 'div > span', '.props', '.prop').each do |node|
+          node.before(node.children).remove
+        end
+
+        css('a pre', 'h3 .propType').each do |node|
+          node.name = 'code'
+        end
+
+        css('a[target]').each do |node|
+          node.remove_attribute('target')
+        end
+
         css('center > .button', 'p:contains("short survey")', 'iframe', '.embedded-simulator', '.deprecatedIcon').remove
 
         css('h4.methodTitle').each do |node|

+ 1 - 17
lib/docs/filters/react_native/entries.rb

@@ -1,7 +1,6 @@
 module Docs
   class ReactNative
     class EntriesFilter < Docs::EntriesFilter
-
       REPLACE_TYPES = {
         'The Basics' => 'Getting Started',
         'apis' => 'APIs',
@@ -23,9 +22,7 @@ module Docs
       end
 
       def additional_entries
-        entries = []
-
-        css('.props > .prop > .propTitle', '.props > .prop > .methodTitle').each do |node| # react-native
+        css('.props > .prop > .propTitle', '.props > .prop > .methodTitle').each_with_object [] do |node, entries|
           name = node.children.find(&:text?).try(:content)
           next if name.blank?
           sep = node.content.include?('static') ? '.' : '#'
@@ -35,19 +32,6 @@ module Docs
           id = node.at_css('.anchor')['name']
           entries << [name, id]
         end
-
-        css('.apiIndex a pre').each do |node| # relay
-          next unless node.parent['href'].start_with?('#')
-          id = node.parent['href'].remove('#')
-          name = node.content.strip
-          sep = name.start_with?('static') ? '.' : '#'
-          name.remove! %r{(abstract|static) }
-          name.sub! %r{\(.*\)}, '()'
-          name.prepend(self.name + sep)
-          entries << [name, id]
-        end
-
-        entries
       end
     end
   end

+ 4 - 6
lib/docs/scrapers/react_native.rb

@@ -1,8 +1,7 @@
 module Docs
-  class ReactNative < React
-    self.name = 'React Native'
+  class ReactNative < UrlScraper
     self.slug = 'react_native'
-    self.type = 'react'
+    self.type = 'react_native'
     self.release = '0.49'
     self.base_url = 'https://facebook.github.io/react-native/docs/'
     self.root_path = 'getting-started.html'
@@ -11,10 +10,9 @@ module Docs
       code: 'https://github.com/facebook/react-native'
     }
 
-    html_filters.replace 'react/entries', 'react_native/entries'
-    html_filters.push 'react_native/clean_html'
+    html_filters.push 'react_native/entries', 'react_native/clean_html'
 
-    options[:root_title] = 'React Native Documentation'
+    options[:container] = '.documentationContent'
     options[:skip_patterns] = [/\Asample\-/]
     options[:skip] = %w(
       videos.html