Browse Source

Convert the `decode_cloudflare_email` helper to a dedicated filter class

Jed Fox 8 years ago
parent
commit
608853bd2e

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

@@ -91,17 +91,6 @@ module Docs
       super
     end
 
-    def decode_cloudflare_email(str)
-      mask = "0x#{str[0..1]}".hex | 0
-      result = ''
-
-      str.chars.drop(2).each_slice(2) do |slice|
-        result += "%" + "0#{("0x#{slice.join}".hex ^ mask).to_s(16)}"[-2..-1]
-      end
-
-      URI.decode(result)
-    end
-
     def clean_path(path)
       path = path.gsub %r{[!;:]}, '-'
       path = path.gsub %r{\+}, '_plus_'

+ 0 - 4
lib/docs/filters/ansible/clean_html.rb

@@ -4,10 +4,6 @@ module Docs
       def call
         @doc = at_css('#page-content')
 
-        css('.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         doc
       end
     end

+ 0 - 4
lib/docs/filters/bootstrap/clean_html_v3.rb

@@ -39,10 +39,6 @@ module Docs
           node['class'] = 'col'
         end
 
-        css('.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         css('figure.highlight').each do |node|
           code = node.at_css('code')
           node['data-language'] = code['data-lang']

+ 0 - 4
lib/docs/filters/bootstrap/clean_html_v4.rb

@@ -40,10 +40,6 @@ module Docs
           node['class'] = 'col'
         end
 
-        css('.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         css('.highlight').each do |node|
           code = node.at_css('code')
           node['data-language'] = code['data-lang']

+ 21 - 0
lib/docs/filters/core/parse_cf_email.rb

@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+module Docs
+  class ParseCfEmailFilter < Filter
+    def call
+      css('.__cf_email__').each do |node|
+        str = node['data-cfemail']
+        mask = "0x#{str[0..1]}".hex | 0
+        result = ''
+
+        str.chars.drop(2).each_slice(2) do |slice|
+          result += "%" + "0#{("0x#{slice.join}".hex ^ mask).to_s(16)}"[-2..-1]
+        end
+
+        node.replace(URI.decode(result))
+      end
+
+      doc
+    end
+  end
+end

+ 0 - 4
lib/docs/filters/flow/clean_html.rb

@@ -45,10 +45,6 @@ module Docs
           end
         end
 
-        css('.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         css('.editor').each do |node|
           pre = node.at_css('.editor-code > pre')
           pre['data-language'] = 'javascript'

+ 0 - 4
lib/docs/filters/laravel/clean_html.rb

@@ -8,10 +8,6 @@ module Docs
           other
         end
 
-        css('a.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         # Remove code highlighting
         css('pre').each do |node|
           node.content = node.content

+ 0 - 4
lib/docs/filters/node/clean_html.rb

@@ -22,10 +22,6 @@ module Docs
           node.content = node.content
         end
 
-        css('.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         doc
       end
     end

+ 0 - 4
lib/docs/filters/vue/clean_html.rb

@@ -9,10 +9,6 @@ module Docs
 
         css('.demo', '.guide-links', '.footer', '#ad').remove
 
-        css('.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         # Remove code highlighting
         css('figure').each do |node|
           node.name = 'pre'

+ 0 - 4
lib/docs/filters/yarn/clean_html.rb

@@ -43,10 +43,6 @@ module Docs
           end
         end
 
-        css('.__cf_email__').each do |node|
-          node.replace(decode_cloudflare_email(node['data-cfemail']))
-        end
-
         css('div.highlighter-rouge').each do |node|
           node['data-language'] = node['class'][/language-(\w+)/, 1] if node['class']
           node.content = node.content.strip

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

@@ -7,7 +7,7 @@ module Docs
       code: 'https://github.com/ansible/ansible'
     }
 
-    html_filters.push 'ansible/entries', 'ansible/clean_html', 'sphinx/clean_html'
+    html_filters.push 'ansible/entries', 'sphinx/clean_html', 'parse_cf_email'
 
     options[:skip] = %w(
       glossary.html

+ 2 - 0
lib/docs/scrapers/bootstrap.rb

@@ -15,6 +15,8 @@ module Docs
       Documentation licensed under the Creative Commons Attribution License v3.0.
     HTML
 
+    html_filters.push 'parse_cf_email'
+
     version '4' do
       self.release = '4.0.0'
       self.base_url = 'https://getbootstrap.com/docs/4.0/'

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

@@ -8,7 +8,7 @@ module Docs
       code: 'https://github.com/facebook/flow'
     }
 
-    html_filters.push 'flow/entries', 'flow/clean_html', 'title'
+    html_filters.push 'flow/entries', 'flow/clean_html', 'title', 'parse_cf_email'
 
     options[:trailing_slash] = false
     options[:root_title] = 'Flow'

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

@@ -7,7 +7,7 @@ module Docs
       code: 'https://github.com/laravel/laravel'
     }
 
-    html_filters.push 'laravel/entries', 'laravel/clean_html'
+    html_filters.push 'laravel/entries', 'laravel/clean_html', 'parse_cf_email'
 
     options[:container] = ->(filter) {
       filter.subpath.start_with?('/api') ? '#content' : '.docs-wrapper'

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

@@ -8,7 +8,7 @@ module Docs
       code: 'https://github.com/nodejs/node'
     }
 
-    html_filters.push 'node/clean_html', 'node/entries', 'title'
+    html_filters.push 'node/clean_html', 'node/entries', 'title', 'parse_cf_email'
 
     options[:title] = false
     options[:root_title] = 'Node.js'

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

@@ -8,7 +8,7 @@ module Docs
       code: 'https://github.com/vuejs/vue'
     }
 
-    html_filters.push 'vue/entries', 'vue/clean_html'
+    html_filters.push 'vue/entries', 'vue/clean_html', 'parse_cf_email'
 
     options[:only_patterns] = [/guide\//, /api\//]
     options[:skip] = %w(guide/team.html)

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

@@ -8,7 +8,7 @@ module Docs
       code: 'https://github.com/yarnpkg/yarn'
     }
 
-    html_filters.push 'yarn/entries', 'yarn/clean_html', 'title'
+    html_filters.push 'yarn/entries', 'yarn/clean_html', 'title', 'parse_cf_email'
 
     options[:root_title] = 'Yarn'
     options[:trailing_slash] = false