Skip to content

Commit

Permalink
Adds rubocop, refactors code
Browse files Browse the repository at this point in the history
  • Loading branch information
captn3m0 committed Sep 17, 2017
1 parent d4f6eaf commit 9897150
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 251 deletions.
50 changes: 50 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,50 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-09-17 09:34:31 +0530 using RuboCop version 0.50.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Metrics/AbcSize:
Max: 16

# Offense count: 8
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 80
AllowHeredoc: true
AllowURI: true
URISchemes:
- https
- http
IgnoredPatterns:
- \`

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 22

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 10

# Offense count: 2
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
Naming/FileName:
Exclude:
- 'wok-reread.rb'
- 'wor-reread.rb'

# Offense count: 5
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: for, each
Style/For:
Exclude:
- 'oathbringer.rb'
- 'wok-reread.rb'
- 'wor-reread.rb'
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -6,6 +6,7 @@ Scripts to generate books from the [Cosmere]() using various public sources. Cur

1. Oathbringer (Serialized till Chapter 32)
2. Way of Kings Reread
3. Words of Radiance Reread

For obvious reasons, the converted ebooks are not part of this repo. You must download
and run the script on your own machine to generate the copies.
Expand Down
60 changes: 44 additions & 16 deletions methods.rb
@@ -1,50 +1,78 @@
module Nokogiri
module XML
# Patch to add class?
class Node
def class?(*classes)
present = false
if self.attribute('class')
present = true
classes.each do |klass|
present &&= self['class'].include? klass
end
end
present
end
end
end
end

# https://stackoverflow.com/a/42533209/368328
def command?(name)
[name,
*ENV['PATH'].split(File::PATH_SEPARATOR).map {|p| File.join(p, name)}
].find {|f| File.executable?(f)}
*ENV['PATH'].split(File::PATH_SEPARATOR)
.map { |p| File.join(p, name) }]
.find { |f| File.executable?(f) }
end

def commands?(commands)
commands.map {|c| command? c}
commands.map { |c| command? c }
end

def format_match(format)
[:all, format].include? format
end


def generate(name, format=:all)
if command? 'pandoc' and format_match(:epub)
def gen_epub(name, _format)
if command?('pandoc') && format_match(:epub)
# Convert it to epub
`pandoc -S -o books/#{name}.epub --epub-metadata=metadata/#{name}.xml --epub-cover-image=covers/#{name}.jpg books/#{name}.html`
puts "[epub] Generated EPUB file"
puts '[epub] Generated EPUB file'
else
puts "[error] Can't generate EPUB without pandoc"
end
end

if command? 'ebook-convert' and format_match(:mobi)
def gen_mobi(name, _format)
if command?('ebook-convert') && format_match(:mobi)
# Convert epub to a mobi
`ebook-convert books/#{name}.epub books/#{name}.mobi`
puts "[mobi] Generated MOBI file"
puts '[mobi] Generated MOBI file'
else
puts "[error] Can't generate MOBI without ebook-convert"
end
end

if commands? ['pandoc', 'convert', 'wkhtmltopdf', 'pdftk'] and format_match(:pdf)
def gen_pdf(name, _format)
if commands?(%w[pandoc convert wkhtmltopdf pdftk]) && format_match(:pdf)
# Generate PDF as well
# First, lets make a better css version of the html
`pandoc books/#{name}.html -s -c style.css -o books/#{name}_pdf.html`
puts "[pdf] Generated html for pdf"
`pandoc books/#{name}.html -s -c ../style.css -o books/#{name}_pdf.html`
puts '[pdf] Generated html for pdf'

# Print the pdf_html file to pdf
`wkhtmltopdf #{name}_pdf.html books/#{name}-nocover.pdf`
puts "[pdf] Generated PDF without cover"
`wkhtmltopdf books/#{name}_pdf.html books/#{name}-nocover.pdf`
puts '[pdf] Generated PDF without cover'

# Join the cover and pdf together
`pdftk covers/#{name}.pdf books/#{name}-nocover.pdf cat output books/#{name}.pdf`
puts "[pdf] Generated PDF file"
puts '[pdf] Generated PDF file'
else
puts "[error] Please check README for PDF dependencies"
puts '[error] Please check README for PDF dependencies'
end
end

def generate(name, _format = :all)
gen_epub(name, _format)
gen_mobi(name, _format)
gen_pdf(name, _format)
end
54 changes: 25 additions & 29 deletions oathbringer.rb
Expand Up @@ -2,15 +2,15 @@
require 'fileutils'
require 'nokogiri'
require_relative './methods'
FileUtils.mkdir_p("oathbringer")
FileUtils.mkdir_p('oathbringer')

BASE = 'https://www.tor.com/2017/'
BASE = 'https://www.tor.com/2017/'.freeze

links = [
'08/22/oathbringer-brandon-sanderson-prologue/',
'08/29/oathbringer-brandon-sanderson-chapter-1-3/',
'09/05/oathbringer-by-brandon-sanderson-chapters-4-6/',
'09/12/oathbringer-by-brandon-sanderson-chapters-7-9/'
'08/22/oathbringer-brandon-sanderson-prologue/',
'08/29/oathbringer-brandon-sanderson-chapter-1-3/',
'09/05/oathbringer-by-brandon-sanderson-chapters-4-6/',
'09/12/oathbringer-by-brandon-sanderson-chapters-7-9/'
]

links.last.split('/')
Expand All @@ -22,43 +22,39 @@

episode = 1

for link in links
url = BASE + link
puts "Download #{url}"
if !File.exists? "oathbringer/#{episode}.html"
`wget --no-clobber "#{url}" --output-document "oathbringer/#{episode}.html" -o /dev/null`
end
episode +=1
links.each do |link|
url = BASE + link
puts "Download #{url}"
unless File.exist? "oathbringer/#{episode}.html"
`wget --no-clobber "#{url}" --output-document "oathbringer/#{episode}.html" -o /dev/null`
end
episode += 1
end

# Now we have all the files
html = ""
html = ''
for i in 1..(links.length)
page = Nokogiri::HTML(open("oathbringer/#{i}.html")).css('.entry-content')
start = ending = false
page.children.each do |e|
if e.name == 'h3'
e.name = 'h1'
start = true
end
if e.name == 'h3'
e.name = 'h1'
start = true
end

if e.attribute('class') and e['class'].include? 'frontmatter' and start
ending = true
end
ending = true if e.class?('frontmatter') && start

if !start or ending
e.remove
end
e.remove if !start || ending
end
html += page.inner_html
url = links[i-1]
url = links[i - 1]

html += "<p>Visit <a href='#{url}'>tor.com</a> for discussion.</p>"
end

html += "<p>~fin\~<br>Next 3 chapters out on #{next_date.to_s}</p>"
html += "<p>~fin\~<br>Next 3 chapters out on #{next_date}</p>"

File.open("books/Oathbringer.html", 'w') { |file| file.write(html) }
puts "[html] Generated HTML file"
File.open('books/Oathbringer.html', 'w') { |file| file.write(html) }
puts '[html] Generated HTML file'

generate("Oathbringer", :all)
generate('Oathbringer', :all)

0 comments on commit 9897150

Please sign in to comment.