Class: Bookie::Emitters::MOBI

Inherits:
HTML
  • Object
show all
Defined in:
lib/bookie/emitters/mobi.rb

Constant Summary collapse

OPF_TEMPLATE =
%{

}

Instance Attribute Summary

Attributes inherited from HTML

#body

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HTML

#build_list, #build_paragraph, #build_raw_text, #build_section_heading, #convert_inlines

Constructor Details

#initializeMOBI

Returns a new instance of MOBI.



12
13
14
# File 'lib/bookie/emitters/mobi.rb', line 12

def initialize
  @chapters = []
end

Class Method Details

.extensionObject



8
9
10
# File 'lib/bookie/emitters/mobi.rb', line 8

def self.extension
  ".mobi"
end

Instance Method Details

#render(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bookie/emitters/mobi.rb', line 23

def render(params)
  Dir.mktmpdir("bookie-mobi") do |dir|
    @chapters.each_with_index do |(title, content),index|
      File.open("#{dir}/#{index}.html", "w") do |f|
        template = File.read("#{Bookie::TEMPLATES_DIR}/mobi_chapter.erb")
        f << ERB.new(template).result(binding)
      end
    end

    File.open("#{dir}/book.opf", "w") do |f|
      template = File.read("#{Bookie::TEMPLATES_DIR}/opf.erb")
      f << ERB.new(template).result(binding)
    end

    `kindlegen #{dir}/book.opf -o #{params[:file]}`
    FileUtils.mv("#{dir}/#{params[:file]}", ".")
  end
end

#start_new_chapter(params) ⇒ Object



16
17
18
19
20
21
# File 'lib/bookie/emitters/mobi.rb', line 16

def start_new_chapter(params)
  @body =  ""

  @chapters << [params[:title], @body]
  @body << "<h1>#{params[:header]}: #{params[:title]}</h1>"
end