Class: Foliokit::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/foliokit/command.rb

Instance Method Summary collapse

Instance Method Details

#convert(folio_path, hpub_path = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/foliokit/command.rb', line 14

def convert(folio_path, hpub_path = nil)
  # print usage information or version if requests
  respond!(Foliokit::VERSION) if options.version?
  help("convert") && exit if options.help?

  # validate arguments
  (folio_file, range) = parse_file_arg(folio_path, must_exist: true)
  hpub_path = "#{folio_file.dir}/#{folio_file.basename}.hpub" if !options.out? and hpub_path.nil?
  hpub_file = parse_file_arg(hpub_path)&.first
  tmp_dir = parse_dir_arg(options.tmp, must_not_exist: !options.force?) || Workspace.tmpdir
  out_dir = parse_dir_arg(options.out, must_not_exist: !options.force?) || Workspace.tmpdir

  # convert folio issue
  issue = Foliokit::Issue.new(folio_file, tmp_dir, range)
  error!("no slides to export") if issue.slides.empty?
  exporter = Foliokit::Exporter.new(issue)
  exporter.export(out_dir.clean) do |slide, index|
    puts "-- converting slide #{index}" unless options.quiet?
  end

  # export hpub
  exporter.pack_hpub(hpub_file) unless hpub_file.nil?

  # clear directories
  tmp_dir.delete unless options.tmp?
  out_dir.delete unless options.out?
end