rust.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. module Docs
  3. class Rust < UrlScraper
  4. self.type = 'rust'
  5. self.release = '1.75.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! "#{Rust.base_url}nightly/", Rust.base_url
  27. url.sub! '/unicode/u_str', '/unicode/str/'
  28. url.sub! '/std/std/', '/std/'
  29. url
  30. end
  31. options[:attribution] = <<-HTML
  32. &copy; 2010 The Rust Project Developers<br>
  33. Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
  34. HTML
  35. def get_latest_version(opts)
  36. doc = fetch_doc('https://www.rust-lang.org/', opts)
  37. label = doc.at_css('.button-download + p > a').content
  38. label.sub(/Version /, '')
  39. end
  40. private
  41. REDIRECT_RGX = /http-equiv="refresh"/i
  42. NOT_FOUND_RGX = /<title>Not Found<\/title>/
  43. def process_response?(response)
  44. !(response.body =~ REDIRECT_RGX || response.body =~ NOT_FOUND_RGX || response.body.blank?)
  45. end
  46. def parse(response) # Hook here because Nokogori removes whitespace from headings
  47. response.body.gsub! %r{<h[1-6] class="code-header">}, '<pre class="code-header">'
  48. super
  49. end
  50. end
  51. end