Makes code more generic, adds discussion link

This commit is contained in:
Nemo 2017-09-15 11:40:06 +05:30
parent 818225cb1e
commit 14b1b55520
3 changed files with 67 additions and 27 deletions

9
.editorconfig Normal file
View File

@ -0,0 +1,9 @@
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

53
methods.rb Normal file
View File

@ -0,0 +1,53 @@
# 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)}
end
def commands?(commands)
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)
# Convert it to epub
`pandoc -S -o #{name}.epub --epub-metadata=metadata.xml --epub-cover-image=cover.jpg #{name}.html`
puts "[epub] Generated EPUB file"
else
puts "[error] Can't generate EPUB without pandoc"
end
if command? 'ebook-convert' and format_match(:mobi)
# Convert epub to a mobi
`ebook-convert #{name}.epub #{name}.mobi`
puts "[mobi] Generated MOBI file"
else
puts "[error] Can't generate MOBI without ebook-convert"
end
if commands? ['pandoc', 'convert', 'wkhtmltopdf', 'pdftk'] and format_match(:pdf)
# Generate PDF as well
# First, lets make a better css version of the html
`pandoc #{name}.html -s -c style.css -o #{name}_pdf.html`
puts "[pdf] Generated html for pdf"
# Now we convert the cover to a pdf
`convert cover.jpg cover.pdf`
puts "[pdf] Generated cover for pdf"
# Print the pdf_html file to pdf
`wkhtmltopdf #{name}_pdf.html /tmp/#{name}.pdf`
puts "[pdf] Generated PDF without cover"
# Join the cover and pdf together
`pdftk cover.pdf /tmp/#{name}.pdf cat output #{name}.pdf`
puts "[pdf] Generated PDF file"
else
puts "[error] Please check README for PDF dependencies"
end
end

View File

@ -1,7 +1,7 @@
require 'date'
require 'fileutils'
require 'nokogiri'
require_relative './methods'
FileUtils.mkdir_p("html")
BASE = 'https://www.tor.com/2017/'
@ -43,7 +43,7 @@ for i in 1..3
end
if e.attribute('class') and e['class'].include? 'frontmatter' and start
ending = true
ending = true
end
if !start or ending
@ -51,35 +51,13 @@ for i in 1..3
end
end
html += page.inner_html
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>"
# Write it in the book
File.open("Oathbringer.html", 'w') { |file| file.write(html) }
puts "[html] Generated HTML file"
# Convert it to epub
`pandoc -S -o Oathbringer.epub --epub-metadata=metadata.xml --epub-cover-image=cover.jpg Oathbringer.html`
puts "[epub] Generated EPUB file"
# Convert epub to a mobi
`ebook-convert Oathbringer.epub Oathbringer.mobi`
puts "[mobi] Generated MOBI file"
# Generate PDF as well
# First, lets make a better css version of the html
`pandoc Oathbringer.html -s -c style.css -o Oathbringer_pdf.html`
puts "[pdf] Generated html for pdf"
# Now we convert the cover to a pdf
`convert cover.jpg cover.pdf`
puts "[pdf] Generated cover for pdf"
# Print the pdf_html file to pdf
`wkhtmltopdf Oathbringer_pdf.html /tmp/Oathbringer.pdf`
puts "[pdf] Generated PDF without cover"
# Join the cover and pdf together
`pdftk cover.pdf /tmp/Oathbringer.pdf cat output Oathbringer.pdf`
puts "[pdf] Generated PDF file"
generate("Oathbringer", :all)