History

Simplify file scraper setup; scrape files in the "docs/[slug]" directory

Thibaut Courouble committed 7 years ago

Commit bf003661 parent 91753ce22 files · +124 −104Browse files

Showing the changes to lib/docs (19 of 22 files). Show all

Unified · Split

19 files
  1. lib/docs/core/doc.rb +6 −0
  2. lib/docs/core/scrapers/file_scraper.rb +18 −7
  3. lib/docs/scrapers/c.rb +0 −1
  4. lib/docs/scrapers/cpp.rb +0 −1
  5. lib/docs/scrapers/dart.rb +0 −2
  6. lib/docs/scrapers/django.rb +0 −6
  7. lib/docs/scrapers/erlang.rb +0 −4
  8. lib/docs/scrapers/gnu/gcc.rb +0 −8
  9. lib/docs/scrapers/gnu/gnu_fortran.rb +0 −4
  10. lib/docs/scrapers/nokogiri2.rb +0 −1
  11. lib/docs/scrapers/numpy.rb +0 −1
  12. lib/docs/scrapers/openjdk.rb +2 −3
  13. lib/docs/scrapers/perl.rb +0 −1
  14. lib/docs/scrapers/php.rb +2 −3
  15. lib/docs/scrapers/python.rb +4 −8
  16. lib/docs/scrapers/rdoc/minitest.rb +1 −1
  17. lib/docs/scrapers/rdoc/rails.rb +0 −1
  18. lib/docs/scrapers/rdoc/ruby.rb +0 −4
  19. lib/docs/scrapers/sqlite.rb +0 −1
