Class: Kitabu::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/kitabu/exporter.rb,
lib/kitabu/exporter/css.rb,
lib/kitabu/exporter/pdf.rb,
lib/kitabu/exporter/base.rb,
lib/kitabu/exporter/epub.rb,
lib/kitabu/exporter/html.rb,
lib/kitabu/exporter/mobi.rb

Defined Under Namespace

Classes: Base, CSS, Epub, HTML, Mobi, PDF

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir, options) ⇒ Exporter

Returns a new instance of Exporter.



12
13
14
15
# File 'lib/kitabu/exporter.rb', line 12

def initialize(root_dir, options)
  @root_dir = root_dir
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/kitabu/exporter.rb', line 10

def options
  @options
end

#root_dirObject

Returns the value of attribute root_dir.



10
11
12
# File 'lib/kitabu/exporter.rb', line 10

def root_dir
  @root_dir
end

Class Method Details

.run(root_dir, options) ⇒ Object



5
6
7
8
# File 'lib/kitabu/exporter.rb', line 5

def self.run(root_dir, options)
  exporter = new(root_dir, options)
  exporter.export!
end

Instance Method Details

#configObject



64
65
66
# File 'lib/kitabu/exporter.rb', line 64

def config
  Kitabu.config(root_dir)
end

#export!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kitabu/exporter.rb', line 21

def export!
  helper = root_dir.join("config/helper.rb")
  load(helper) if helper.exist?

  FileUtils.rm_rf root_dir.join("output").to_s

  export_pdf = [nil, "pdf"].include?(options[:only])
  export_epub = [nil, "mobi", "epub"].include?(options[:only])
  export_mobi = [nil, "mobi"].include?(options[:only])

  exported = []
  exported << HTML.export(root_dir)
  exported << PDF.export(root_dir) if export_pdf && Dependency.prince?
  exported << Epub.export(root_dir) if export_epub
  exported << Mobi.export(root_dir) if export_mobi && Dependency.calibre?

  if exported.all?
    color = :green
    message = options[:auto] ? "exported!" : "=> e-book has been exported"

    if options[:open] && export_pdf
      filepath = root_dir.join("output/#{File.basename(root_dir)}.pdf")

      case RUBY_PLATFORM
      when /darwin/
        IO.popen("open -a Preview.app '#{filepath}'").close
      when /linux/
        Process.detach(Process.spawn("xdg-open '#{filepath}'",
                                     out: "/dev/null"))
      end
    end
  else
    color = :red
    message = if options[:auto]
                "could not be exported!"
              else
                "=> e-book couldn't be exported"
              end
  end

  ui.say message, color
end

#uiObject



17
18
19
# File 'lib/kitabu/exporter.rb', line 17

def ui
  @ui ||= Thor::Base.shell.new
end