forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.rb
49 lines (40 loc) · 1.52 KB
/
go.rb
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
module Docs
class Go < UrlScraper
self.type = 'go'
self.release = '1.19.0'
self.base_url = 'https://golang.org/pkg/'
self.links = {
home: 'https://golang.org/',
code: 'https://go.googlesource.com/go'
}
# Run godoc locally, since https://golang.org/pkg/ redirects to https://pkg.go.dev/std with rate limiting / scraping protection.
# docker run --expose=6060 --rm -it docker.io/golang:1.18.0
#docker# go install golang.org/x/tools/cmd/godoc@latest
#docker# rm -r /usr/local/go/test/
#docker# godoc -http 0.0.0.0:6060 -v
self.base_url = 'http://localhost:6060/pkg/'
html_filters.push 'clean_local_urls'
html_filters.push 'go/clean_html', 'go/entries'
text_filters.replace 'attribution', 'go/attribution'
options[:trailing_slash] = true
options[:container] = '#page .container'
options[:skip] = %w(runtime/msan/)
options[:skip_patterns] = [/\/\//]
options[:fix_urls] = ->(url) do
url.sub '/pkg//', '/pkg/'
end
options[:attribution] = <<-HTML
© Google, Inc.<br>
Licensed under the Creative Commons Attribution License 3.0.
HTML
def get_latest_version(opts)
doc = fetch_doc('https://go.dev/dl/', opts)
doc.at_css('.download[href]')['href'][/go1[0-9.]+[0-9]/][2..]
end
private
def parse(response) # Hook here because Nokogori removes whitespace from textareas
response.body.gsub! %r{<textarea\ class="code"[^>]*>([\W\w]+?)</textarea>}, '<pre class="code">\1</pre>'
super
end
end
end