-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
row.rb
84 lines (71 loc) · 2.73 KB
/
row.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true
require 'date'
require 'fileutils'
require 'nokogiri'
require_relative './methods'
require_relative './row-annotations'
FileUtils.mkdir_p('row')
BASE = 'https://www.tor.com/2020/'
links = [
'07/23/read-rhythm-of-war-by-brandon-sanderson-prologue-and-chapter-one/',
'07/28/read-rhythm-of-war-by-brandon-sanderson-chapters-two-and-three/',
'08/04/read-rhythm-of-war-by-brandon-sanderson-chapters-four-and-five/',
'08/11/read-rhythm-of-war-by-brandon-sanderson-chapter-six/',
'08/18/read-rhythm-of-war-by-brandon-sanderson-chapter-seven/',
'08/25/read-rhythm-of-war-by-brandon-sanderson-chapter-eight/',
'09/01/read-rhythm-of-war-by-brandon-sanderson-chapter-nine/',
'09/08/read-rhythm-of-war-by-brandon-sanderson-chapter-ten/',
'09/15/read-rhythm-of-war-by-brandon-sanderson-chapter-eleven/',
'09/22/read-rhythm-of-war-by-brandon-sanderson-chapter-twelve/',
'09/29/read-rhythm-of-war-by-brandon-sanderson-chapter-thirteen/',
'10/06/read-rhythm-of-war-by-brandon-sanderson-chapter-fourteen/',
'10/13/read-rhythm-of-war-by-brandon-sanderson-chapter-fifteen/',
'10/20/read-rhythm-of-war-by-brandon-sanderson-chapter-sixteen/',
'10/27/read-rhythm-of-war-by-brandon-sanderson-chapter-seventeen/',
'11/03/read-rhythm-of-war-by-brandon-sanderson-chapter-eighteen/',
'11/10/read-rhythm-of-war-by-brandon-sanderson-chapter-nineteen/'
]
# Automatically adds all recent chapters
puts 'Downloading all found links'
episode = 1
links.each do |link|
url = BASE + link
puts "Download #{url}"
unless File.exist? "row/#{episode}.html"
`wget --no-clobber "#{url}" --output-document "row/#{episode}.html" -o /dev/null`
end
episode += 1
end
# Now we have all the files
html = ''
(1..(links.length)).each do |i|
page = Nokogiri::HTML(open("row/#{i}.html")).css('.entry-content')
start = ending = false
page.children.each do |e|
if e.name == 'h3'
e.name = 'h1'
start = true
end
ending = true if e.class?('frontmatter') && start
e.remove if !start || ending
end
chapter_html = page.inner_html.sub(/<h1/, "<h1 id='chapter-#{i-1}'")
html += chapter_html
if $annotations[i-1]
html += "<p><a href='#annotation-#{i-1}'>Click here</a> to reach Brandon's annotations for this chapter.</p>"
end
url = BASE + links[i - 1]
end
html += $images
$annotations.each_with_index do |a, i|
if a
html += "<h1 id='annotation-#{i}'>Annotations - " + links[i].split('/').last[40..] + "</h1>"
html += a.gsub(/(\r)?\n/, "<br>")
# There is no next chapter
html += "<a href='#chapter-#{i+1}'>Click here</a> to go the next chapter." if i !=15
end
end
html+= $jasnahspoiler
File.open('books/row.html', 'w') { |file| file.write(html) }
puts '[html] Generated HTML file'
generate('row', :all)