cosmere-books/methods.rb

108 lines
2.7 KiB
Ruby
Raw Normal View History

2019-12-30 18:30:53 +00:00
# frozen_string_literal: true
2017-09-17 04:30:34 +00:00
module Nokogiri
module XML
# Patch to add class?
class Node
def class?(*classes)
present = false
2017-09-27 16:44:55 +00:00
if attribute('class')
2017-09-17 04:30:34 +00:00
present = true
classes.each do |klass|
present &&= self['class'].include? klass
end
end
present
end
end
end
end
# https://stackoverflow.com/a/42533209/368328
2017-11-12 21:16:21 +00:00
def command?(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
2017-11-12 21:16:21 +00:00
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
2017-11-12 21:16:21 +00:00
end
nil
end
def commands?(commands)
2017-09-17 04:30:34 +00:00
commands.map { |c| command? c }
end
def format_match(format, format_to_match)
[:all, format_to_match].include? format
end
2017-09-27 16:44:55 +00:00
def gen_epub(name, format)
return unless format_match(format, :epub)
2017-09-27 16:44:55 +00:00
begin
require 'paru/pandoc'
Paru::Pandoc.new do
from 'html'
to 'epub'
epub_metadata "metadata/#{name}.xml"
epub_cover_image "covers/#{name}.jpg"
2020-04-26 01:26:36 +00:00
metadata title: name
data_dir Dir.pwd
2017-09-27 16:44:55 +00:00
output "books/#{name}.epub"
end.convert File.read("books/#{name}.html")
puts '[epub] Generated EPUB file'
rescue LoadError
puts "[error] Can't generate EPUB without paru"
end
2017-09-17 04:30:34 +00:00
end
2017-09-27 16:44:55 +00:00
def gen_mobi(name, format)
if command?('ebook-convert') && format_match(format, :mobi)
# 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'
else
puts "[error] Can't generate MOBI without ebook-convert"
end
2017-09-17 04:30:34 +00:00
end
def inside_docker?
File.readlines('/proc/1/sched').each do |line|
return line.strip != 'systemd (1, #threads: 1)'
end
2020-08-17 11:47:24 +00:00
rescue Errno::ENOENT
false
end
2017-09-27 16:44:55 +00:00
def gen_pdf(name, format)
if commands?(%w[pandoc convert wkhtmltopdf pdftk]) && format_match(format, :pdf)
# Generate PDF as well
# First, lets make a better css version of the html
`pandoc books/#{name}.html -s -c ../epub.css -o books/#{name}_pdf.html`
2017-09-17 04:30:34 +00:00
puts '[pdf] Generated html for pdf'
# Print the pdf_html file to pdf
if inside_docker?
`xvfb-run wkhtmltopdf books/#{name}_pdf.html books/#{name}-nocover.pdf`
else
`wkhtmltopdf books/#{name}_pdf.html books/#{name}-nocover.pdf`
end
2017-09-17 04:30:34 +00:00
puts '[pdf] Generated PDF without cover'
# 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'
else
2017-09-17 04:30:34 +00:00
puts '[error] Please check README for PDF dependencies'
end
end
2017-09-17 04:30:34 +00:00
2017-09-27 16:44:55 +00:00
def generate(name, format = :all)
gen_epub(name, format)
gen_mobi(name, format)
gen_pdf(name, format)
2017-09-17 04:30:34 +00:00
end