lib/docs/core/doc.rb+6−0
@@ -95,6 +95,9 @@ module Docs · 94 unmodified lines
9595 false
9696 end
9797 end
98+ rescue Docs::SetupError => error
99+ puts "ERROR: #{error.message}"
100+ false
98101 end
99102
100103 def store_pages(store)
@@ -118,6 +121,9 @@ module Docs · 17 unmodified lines
118121 false
119122 end
120123 end
124+ rescue Docs::SetupError => error
125+ puts "ERROR: #{error.message}"
126+ false
121127 end
122128
123129 private
lib/docs/core/scrapers/file_scraper.rb+18−7
@@ -1,14 +1,13 @@
11 module Docs
22 class FileScraper < Scraper
3+ SOURCE_DIRECTORY = File.expand_path '../../../../../docs', __FILE__
4+
35 Response = Struct.new :body, :url
46
57 class << self
6− attr_accessor :dir
7−
88 def inherited(subclass)
99 super
1010 subclass.base_url = base_url
11− subclass.dir = dir
1211 end
1312 end
1413
@@ -16,13 +15,25 @@ module Docs · 1 unmodified line
1615
1716 html_filters.push 'clean_local_urls'
1817
18+ def source_directory
19+ @source_directory ||= File.join(SOURCE_DIRECTORY, self.class.path)
20+ end
21+
1922 private
2023
24+ def assert_source_directory_exists
25+ unless Dir.exists?(source_directory)
26+ raise SetupError, "The #{self.class.name} scraper requires the original documentation files to be stored in the \"#{source_directory}\" directory."
27+ end
28+ end
29+
2130 def request_one(url)
22− Response.new read_file(file_path_for(url)), URL.parse(url)
31+ assert_source_directory_exists
32+ Response.new read_file(url_to_path(url)), URL.parse(url)
2333 end
2434
2535 def request_all(urls)
36+ assert_source_directory_exists
2637 queue = [urls].flatten
2738 until queue.empty?
2839 result = yield request_one(queue.shift)
@@ -34,12 +45,12 @@ module Docs · 5 unmodified lines
3445 response.body.present?
3546 end
3647
37− def file_path_for(url)
38− File.join self.class.dir, url.remove(base_url.to_s)
48+ def url_to_path(url)
49+ url.remove(base_url.to_s)
3950 end
4051
4152 def read_file(path)
42− File.read(path)
53+ File.read(File.join(source_directory, path))
4354 rescue
4455 instrument 'warn.doc', msg: "Failed to open file: #{path}"
4556 nil
lib/docs/scrapers/c.rb+0−1
@@ -1,7 +1,6 @@
11 module Docs
22 class C < FileScraper
33 self.type = 'c'
4− self.dir = '/Users/Thibaut/DevDocs/Docs/c'
54 self.base_url = 'http://en.cppreference.com/w/c/'
65 self.root_path = 'header.html'
76
lib/docs/scrapers/cpp.rb+0−1
@@ -3,7 +3,6 @@ module Docs · 2 unmodified lines
33 self.name = 'C++'
44 self.slug = 'cpp'
55 self.type = 'c'
6− self.dir = '/Users/Thibaut/DevDocs/Docs/cpp'
76 self.base_url = 'http://en.cppreference.com/w/cpp/'
87 self.root_path = 'header.html'
98
lib/docs/scrapers/dart.rb+0−2
@@ -24,13 +24,11 @@ module Docs · 23 unmodified lines
2424
2525 version '2' do
2626 self.release = '2.0.0'
27− self.dir = '/Users/Thibaut/DevDocs/Docs/Dart2'
2827 self.base_url = "https://api.dartlang.org/stable/#{release}/"
2928 end
3029
3130 version '1' do
3231 self.release = '1.24.3'
33− self.dir = '/Users/Thibaut/DevDocs/Docs/Dart1'
3432 self.base_url = "https://api.dartlang.org/stable/#{release}/"
3533 end
3634 end
lib/docs/scrapers/django.rb+0−6
@@ -36,37 +36,31 @@ module Docs · 35 unmodified lines
3636
3737 version '2.1' do
3838 self.release = '2.1.0'
39− self.dir = '/Users/Thibaut/DevDocs/Docs/Django21'
4039 self.base_url = 'https://docs.djangoproject.com/en/2.1/'
4140 end
4241
4342 version '2.0' do
4443 self.release = '2.0.7'
45− self.dir = '/Users/Thibaut/DevDocs/Docs/Django20'
4644 self.base_url = 'https://docs.djangoproject.com/en/2.0/'
4745 end
4846
4947 version '1.11' do
5048 self.release = '1.11.9'
51− self.dir = '/Users/Thibaut/DevDocs/Docs/Django111'
5249 self.base_url = 'https://docs.djangoproject.com/en/1.11/'
5350 end
5451
5552 version '1.10' do
5653 self.release = '1.10.8'
57− self.dir = '/Users/Thibaut/DevDocs/Docs/Django110'
5854 self.base_url = 'https://docs.djangoproject.com/en/1.10/'
5955 end
6056
6157 version '1.9' do
6258 self.release = '1.9.13'
63− self.dir = '/Users/Thibaut/DevDocs/Docs/Django19'
6459 self.base_url = 'https://docs.djangoproject.com/en/1.9/'
6560 end
6661
6762 version '1.8' do
6863 self.release = '1.8.18'
69− self.dir = '/Users/Thibaut/DevDocs/Docs/Django18'
7064 self.base_url = 'https://docs.djangoproject.com/en/1.8/'
7165 end
7266 end
lib/docs/scrapers/erlang.rb+0−4
@@ -42,22 +42,18 @@ module Docs · 41 unmodified lines
4242
4343 version '21' do
4444 self.release = '21.0'
45− self.dir = '/Users/Thibaut/DevDocs/Docs/Erlang21'
4645 end
4746
4847 version '20' do
4948 self.release = '20.3'
50− self.dir = '/Users/Thibaut/DevDocs/Docs/Erlang20'
5149 end
5250
5351 version '19' do
5452 self.release = '19.3'
55− self.dir = '/Users/Thibaut/DevDocs/Docs/Erlang19'
5653 end
5754
5855 version '18' do
5956 self.release = '18.3'
60− self.dir = '/Users/Thibaut/DevDocs/Docs/Erlang18'
6157 end
6258 end
6359 end
lib/docs/scrapers/gnu/gcc.rb+0−8
@@ -48,13 +48,11 @@ module Docs · 47 unmodified lines
4848
4949 version '7' do
5050 self.release = '7.3.0'
51− self.dir = '/Users/Thibaut/DevDocs/Docs/gcc7'
5251 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gcc/"
5352 end
5453
5554 version '7 CPP' do
5655 self.release = '7.3.0'
57− self.dir = '/Users/Thibaut/DevDocs/Docs/gcpp7'
5856 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/cpp/"
5957
6058 options[:replace_paths] = CPP_PATHS
@@ -62,7 +60,6 @@ module Docs · 1 unmodified line
6260
6361 version '6' do
6462 self.release = '6.4.0'
65− self.dir = '/Users/Thibaut/DevDocs/Docs/gcc6'
6663 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gcc/"
6764
6865 options[:root_title] = 'Using the GNU Compiler Collection (GCC)'
@@ -70,7 +67,6 @@ module Docs · 1 unmodified line
7067
7168 version '6 CPP' do
7269 self.release = '6.4.0'
73− self.dir = '/Users/Thibaut/DevDocs/Docs/gcpp6'
7470 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/cpp/"
7571
7672 options[:replace_paths] = CPP_PATHS
@@ -78,7 +74,6 @@ module Docs · 1 unmodified line
7874
7975 version '5' do
8076 self.release = '5.4.0'
81− self.dir = '/Users/Thibaut/DevDocs/Docs/gcc5'
8277 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gcc/"
8378
8479 options[:root_title] = 'Using the GNU Compiler Collection (GCC)'
@@ -86,7 +81,6 @@ module Docs · 1 unmodified line
8681
8782 version '5 CPP' do
8883 self.release = '5.4.0'
89− self.dir = '/Users/Thibaut/DevDocs/Docs/gcpp5'
9084 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/cpp/"
9185
9286 options[:replace_paths] = CPP_PATHS
@@ -94,7 +88,6 @@ module Docs · 1 unmodified line
9488
9589 version '4' do
9690 self.release = '4.9.3'
97− self.dir = '/Users/Thibaut/DevDocs/Docs/gcc4'
9891 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gcc/"
9992
10093 options[:root_title] = 'Using the GNU Compiler Collection (GCC)'
@@ -102,7 +95,6 @@ module Docs · 1 unmodified line
10295
10396 version '4 CPP' do
10497 self.release = '4.9.3'
105− self.dir = '/Users/Thibaut/DevDocs/Docs/gcpp4'
10698 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/cpp/"
10799
108100 options[:replace_paths] = CPP_PATHS
lib/docs/scrapers/gnu/gnu_fortran.rb+0−4
@@ -8,25 +8,21 @@ module Docs · 7 unmodified lines
88
99 version '7' do
1010 self.release = '7.3.0'
11− self.dir = '/Users/Thibaut/DevDocs/Docs/gfortran7'
1211 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gfortran/"
1312 end
1413
1514 version '6' do
1615 self.release = '6.4.0'
17− self.dir = '/Users/Thibaut/DevDocs/Docs/gfortran6'
1816 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gfortran/"
1917 end
2018
2119 version '5' do
2220 self.release = '5.4.0'
23− self.dir = '/Users/Thibaut/DevDocs/Docs/gfortran5'
2421 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gfortran/"
2522 end
2623
2724 version '4' do
2825 self.release = '4.9.3'
29− self.dir = '/Users/Thibaut/DevDocs/Docs/gfortran4'
3026 self.base_url = "https://gcc.gnu.org/onlinedocs/gcc-#{release}/gfortran/"
3127 end
3228 end
lib/docs/scrapers/nokogiri2.rb+0−1
@@ -3,7 +3,6 @@ module Docs · 2 unmodified lines
33 self.name = 'Nokogiri'
44 self.slug = 'nokogiri'
55 self.release = '1.8.1'
6− self.dir = '/Users/Thibaut/DevDocs/Docs/RDoc/Nokogiri'
76
87 html_filters.replace 'rdoc/entries', 'nokogiri2/entries'
98
lib/docs/scrapers/numpy.rb+0−1
@@ -2,7 +2,6 @@ module Docs · 1 unmodified line
22 class Numpy < FileScraper
33 self.name = 'NumPy'
44 self.type = 'sphinx'
5− self.dir = '/Users/Thibaut/DevDocs/Docs/numpy/reference/'
65 self.root_path = 'index.html'
76 self.links = {
87 home: 'http://www.numpy.org/',
lib/docs/scrapers/openjdk.rb+2−3
@@ -1,11 +1,10 @@
11 module Docs
22 class Openjdk < FileScraper
3+ # Downloaded from packages.debian.org/sid/openjdk-8-doc
4+ # Extracting subdirectory /usr/share/doc/openjdk-8-jre-headless/api
35 self.name = 'OpenJDK'
46 self.type = 'openjdk'
57 self.root_path = 'overview-summary.html'
6− # Downloaded from packages.debian.org/sid/openjdk-8-doc
7− # Extracting subdirectory /usr/share/doc/openjdk-8-jre-headless/api
8− self.dir = '/Users/Thibaut/DevDocs/Docs/OpenJDK'
98
109 html_filters.insert_after 'internal_urls', 'openjdk/clean_urls'
1110 html_filters.push 'openjdk/entries', 'openjdk/clean_html'
lib/docs/scrapers/perl.rb+0−1
@@ -2,7 +2,6 @@ module Docs · 1 unmodified line
22 class Perl < FileScraper
33 self.name = 'Perl'
44 self.type = 'perl'
5− self.dir = '/Users/Thibaut/DevDocs/Docs/Perl'
65 self.root_path = 'index.html'
76 self.links = {
87 home: 'https://www.perl.org/'
lib/docs/scrapers/php.rb+2−3
@@ -1,5 +1,7 @@
11 module Docs
22 class Php < FileScraper
3+ # Downloaded from php.net/download-docs.php
4+
35 include FixInternalUrlsBehavior
46
57 self.name = 'PHP'
@@ -23,9 +25,6 @@ module Docs · 17 unmodified lines
2325 code: 'https://git.php.net/?p=php-src.git;a=summary'
2426 }
2527
26− # Downloaded from php.net/download-docs.php
27− self.dir = '/Users/Thibaut/DevDocs/Docs/PHP'
28−
2928 html_filters.push 'php/internal_urls', 'php/entries', 'php/clean_html', 'title'
3029 text_filters.push 'php/fix_urls'
3130
lib/docs/scrapers/python.rb+4−8
@@ -23,33 +23,29 @@ module Docs · 22 unmodified lines
2323 Licensed under the PSF License.
2424 HTML
2525
26− version '3.7' do
26+ version '3.7' do # docs.python.org/3.7/download.html
2727 self.release = '3.7.0'
28− self.dir = '/Users/Thibaut/DevDocs/Docs/Python37' # docs.python.org/3.7/download.html
2928 self.base_url = 'https://docs.python.org/3.7/'
3029
3130 html_filters.push 'python/entries_v3', 'sphinx/clean_html', 'python/clean_html'
3231 end
3332
34− version '3.6' do
33+ version '3.6' do # docs.python.org/3.6/download.html
3534 self.release = '3.6.6'
36− self.dir = '/Users/Thibaut/DevDocs/Docs/Python36' # docs.python.org/3.6/download.html
3735 self.base_url = 'https://docs.python.org/3.6/'
3836
3937 html_filters.push 'python/entries_v3', 'sphinx/clean_html', 'python/clean_html'
4038 end
4139
42− version '3.5' do
40+ version '3.5' do # docs.python.org/3.5/download.html
4341 self.release = '3.5.3'
44− self.dir = '/Users/Thibaut/DevDocs/Docs/Python35' # docs.python.org/3.5/download.html
4542 self.base_url = 'https://docs.python.org/3.5/'
4643
4744 html_filters.push 'python/entries_v3', 'sphinx/clean_html', 'python/clean_html'
4845 end
4946
50− version '2.7' do
47+ version '2.7' do # docs.python.org/2.7/download.html
5148 self.release = '2.7.13'
52− self.dir = '/Users/Thibaut/DevDocs/Docs/Python27' # docs.python.org/2.7/download.html
5349 self.base_url = 'https://docs.python.org/2.7/'
5450
5551 html_filters.push 'python/entries_v2', 'sphinx/clean_html', 'python/clean_html'
lib/docs/scrapers/rdoc/minitest.rb+1−1
@@ -1,9 +1,9 @@
11 module Docs
22 class Minitest < Rdoc
3+ # Run "rake docs" in the gem directory
34 self.name = 'Ruby / Minitest'
45 self.slug = 'minitest'
56 self.release = '5.10.3'
6− self.dir = '/Users/Thibaut/DevDocs/Docs/RDoc/Minitest' # rake docs
77 self.links = {
88 code: 'https://github.com/seattlerb/minitest'
99 }
lib/docs/scrapers/rdoc/rails.rb+0−1
@@ -4,7 +4,6 @@ module Docs · 3 unmodified lines
44
55 self.name = 'Ruby on Rails'
66 self.slug = 'rails'
7− self.dir = '/Users/Thibaut/DevDocs/Docs/RDoc/Rails'
87 self.initial_paths = %w(guides/index.html)
98 self.links = {
109 home: 'http://rubyonrails.org/',
lib/docs/scrapers/rdoc/ruby.rb+0−4
@@ -78,22 +78,18 @@ module Docs · 77 unmodified lines
7878
7979 version '2.5' do
8080 self.release = '2.5.0'
81− self.dir = '/Users/Thibaut/DevDocs/Docs/RDoc/Ruby25'
8281 end
8382
8483 version '2.4' do
8584 self.release = '2.4.3'
86− self.dir = '/Users/Thibaut/DevDocs/Docs/RDoc/Ruby24'
8785 end
8886
8987 version '2.3' do
9088 self.release = '2.3.6'
91− self.dir = '/Users/Thibaut/DevDocs/Docs/RDoc/Ruby23'
9289 end
9390
9491 version '2.2' do
9592 self.release = '2.2.9'
96− self.dir = '/Users/Thibaut/DevDocs/Docs/RDoc/Ruby22'
9793 end
9894 end
9995 end
lib/docs/scrapers/sqlite.rb+0−1
@@ -3,7 +3,6 @@ module Docs · 2 unmodified lines
33 self.name = 'SQLite'
44 self.type = 'sqlite'
55 self.release = '3.25.2'
6− self.dir = '/Users/Thibaut/DevDocs/Docs/sqlite/'
76 self.base_url = 'https://sqlite.org/'
87 self.root_path = 'docs.html'
98 self.initial_paths = %w(keyword_index.html)