Class: Bookshelf::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/bookshelf/exporter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book_dir, options) ⇒ Exporter

Returns a new instance of Exporter.



11
12
13
14
# File 'lib/bookshelf/exporter.rb', line 11

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

Instance Attribute Details

#book_dirObject

Returns the value of attribute book_dir.



8
9
10
# File 'lib/bookshelf/exporter.rb', line 8

def book_dir
  @book_dir
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/bookshelf/exporter.rb', line 9

def options
  @options
end

Class Method Details

.run(book_dir, options) ⇒ Object



3
4
5
6
# File 'lib/bookshelf/exporter.rb', line 3

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

Instance Method Details

#configObject



60
61
62
# File 'lib/bookshelf/exporter.rb', line 60

def config
  Bookshelf.config
end

#export!Object



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
# File 'lib/bookshelf/exporter.rb', line 31

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

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

  exported = []
  exported << Parser::HTML.parse(book_dir)
  exported << Parser::PDF.parse(book_dir) if export_pdf && Dependency.prince?
  exported << Parser::Epub.parse(book_dir) if export_epub

  if exported.all?
    color = :green
    message = options[:auto] ? "exported!" : "** e-book has been exported"
    Notifier.notify(
      :image   => Bookshelf::ROOT.join("templates/ebook.png"),
      :title   => "Bookshelf",
      :message => "Your \"#{config[:title]}\" e-book has been exported!"
    )
  else
    color = :red
    message = options[:auto] ? "could not be exported!" : "** e-book couldn't be exported"
  end

  ui.say message, color
end

#export_assetsObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/bookshelf/exporter.rb', line 20

def export_assets
  FileUtils.mkdir_p("output/assets/fonts")
  FileUtils.mkdir_p("output/assets/images")
  FileUtils.mkdir_p("output/assets/styles")
  FileUtils.cp_r(Dir.glob('assets/fonts/*'), "output/assets/fonts/")
  FileUtils.cp_r(Dir.glob('assets/images/*'), "output/assets/images/")
  ["epub","html"].each do |style_sheet|
    `sass assets/styles/#{style_sheet}.scss output/assets/styles/#{style_sheet}.css`
  end
end

#uiObject



16
17
18
# File 'lib/bookshelf/exporter.rb', line 16

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