1
0

rust.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # frozen_string_literal: true
  2. module Docs
  3. class Rust < UrlScraper
  4. self.type = 'rust'
  5. self.release = '1.48.0'
  6. self.base_url = 'https://doc.rust-lang.org/'
  7. self.root_path = 'book/index.html'
  8. self.initial_paths = %w(
  9. reference/introduction.html
  10. std/index.html
  11. error-index.html)
  12. self.links = {
  13. home: 'https://www.rust-lang.org/',
  14. code: 'https://github.com/rust-lang/rust'
  15. }
  16. html_filters.push 'rust/entries', 'rust/clean_html'
  17. options[:only_patterns] = [
  18. /\Abook\//,
  19. /\Areference\//,
  20. /\Acollections\//,
  21. /\Astd\// ]
  22. options[:skip] = %w(book/README.html book/ffi.html)
  23. options[:skip_patterns] = [/(?<!\.html)\z/, /\/print\.html/, /\Abook\/second-edition\//]
  24. options[:fix_urls] = ->(url) do
  25. url.sub! %r{(#{Rust.base_url}.+/)\z}, '\1index.html'
  26. url.sub! '/unicode/u_str', '/unicode/str/'
  27. url.sub! '/std/std/', '/std/'
  28. url
  29. end
  30. options[:attribution] = <<-HTML
  31. &copy; 2010 The Rust Project Developers<br>
  32. Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
  33. HTML
  34. def get_latest_version(opts)
  35. doc = fetch_doc('https://www.rust-lang.org/', opts)
  36. label = doc.at_css('.button-download + p > a').content
  37. label.sub(/Version /, '')
  38. end
  39. private
  40. REDIRECT_RGX = /http-equiv="refresh"/i
  41. NOT_FOUND_RGX = /<title>Not Found<\/title>/
  42. def process_response?(response)
  43. !(response.body =~ REDIRECT_RGX || response.body =~ NOT_FOUND_RGX || response.body.blank?)
  44. end
  45. end
  46. end