2017-09-17 04:30:34 +00:00
|
|
|
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
|
|
|
|
|
2017-09-15 06:10:06 +00:00
|
|
|
# https://stackoverflow.com/a/42533209/368328
|
|
|
|
def command?(name)
|
|
|
|
[name,
|
2017-09-17 04:30:34 +00:00
|
|
|
*ENV['PATH'].split(File::PATH_SEPARATOR)
|
|
|
|
.map { |p| File.join(p, name) }]
|
|
|
|
.find { |f| File.executable?(f) }
|
2017-09-15 06:10:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def commands?(commands)
|
2017-09-17 04:30:34 +00:00
|
|
|
commands.map { |c| command? c }
|
2017-09-15 06:10:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_match(format)
|
|
|
|
[:all, format].include? format
|
|
|
|
end
|
|
|
|
|
2017-09-17 04:30:34 +00:00
|
|
|
def gen_epub(name, _format)
|
|
|
|
if command?('pandoc') && format_match(:epub)
|
2017-09-15 06:10:06 +00:00
|
|
|
# Convert it to epub
|
2017-09-15 08:28:26 +00:00
|
|
|
`pandoc -S -o books/#{name}.epub --epub-metadata=metadata/#{name}.xml --epub-cover-image=covers/#{name}.jpg books/#{name}.html`
|
2017-09-17 04:30:34 +00:00
|
|
|
puts '[epub] Generated EPUB file'
|
2017-09-15 06:10:06 +00:00
|
|
|
else
|
|
|
|
puts "[error] Can't generate EPUB without pandoc"
|
|
|
|
end
|
2017-09-17 04:30:34 +00:00
|
|
|
end
|
2017-09-15 06:10:06 +00:00
|
|
|
|
2017-09-17 04:30:34 +00:00
|
|
|
def gen_mobi(name, _format)
|
|
|
|
if command?('ebook-convert') && format_match(:mobi)
|
2017-09-15 06:10:06 +00:00
|
|
|
# Convert epub to a mobi
|
2017-09-15 08:28:26 +00:00
|
|
|
`ebook-convert books/#{name}.epub books/#{name}.mobi`
|
2017-09-17 04:30:34 +00:00
|
|
|
puts '[mobi] Generated MOBI file'
|
2017-09-15 06:10:06 +00:00
|
|
|
else
|
|
|
|
puts "[error] Can't generate MOBI without ebook-convert"
|
|
|
|
end
|
2017-09-17 04:30:34 +00:00
|
|
|
end
|
2017-09-15 06:10:06 +00:00
|
|
|
|
2017-09-17 04:30:34 +00:00
|
|
|
def gen_pdf(name, _format)
|
|
|
|
if commands?(%w[pandoc convert wkhtmltopdf pdftk]) && format_match(:pdf)
|
2017-09-15 06:10:06 +00:00
|
|
|
# Generate PDF as well
|
|
|
|
# First, lets make a better css version of the html
|
2017-09-17 04:30:34 +00:00
|
|
|
`pandoc books/#{name}.html -s -c ../style.css -o books/#{name}_pdf.html`
|
|
|
|
puts '[pdf] Generated html for pdf'
|
2017-09-15 06:10:06 +00:00
|
|
|
|
|
|
|
# Print the pdf_html file to pdf
|
2017-09-17 04:30:34 +00:00
|
|
|
`wkhtmltopdf books/#{name}_pdf.html books/#{name}-nocover.pdf`
|
|
|
|
puts '[pdf] Generated PDF without cover'
|
2017-09-15 06:10:06 +00:00
|
|
|
|
|
|
|
# Join the cover and pdf together
|
2017-09-15 08:28:26 +00:00
|
|
|
`pdftk covers/#{name}.pdf books/#{name}-nocover.pdf cat output books/#{name}.pdf`
|
2017-09-17 04:30:34 +00:00
|
|
|
puts '[pdf] Generated PDF file'
|
2017-09-15 06:10:06 +00:00
|
|
|
else
|
2017-09-17 04:30:34 +00:00
|
|
|
puts '[error] Please check README for PDF dependencies'
|
2017-09-15 06:10:06 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-17 04:30:34 +00:00
|
|
|
|
|
|
|
def generate(name, _format = :all)
|
|
|
|
gen_epub(name, _format)
|
|
|
|
gen_mobi(name, _format)
|
|
|
|
gen_pdf(name, _format)
|
|
|
|
end
|