forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrust.rb
More file actions
65 lines (54 loc) · 2.03 KB
/
Copy pathrust.rb
File metadata and controls
65 lines (54 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
module Docs
class Rust < UrlScraper
self.type = 'rust'
self.release = '1.88.0'
self.base_url = 'https://doc.rust-lang.org/'
self.root_path = 'book/index.html'
self.initial_paths = %w(
reference/introduction.html
std/index.html
error_codes/error-index.html)
self.links = {
home: 'https://www.rust-lang.org/',
code: 'https://github.com/rust-lang/rust'
}
html_filters.push 'rust/entries', 'rust/clean_html'
options[:only_patterns] = [
/\Abook\//,
/\Areference\//,
/\Acollections\//,
/\Astd\//,
/\Aerror_codes\//, ]
options[:skip] = %w(book/README.html book/ffi.html)
options[:skip_patterns] = [/(?<!\.html)\z/, /\/print\.html/, /\Abook\/second-edition\//]
options[:fix_urls] = ->(url) do
url.sub! %r{(#{Rust.base_url}.+/)\z}, '\1index.html'
url.sub! "#{Rust.base_url}nightly/", Rust.base_url
url.sub! '/unicode/u_str', '/unicode/str/'
url.sub! '/std/std/', '/std/'
url
end
options[:attribution] = <<-HTML
© 2010 The Rust Project Developers<br>
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
HTML
def get_latest_version(opts)
doc = fetch_doc('https://www.rust-lang.org/', opts)
label = doc.at_css('.button-download + p > a').content
label.sub(/Version /, '')
end
private
REDIRECT_RGX = /http-equiv="refresh"/i
NOT_FOUND_RGX = /<title>Not Found<\/title>/
def process_response?(response)
!(response.body =~ REDIRECT_RGX || response.body =~ NOT_FOUND_RGX || response.body.blank?)
end
def parse(response) # Hook here because Nokogori removes whitespace from headings
response.body.gsub! %r{<h[1-6] class="code-header">}, '<pre class="code-header">'
# And the reference uses whitespace for indentation in grammar definitions
response.body.gsub! %r{<div class="grammar-container">([\W\w]+?)</div>}, '<pre class="grammar-container">\1</pre>'
super
end
end
end