Class: Burr::Epub

Inherits:
Exporter show all
Defined in:
lib/burr/exporters/epub.rb

Constant Summary

Constants inherited from Exporter

Burr::Exporter::BACKMATTER, Burr::Exporter::BODYMATTER, Burr::Exporter::FRONTMATTER

Instance Attribute Summary

Attributes inherited from Exporter

#backmatter, #bodymatter, #book, #config, #frontmatter

Instance Method Summary collapse

Methods inherited from Exporter

#initialize, #load_contents, #run, #run_plugins_of_type

Constructor Details

This class inherits a constructor from Burr::Exporter

Instance Method Details

#assemble_bookObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/burr/exporters/epub.rb', line 40

def assemble_book
  # 1. create html files
  special_elements = %w(blank)
  base = File.join(self.book.outputs_dir, 'epub')
  included_files = []
  tmp_files = []

  self.book.items.each do |item|
    next if special_elements.include?(item['element'])

    basename = if item['file'].blank?
      item['element']
    else
      item['file'].split('.')[0..-2].join('.')
    end
    html_path = File.join(base, "#{ basename }.html")
    included_files << html_path
    tmp_files << html_path

    File.open(html_path, 'w') do |f|
      f.puts self.book.render(self.book.template_for(item['element']), { 'item' => item, 'toc' => html_toc })
    end
  end

  # 2. add other files
  included_files << File.join(base, 'style.css')
  Dir.glob(File.join(self.book.outputs_dir, 'site', 'figures', '*.*')) do |figure|
    included_files << { figure => 'figures' }
  end
  included_files << File.join(base, 'cover.jpg')

  # 3. build epub file
  config = self.book.config
  nav = ncx_toc
  guide = build_guide

  epub = Burr::EpubMaker.new do
    title      config['title']
    creator    config['translator'].blank? ? config['author'] : config['translator']
    publisher  config['publisher']
    date       config['pubdate']
    identifier config['identifier'], :scheme => config['id_scheme'], :id => config['slug']
    uid        config['slug']
    language   config['language']
    cover      'cover.jpg'

    files included_files
    nav nav
    guide guide
  end

  epub.save(File.join(base, "#{self.book.config['slug']}-#{Time.new.strftime('%Y%m%d')}.epub"))

  # 4. remove useless files
  tmp_files.each do |file|
    FileUtils.remove_entry(file)
  end
end

#decorate_contentsObject

Decorate the contents with template



29
30
31
32
33
34
35
36
37
38
# File 'lib/burr/exporters/epub.rb', line 29

def decorate_contents
  decorated_items = []
  self.book.items.each do |item|
    self.book.current_item = item
    self.run_plugins_of_type(:before_decorate)
    self.run_plugins_of_type(:after_decorate)
    decorated_items << self.book.current_item
  end
  self.book.items = decorated_items
end

#parse_contentsObject

Convert original contents into HTML



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/burr/exporters/epub.rb', line 6

def parse_contents
  parsed_items = []
  self.book.items.each do |item|
    self.book.current_item = item

    # 'blank' element not include in epub
    next if item['element'] == 'blank'

    self.run_plugins_of_type(:before_parse)

    unless item['skip']
      item['content'] = Burr::Converter.new(self.book).convert(item['original'])
    end

    self.run_plugins_of_type(:after_parse)

    parsed_items << self.book.current_item
  end
  self.book.items = parsed_items
end