angular.rb 4.6 KB

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