From 14b1b5552025f120969eb7db1cb19c7b98a513eb Mon Sep 17 00:00:00 2001 From: Nemo Date: Fri, 15 Sep 2017 11:40:06 +0530 Subject: [PATCH] Makes code more generic, adds discussion link --- .editorconfig | 9 +++++++++ methods.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ setup.rb | 32 +++++-------------------------- 3 files changed, 67 insertions(+), 27 deletions(-) create mode 100644 .editorconfig create mode 100644 methods.rb diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..c6c8b36 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/methods.rb b/methods.rb new file mode 100644 index 0000000..5ed8c6e --- /dev/null +++ b/methods.rb @@ -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 diff --git a/setup.rb b/setup.rb index 06c756b..2a7616a 100644 --- a/setup.rb +++ b/setup.rb @@ -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 += "

Visit tor.com for discussion.

" end html += "

~fin\~
Next 3 chapters out on #{next_date.to_s}

" -# 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)