angular.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require 'yajl/json_gem'
  2. module Docs
  3. class Angular < UrlScraper
  4. self.type = 'angular'
  5. self.links = {
  6. home: 'https://angular.io/',
  7. code: 'https://github.com/angular/angular'
  8. }
  9. options[:max_image_size] = 256_000
  10. options[:attribution] = <<-HTML
  11. &copy; 2010&ndash;2017 Google, Inc.<br>
  12. Licensed under the Creative Commons Attribution License 4.0.
  13. HTML
  14. version do
  15. self.release = '4.4.6'
  16. self.base_url = 'https://angular.io/'
  17. self.root_path = 'docs'
  18. html_filters.push 'angular/clean_html', 'angular/entries'
  19. options[:follow_links] = false
  20. options[:only_patterns] = [/\Aguide/, /\Atutorial/, /\Aapi/]
  21. options[:fix_urls_before_parse] = ->(url) do
  22. url.sub! %r{\Aguide/}, '/guide/'
  23. url.sub! %r{\Atutorial/}, '/tutorial/'
  24. url.sub! %r{\Aapi/}, '/api/'
  25. url.sub! %r{\Agenerated/}, '/generated/'
  26. url
  27. end
  28. private
  29. def initial_urls
  30. initial_urls = []
  31. Request.run 'https://angular.io/generated/navigation.json' do |response|
  32. data = JSON.parse(response.body)
  33. dig = ->(entry) do
  34. initial_urls << url_for("generated/docs/#{entry['url']}.json") if entry['url'] && entry['url'] != 'api'
  35. entry['children'].each(&dig) if entry['children']
  36. end
  37. data['SideNav'].each(&dig)
  38. end
  39. Request.run 'https://angular.io/generated/docs/api/api-list.json' do |response|
  40. data = JSON.parse(response.body)
  41. dig = ->(entry) do
  42. initial_urls << url_for("generated/docs/#{entry['path']}.json") if entry['path']
  43. initial_urls << url_for("generated/docs/api/#{entry['name']}.json") if entry['name'] && !entry['path']
  44. entry['items'].each(&dig) if entry['items']
  45. end
  46. data.each(&dig)
  47. end
  48. initial_urls
  49. end
  50. def handle_response(response)
  51. if response.mime_type.include?('json')
  52. response.options[:response_body] = JSON.parse(response.body)['contents']
  53. response.headers['Content-Type'] = 'text/html'
  54. response.url.path = response.url.path.sub('/generated/docs/', '/').remove('.json')
  55. response.effective_url.path = response.effective_url.path.sub('/generated/docs/', '/').remove('.json')
  56. end
  57. super
  58. end
  59. end
  60. version '2' do
  61. self.release = '2.4.10'
  62. self.base_url = 'https://v2.angular.io/docs/ts/latest/'
  63. self.root_path = 'api/'
  64. html_filters.push 'angular/entries_v2', 'angular/clean_html_v2'
  65. stub 'api/' do
  66. base_url = URL.parse(self.base_url)
  67. capybara = load_capybara_selenium
  68. capybara.app_host = base_url.origin
  69. capybara.visit(base_url.path + 'api/')
  70. capybara.execute_script('return document.body.innerHTML')
  71. end
  72. options[:skip_patterns] = [/deprecated/, /VERSION-let/]
  73. options[:skip] = %w(
  74. index.html
  75. styleguide.html
  76. quickstart.html
  77. cheatsheet.html
  78. guide/cheatsheet.html
  79. guide/style-guide.html)
  80. options[:replace_paths] = {
  81. 'testing/index.html' => 'guide/testing.html',
  82. 'guide/glossary.html' => 'glossary.html',
  83. 'tutorial' => 'tutorial/',
  84. 'api' => 'api/'
  85. }
  86. options[:fix_urls] = -> (url) do
  87. url.sub! %r{\A(https://(?:v2\.)?angular\.io/docs/.+/)index\.html\z}, '\1'
  88. url
  89. end
  90. end
  91. private
  92. def parse(response)
  93. response.body.gsub! '<code-example', '<pre'
  94. response.body.gsub! '</code-example', '</pre'
  95. response.body.gsub! '<code-pane', '<pre'
  96. response.body.gsub! '</code-pane', '</pre'
  97. response.body.gsub! '<live-example></live-example>', 'live example'
  98. response.body.gsub! '<live-example', '<span'
  99. response.body.gsub! '</live-example', '</span'
  100. super
  101. end
  102. end
  103. end