From 8d959592b4a22c10011f605134ceeb27962c447e Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sat, 26 Mar 2016 17:58:38 -0400 Subject: [PATCH] Update & improve Angular documentation (1.5.3) Fixes #352. --- lib/app.rb | 1 + lib/docs/filters/angular/clean_html.rb | 4 +++- lib/docs/filters/angular/clean_urls.rb | 4 ++-- lib/docs/filters/angular/entries.rb | 31 ++++++++++++++++---------- lib/docs/scrapers/angular.rb | 31 +++++++++++++++++++------- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index 9eefc5fd..d8d05333 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -278,6 +278,7 @@ class App < Sinatra::Application get %r{\A/([\w~\.]+)(\-[\w\-]+)?(/.*)?\z} do |doc, type, rest| return redirect "/#{DOC_REDIRECTS[doc]}#{type}#{rest}" if DOC_REDIRECTS.key?(doc) + return redirect "/angular/api#{rest}", 301 if doc == 'angular' && rest.start_with?('/ng') return 404 unless @doc = find_doc(doc) if rest.nil? diff --git a/lib/docs/filters/angular/clean_html.rb b/lib/docs/filters/angular/clean_html.rb index 44ffcfbc..cc897bb6 100644 --- a/lib/docs/filters/angular/clean_html.rb +++ b/lib/docs/filters/angular/clean_html.rb @@ -36,7 +36,9 @@ module Docs node.before(node.children).remove end - at_css('h1').prepend_child(css('.view-source', '.improve-docs')) + if h1 = at_css('h1') + h1.prepend_child(css('.view-source', '.improve-docs')) + end # Remove root-level
while div = at_css('h1 + div') diff --git a/lib/docs/filters/angular/clean_urls.rb b/lib/docs/filters/angular/clean_urls.rb index 3603b73a..09e93a85 100644 --- a/lib/docs/filters/angular/clean_urls.rb +++ b/lib/docs/filters/angular/clean_urls.rb @@ -2,8 +2,8 @@ module Docs class Angular class CleanUrlsFilter < Filter def call - html.gsub! "angularjs.org/#{Angular.release}/docs/partials/api/", "angularjs.org/#{Angular.release}/docs/api/" - html.gsub! %r{angularjs.org/#{Angular.release}/docs/api/(.+?)\.html}, "angularjs.org/#{Angular.release}/docs/api/\1" + html.gsub! %r{angularjs\.org/([\d\.]+)/docs/partials/(\w+)/}, 'angularjs.org/\1/docs/\2/' + html.gsub! %r{angularjs\.org/([\d\.]+)/docs/(\w+)/(.+?)\.html}, 'angularjs.org/\1/docs/\2/\3' html end end diff --git a/lib/docs/filters/angular/entries.rb b/lib/docs/filters/angular/entries.rb index 761d14c1..ce7a2975 100644 --- a/lib/docs/filters/angular/entries.rb +++ b/lib/docs/filters/angular/entries.rb @@ -2,17 +2,27 @@ module Docs class Angular class EntriesFilter < Docs::EntriesFilter def get_name - name = slug.split('/').last - name.remove! %r{\Ang\.} - name << " (#{subtype})" if subtype == 'directive' || subtype == 'filter' - name.prepend("#{type}.") unless type.starts_with?('ng ') || name == type - name + if slug.start_with?('api') + name = URI.unescape(slug).split('/').last + name.remove! %r{\Ang\.} + name << " (#{subtype})" if subtype == 'directive' || subtype == 'filter' + name.prepend("#{type}.") unless type.starts_with?('ng ') || name == type + name + elsif slug.start_with?('guide') + name = URI.decode(at_css('.improve-docs')['href'][/message=docs\(guide%2F(.+?)\)/, 1]) + name.prepend 'Guide: ' + name + end end def get_type - type = slug.split('/').first - type << " #{subtype}s" if type == 'ng' && subtype - type + if slug.start_with?('api') + type = slug.split('/').drop(1).first + type << " #{subtype}s" if type == 'ng' && subtype + type + elsif slug.start_with?('guide') + 'Guide' + end end def subtype @@ -23,6 +33,7 @@ module Docs end def additional_entries + return [] unless slug.start_with?('api') entries = [] css('ul.defs').each do |list| @@ -37,10 +48,6 @@ module Docs entries end - - def include_default_entry? - !at_css('h1 + .definition-table:last-child') - end end end end diff --git a/lib/docs/scrapers/angular.rb b/lib/docs/scrapers/angular.rb index d3e48a97..d1cf82b2 100644 --- a/lib/docs/scrapers/angular.rb +++ b/lib/docs/scrapers/angular.rb @@ -5,8 +5,10 @@ module Docs self.name = 'Angular.js' self.slug = 'angular' self.type = 'angular' - self.release = '1.5.0' - self.base_url = "https://code.angularjs.org/#{release}/docs/partials/api/" + self.release = '1.5.3' + self.base_url = "https://code.angularjs.org/#{release}/docs/partials/" + self.root_path = 'api.html' + self.initial_paths = %w(guide.html) html_filters.push 'angular/clean_html', 'angular/entries', 'title' text_filters.push 'angular/clean_urls' @@ -14,14 +16,25 @@ module Docs options[:title] = false options[:root_title] = 'Angular.js' + options[:decode_and_clean_paths] = true + options[:fix_urls_before_parse] = ->(str) do + str.gsub!('[', '%5B') + str.gsub!(']', '%5D') + str + end + options[:fix_urls] = ->(url) do - url.sub! '/partials/api/api/', '/partials/api/' - url.sub! %r{/api/(.+?)/api/}, '/api/' - url.sub! %r{/partials/api/(.+?)(? @@ -31,9 +44,11 @@ module Docs private def root_page_body - require 'capybara' + require 'capybara/dsl' Capybara.current_driver = :selenium - Capybara.visit("https://code.angularjs.org/#{self.class.release}/docs/api") + Capybara.run_server = false + Capybara.app_host = 'https://code.angularjs.org' + Capybara.visit("/#{self.class.release}/docs/api") Capybara.find('.side-navigation')['innerHTML'] end end