소스 검색

Update Kotlin documentation (1.6.0)

Simon Legner 4 년 전
부모
커밋
1378b3878e
3개의 변경된 파일38개의 추가작업 그리고 22개의 파일을 삭제
  1. 7 1
      lib/docs/filters/kotlin/clean_html.rb
  2. 8 7
      lib/docs/filters/kotlin/entries.rb
  3. 23 14
      lib/docs/scrapers/kotlin.rb

+ 7 - 1
lib/docs/filters/kotlin/clean_html.rb

@@ -2,7 +2,6 @@ module Docs
   class Kotlin
     class CleanHtmlFilter < Filter
       def call
-        @doc = at_css('.page-content')
         subpath.start_with?('api') ? api_page : doc_page
         doc
       end
@@ -14,6 +13,13 @@ module Docs
           node.parent.before(node.parent.content).remove
         end
 
+        css('div.code-block').each do |node|
+          node.name = 'pre'
+          node['data-language'] = node['data-lang']
+          node.content = node.content
+          # FIXME: newlines in code-block are lost because of <div>? (on https://kotlinlang.org/docs/typecasts.html for instance)
+        end
+
         css('pre').each do |node|
           node['data-language'] = 'kotlin' if node.at_css('code.language-kotlin')
           node['data-language'] = 'groovy' if node.at_css('code.language-groovy')

+ 8 - 7
lib/docs/filters/kotlin/entries.rb

@@ -6,25 +6,26 @@ module Docs
           breadcrumbs[1..-1].join('.')
         else
           node = (at_css('h1') || at_css('h2'))
-          return node.content unless node.nil?
-          subpath[/\/([a-z0-9_-]+)\./][1..-2].titleize.sub('Faq', 'FAQ')
+          return [breadcrumbs[1..], [node.content]].flatten.join(': ') unless node.nil?
         end
       end
 
       def get_type
         if subpath.start_with?('api')
           breadcrumbs[1]
-        elsif subpath.start_with?('docs/tutorials')
-          'Tutorials'
-        elsif subpath.start_with?('docs/reference')
-          'Reference'
+        else
+          breadcrumbs[0]
         end
       end
 
       private
 
       def breadcrumbs
-        @breadcrumbs ||= css('.api-docs-breadcrumbs a').map(&:content).map(&:strip)
+        if subpath.start_with?('api')
+          @breadcrumbs ||= css('.api-docs-breadcrumbs a').map(&:content).map(&:strip)
+        else
+          @breadcrumbs ||= doc.document.at_css('body')['data-breadcrumbs'].split('///')
+        end 
       end
     end
   end

+ 23 - 14
lib/docs/scrapers/kotlin.rb

@@ -1,7 +1,6 @@
 module Docs
   class Kotlin < UrlScraper
     self.type = 'kotlin'
-    self.release = '1.4.10'
     self.base_url = 'https://kotlinlang.org/'
     self.root_path = 'api/latest/jvm/stdlib/index.html'
     self.links = {
@@ -11,9 +10,8 @@ module Docs
 
     html_filters.push 'kotlin/entries', 'kotlin/clean_html'
 
-    options[:container] = '.global-content'
-
-    options[:only_patterns] = [/\Adocs\/tutorials\//, /\Adocs\/reference\//, /\Aapi\/latest\/jvm\/stdlib\//]
+    options[:container] = 'article'
+    options[:only_patterns] = [/\Adocs\//, /\Aapi\/latest\/jvm\/stdlib\//]
     options[:skip_patterns] = [/stdlib\/org\./]
     options[:skip] = %w(
       api/latest/jvm/stdlib/alltypes/index.html
@@ -22,23 +20,34 @@ module Docs
       docs/events.html
       docs/resources.html
       docs/reference/grammar.html)
-    options[:replace_paths] = {
-      'api/latest/jvm/stdlib/' => 'api/latest/jvm/stdlib/index.html',
-      'docs/reference/coroutines.html' => 'docs/reference/coroutines-overview.html',
-      'api/latest/jvm/stdlib/kotlin/fold.html' => 'api/latest/jvm/stdlib/kotlin.collections/fold.html',
-      'api/latest/jvm/stdlib/kotlin/get-or-else.html' => 'api/latest/jvm/stdlib/kotlin.collections/get-or-else.html',
-      'api/latest/jvm/stdlib/kotlin/map.html' => 'api/latest/jvm/stdlib/kotlin.collections/map.html',
-      'docs/tutorials/native/targeting-multiple-platforms.html' => 'docs/tutorials/native/basic-kotlin-native-app.html',
-      'api/latest/jvm/stdlib/kotlin/-throwable/print-stack-trace.html' => 'api/latest/jvm/stdlib/kotlin/print-stack-trace.html',
-    }
+
+    options[:fix_urls] = ->(url) do
+      url.sub! %r{/docs/reference/}, '/docs/'
+      url
+    end
 
     options[:attribution] = <<-HTML
-      &copy; 2010&ndash;2020 JetBrains s.r.o. and Kotlin Programming Language contributors<br>
+      &copy; 2010&ndash;2021 JetBrains s.r.o. and Kotlin Programming Language contributors<br>
       Licensed under the Apache License, Version 2.0.
     HTML
 
+    version '1.6' do
+      self.release = '1.6.0'
+    end
+
+    version '1.4' do
+      self.release = '1.4.10'
+    end
+
     def get_latest_version(opts)
       get_latest_github_release('JetBrains', 'kotlin', opts)
     end
+
+    private
+
+    def process_response?(response)
+      return false unless super
+      response.body !~ /http-equiv="refresh"/i
+    end
   end
 end