From 1f2030de2427933fb95ce10391a881563e3317bd Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Thu, 1 Feb 2018 16:13:10 -0500 Subject: [PATCH] Add Babel --- assets/stylesheets/application-dark.css.scss | 1 + assets/stylesheets/application.css.scss | 1 + assets/stylesheets/pages/_babel.scss | 10 +++ lib/docs/filters/babel/clean_html.rb | 76 ++++++++++++++++++++ lib/docs/filters/babel/entries.rb | 31 ++++++++ lib/docs/scrapers/babel.rb | 31 ++++++++ 6 files changed, 150 insertions(+) create mode 100644 assets/stylesheets/pages/_babel.scss create mode 100644 lib/docs/filters/babel/clean_html.rb create mode 100644 lib/docs/filters/babel/entries.rb create mode 100644 lib/docs/scrapers/babel.rb diff --git a/assets/stylesheets/application-dark.css.scss b/assets/stylesheets/application-dark.css.scss index c8dc1f8f..d0abeb4e 100644 --- a/assets/stylesheets/application-dark.css.scss +++ b/assets/stylesheets/application-dark.css.scss @@ -35,6 +35,7 @@ 'pages/angularjs', 'pages/apache', 'pages/async', + 'pages/babel', 'pages/bootstrap', 'pages/c', 'pages/cakephp', diff --git a/assets/stylesheets/application.css.scss b/assets/stylesheets/application.css.scss index 26e0b885..a18e933d 100644 --- a/assets/stylesheets/application.css.scss +++ b/assets/stylesheets/application.css.scss @@ -35,6 +35,7 @@ 'pages/angularjs', 'pages/apache', 'pages/async', + 'pages/babel', 'pages/bootstrap', 'pages/c', 'pages/cakephp', diff --git a/assets/stylesheets/pages/_babel.scss b/assets/stylesheets/pages/_babel.scss new file mode 100644 index 00000000..3980bf47 --- /dev/null +++ b/assets/stylesheets/pages/_babel.scss @@ -0,0 +1,10 @@ +._babel { + @extend %simple; + ._note { + h1, h2, h3, h4, h5, h6 { + &:first-child { + margin: 0.5em 0; + } + } + } +} diff --git a/lib/docs/filters/babel/clean_html.rb b/lib/docs/filters/babel/clean_html.rb new file mode 100644 index 00000000..ff46f386 --- /dev/null +++ b/lib/docs/filters/babel/clean_html.rb @@ -0,0 +1,76 @@ +module Docs + class Babel + class CleanHtmlFilter < Filter + def call + css('.btn-clipboard').remove + + css('div.highlighter-rouge').each do |node| + pre = node.at_css('pre') + + # copy over the highlighting metadata + match = /language-(\w+)/.match(node['class']) + if match + lang = match[1] + if lang == 'sh' + lang = 'bash' + end + pre['class'] = nil + pre['data-language'] = lang + end + + # Remove the server-rendered syntax highlighting + code = pre.at_css('code') + code.content = code.text + + # Remove the div.highlighter-rouge and div.highlight wrapping the
+          node.add_next_sibling pre
+          node.remove
+        end
+
+
+        css('blockquote').each do |node|
+          node.name = 'div'
+          node['class'] = '_note'
+        end
+
+        css((1..6).map { |n| "h#{n}" }).each do |header|
+          return unless header.at_css('a')
+          header.content = header.at_css('a').content
+        end
+
+
+        header = doc # .docs-content
+          .parent # .row
+          .parent # .container
+          .previous_element # .docs_header
+
+        toc = doc # .docs-content
+          .parent # .row
+          .at_css('.sidebar')
+        toc['class'] = '_toc'
+        toc.css('a').each do |a|
+          a['class'] = '_toc-link'
+          a.parent.remove if a.content == 'Community Discussion'
+        end
+        toc.css('ul').attr 'class', '_toc-list'
+
+        h1 = header.at_css('h1')
+        h1.content = h1.content
+          .titleize
+          .sub(/\bEnv\b/, 'env')
+          .sub(/\.[A-Z]/) { |s| s.downcase }
+          .sub(/\.babelrc/i, '.babelrc')
+          .sub('Common Js', 'CommonJS')
+          .sub('J Script', 'JScript')
+          .sub(/regexp/i, 'RegExp')
+          .sub(/api|Es(\d+)|cli|jsx?|[au]md/i) { |s| s.upcase }
+
+        doc.children.before toc
+        doc.children.before header.at_css 'p'
+        doc.children.before h1
+
+        doc
+      end
+    end
+  end
+end
diff --git a/lib/docs/filters/babel/entries.rb b/lib/docs/filters/babel/entries.rb
new file mode 100644
index 00000000..98180b34
--- /dev/null
+++ b/lib/docs/filters/babel/entries.rb
@@ -0,0 +1,31 @@
+module Docs
+  class Babel
+    class EntriesFilter < Docs::EntriesFilter
+      def get_name
+        at_css('h1').content.sub /^(minify|syntax)|(transform|preset)$/i, ''
+      end
+
+      def get_type
+        if subpath.start_with? 'plugins/preset'
+          'Presets'
+        elsif subpath.start_with? 'plugins/transform'
+          'Transform Plugins'
+        elsif subpath.start_with? 'plugins/minify'
+          'Minification'
+        elsif subpath.start_with? 'plugins/syntax'
+          'Syntax Plugins'
+        elsif subpath.start_with? 'plugins'
+          'Plugins'
+        elsif subpath.start_with? 'usage/'
+          'Usage'
+        else
+          'Docs'
+        end
+      end
+
+      def path
+        super
+      end
+    end
+  end
+end
diff --git a/lib/docs/scrapers/babel.rb b/lib/docs/scrapers/babel.rb
new file mode 100644
index 00000000..39495dcf
--- /dev/null
+++ b/lib/docs/scrapers/babel.rb
@@ -0,0 +1,31 @@
+module Docs
+  class Babel < UrlScraper
+    self.type = 'babel'
+    self.base_url = 'http://babeljs.io/docs/'
+    self.root_path = '/plugins/'
+    self.release = '6.26.0'
+    self.initial_paths = %w[faq tour usage/babel-register core-packages editors usage/caveats]
+    self.links = {
+      home: 'https://babeljs.io/',
+      code: 'https://github.com/babel/babel'
+    }
+
+    html_filters.push 'babel/clean_html', 'babel/entries'
+
+    options[:trailing_slash] = true
+    options[:container] = '.docs-content'
+    options[:skip] = %w{setup/ community/videos/}
+    options[:fix_urls] = ->(url) do
+      return url unless url.start_with? self.base_url
+      url.sub %r{/(index\.\w+)?$}, ''
+    end
+
+    options[:attribution] = <<-HTML
+      © 2018 Sebastian McKenzie
+ Licensed under the + + MIT License + + HTML + end +end