Class: Bookie::Emitters::EPUB

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

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

#initializeEPUB

Returns a new instance of EPUB.



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

def initialize
  @chapters = []
end

Class Method Details

.extensionObject



4
5
6
# File 'lib/bookie/emitters/epub.rb', line 4

def self.extension
  ".epub"
end

Instance Method Details

#render(params) ⇒ Object



19
20
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
# File 'lib/bookie/emitters/epub.rb', line 19

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

    chapter_filenames = @chapters.length.times.map do |i|
      File.expand_path("#{dir}/#{i}.html")
    end

    chapter_outline = @chapters.map.with_index do |(title,_), i| 
      { :label => title, :content => "#{i}.html" }
    end

    epub = EeePub.make do
      title       params[:title]
      identifier  '', :scheme => 'URL'
      uid         ''
      files chapter_filenames
      nav chapter_outline
    end

    epub.save(params[:file])
  end
end

#start_new_chapter(params) ⇒ Object



12
13
14
15
16
17
# File 'lib/bookie/emitters/epub.rb', line 12

def start_new_chapter(params)
  @body =  ""

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