Преглед на файлове

Merge pull request #170 from TrangPham/master

Adding Official Url for url scrapped documentations
Thibaut Courouble преди 10 години
родител
ревизия
4b9256a731
променени са 6 файла, в които са добавени 57 реда и са изтрити 3 реда
  1. 17 0
      assets/stylesheets/components/_page.scss
  2. 1 1
      lib/docs/core/doc.rb
  3. 4 0
      lib/docs/core/filter.rb
  4. 6 2
      lib/docs/core/scraper.rb
  5. 16 0
      lib/docs/filters/core/home_url.rb
  6. 13 0
      test/lib/docs/core/doc_test.rb

+ 17 - 0
assets/stylesheets/components/_page.scss

@@ -22,6 +22,23 @@
   }
 }
 
+//
+// Official Website box
+//
+
+._official {
+  clear: both;
+  margin: 2rem 0 1.5rem;
+  font-size: .75rem;
+  color: $textColorLight;
+  text-align: center;
+  -webkit-font-smoothing: subpixel-antialiased;
+
+  & + & { margin-top: 1.5rem; }
+}
+
+._official-link { @extend %external-link; }
+
 //
 // Attribution box
 //

+ 1 - 1
lib/docs/core/doc.rb

@@ -4,7 +4,7 @@ module Docs
     DB_FILENAME = 'db.json'
 
     class << self
-      attr_accessor :name, :slug, :type, :version, :abstract
+      attr_accessor :name, :slug, :type, :version, :abstract, :home_url
 
       def inherited(subclass)
         subclass.type = type

+ 4 - 0
lib/docs/core/filter.rb

@@ -20,6 +20,10 @@ module Docs
       context[:base_url]
     end
 
+    def home_url
+      context[:home_url]
+    end
+
     def current_url
       context[:url]
     end

+ 6 - 2
lib/docs/core/scraper.rb

@@ -34,7 +34,7 @@ module Docs
     self.text_filters = FilterStack.new
 
     html_filters.push 'container', 'clean_html', 'normalize_urls', 'internal_urls', 'normalize_paths'
-    text_filters.push 'inner_html', 'clean_text', 'attribution'
+    text_filters.push 'inner_html', 'clean_text', 'home_url', 'attribution'
 
     def build_page(path)
       response = request_one url_for(path)
@@ -65,6 +65,10 @@ module Docs
       @root_url ||= root_path? ? URL.parse(File.join(base_url.to_s, root_path)) : base_url.normalize
     end
 
+    def home_url
+      @home_url ||= self.class.home_url
+    end
+
     def root_path
       self.class.root_path
     end
@@ -89,7 +93,7 @@ module Docs
 
     def options
       @options ||= self.class.options.deep_dup.tap do |options|
-        options.merge! base_url: base_url, root_url: root_url,
+        options.merge! base_url: base_url, root_url: root_url, home_url: home_url,
                        root_path: root_path, initial_paths: initial_paths
 
         if root_path?

+ 16 - 0
lib/docs/filters/core/home_url.rb

@@ -0,0 +1,16 @@
+module Docs
+  class HomeUrlFilter < Filter
+    def call
+      html.prepend(home_url_html) if home_url
+      html
+    end
+
+    def home_url_html
+      <<-HTML.strip_heredoc
+      <div class="_official">
+        Official Documentation: <a href="#{home_url}" class="_official-link">#{home_url}</a>
+      </div>
+      HTML
+    end
+  end
+end

+ 13 - 0
test/lib/docs/core/doc_test.rb

@@ -60,6 +60,19 @@ class DocsDocTest < MiniTest::Spec
     end
   end
 
+  describe ".home_url" do
+    it "returns nil" do
+      assert_nil doc.home_url
+    end
+  end
+
+  describe ".home_url=" do
+    it "stores .home_url" do
+      doc.home_url = 'http://www.url.com/doc'
+      assert_equal 'http://www.url.com/doc', doc.home_url
+    end
+  end
+  
   describe ".abstract" do
     it "returns nil" do
       assert_nil doc.abstract