Class: Cheepub::Generator::Epub

Inherits:
Cheepub::Generator show all
Defined in:
lib/cheepub/generator/epub.rb

Constant Summary

Constants inherited from Cheepub::Generator

ROLES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Cheepub::Generator

#check_params, #execute, #parse_creator

Constructor Details

#initialize(src, params = Hash.new) ⇒ Epub

Returns a new instance of Epub.



11
12
13
14
15
16
# File 'lib/cheepub/generator/epub.rb', line 11

def initialize(src, params = Hash.new)
  super
  @book = GEPUB::Book.new
  @asset_store = AssetStore.new(Dir.mktmpdir("cheepub"), @src ? File.dirname(@src) : nil)
  params[:asset_store] = @asset_store
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



9
10
11
# File 'lib/cheepub/generator/epub.rb', line 9

def book
  @book
end

Instance Method Details

#add_creator(name, role) ⇒ Object



23
24
25
# File 'lib/cheepub/generator/epub.rb', line 23

def add_creator(name, role)
  @book.add_creator(name, nil, role: role)
end

#apply_params(params) ⇒ Object



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
# File 'lib/cheepub/generator/epub.rb', line 27

def apply_params(params)
  @book.identifier = params[:id] || "urn:uuid:#{SecureRandom.uuid}"
  @book.title = params[:title]
  @book.add_creator(params[:author])
  parse_creator(params[:creator])
  @book.language = params[:language] || 'ja'
  @book.version = '3.0'
  @book.publisher = params[:publisher] if params[:publisher]
  ## book.date= params[:date] || Time.now
  @book.add_date(params[:date] || Time.now, nil)
  @book.lastmodified = params[:lastModified] || Time.now
  @book.page_progression_direction = params[:pageDirection] || "ltr"
  style_content = apply_template("style.css.erb")
  @book.add_item("style.css").add_raw_content(style_content)
  @book.ordered do
    make_titlepage(params)
    nav = Cheepub::Nav.new(@content)
    @book.add_item('nav.xhtml', nil,nil,'properties'=>['nav']).add_raw_content(nav.to_html)
    @content.each_with_index do |file, idx|
      html = Cheepub::Markdown.new(file, asset_store: @asset_store).to_html
      filename = "bodymatter_#{idx}.xhtml"
      @book.add_item(filename).add_raw_content(html)
    end
    make_colophon(params)
  end
  @asset_store.each_relpath_and_content do |path, content|
    @book.add_item(path).add_raw_content(content)
  end
end

#apply_template(template_file) ⇒ Object



74
75
76
77
# File 'lib/cheepub/generator/epub.rb', line 74

def apply_template(template_file)
  template = File.read(File.join(Cheepub::TEMPLATES_DIR, template_file))
  return ERB.new(template).result(binding)
end

#make_colophon(params) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/cheepub/generator/epub.rb', line 64

def make_colophon(params)
  if params[:colophon]
    @colophon = params[:colophon]
    @colophon_before = params[:colophon_before] || ""
    @colophon_after = params[:colophon_after] || ""
    content = apply_template("colophon.xhtml.erb")
    @book.add_item("colophon.xhtml").add_raw_content(content)
  end
end

#make_titlepage(params) ⇒ Object



57
58
59
60
61
62
# File 'lib/cheepub/generator/epub.rb', line 57

def make_titlepage(params)
  if params[:titlepage]
    titlepage_content = apply_template("titlepage.xhtml.erb")
    @book.add_item("titlepage.xhtml").add_raw_content(titlepage_content)
  end
end

#output_file(params) ⇒ Object



18
19
20
21
# File 'lib/cheepub/generator/epub.rb', line 18

def output_file(params)
  output = params[:output] || "book.epub"
  @book.generate_epub(output)
end