Class: CC2HTML::EpubBuilder

Inherits:
Builder
  • Object
show all
Defined in:
lib/cc2html/epub_builder.rb

Constant Summary collapse

TEMPLATE_DIR =
'../templates/epub/'

Instance Method Summary collapse

Constructor Details

#initialize(manifest, dest_dir, zip_file = nil) ⇒ EpubBuilder

Returns a new instance of EpubBuilder.



5
6
7
8
9
10
11
# File 'lib/cc2html/epub_builder.rb', line 5

def initialize(manifest, dest_dir, zip_file=nil)
  super(manifest, dest_dir)
  @file_name = 'cc_book'
  @content_dir = File.join(@dest_dir, 'content')
  @zip_file = zip_file
  @items_with_resource = @items.select{|i|i.resource}
end

Instance Method Details

#copy_html_webresourcesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cc2html/epub_builder.rb', line 54

def copy_html_webresources
  return unless @zip_file.end_with?('zip') || @zip_file.end_with?('imscc')
  Zip::File.open(@zip_file) do |zipfile|
    @items_with_resource.each do |item|
      if item.resource.type == 'webcontent' && item.resource.href && item.resource.href.end_with?('html')
        puts item.identifier
        path = File.join(@content_dir = File.join(@dest_dir, 'content'), item.identifierref + '.html')
        File.open(path, 'w') do |file|
          entry = zipfile.get_entry(item.resource.href)
          file << entry.get_input_stream.read
        end
      end
    end
  end
end

#create_chaptersObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cc2html/epub_builder.rb', line 40

def create_chapters
  template = File.expand_path(TEMPLATE_DIR + 'chapter.html.erb', __FILE__)
  @items_with_resource.each do |item|
    next if item.resource.type == 'webcontent'
    # so stupid... I'll fix later. :)
    @item = item
    path = File.join(@content_dir = File.join(@dest_dir, 'content'), item.identifierref + '.html')
    File.open(path, 'w') do |file|
      erb = ERB.new(File.read(template))
      file.write(erb.result(binding))
    end
  end
end

#create_meta_infObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cc2html/epub_builder.rb', line 70

def create_meta_inf
  meta_dir = File.join(@dest_dir, 'META-INF')
  FileUtils.mkdir_p(meta_dir)
  File.open(File.join(meta_dir, 'container.xml'), 'w') do |f|
    f << "<?xml version=\"1.0\"?>\n<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n   <rootfiles>\n  <rootfile full-path=\"content/\#{@file_name}.opf\" media-type=\"application/oebps-package+xml\"/>\n   </rootfiles>\n</container>\n"
  end
end

#create_mimetype_fileObject



24
25
26
27
28
# File 'lib/cc2html/epub_builder.rb', line 24

def create_mimetype_file
  File.open(File.join(@dest_dir, 'mimetype'), 'w') do |f|
    f << "application/epub+zip\n"
  end
end

#create_navObject



30
31
32
33
34
35
36
37
38
# File 'lib/cc2html/epub_builder.rb', line 30

def create_nav
  template = File.expand_path(TEMPLATE_DIR + 'navigation.html.erb', __FILE__)
  path = File.join(@content_dir = File.join(@dest_dir, 'content'), 'navigation.html')

  File.open(path, 'w') do |file|
    erb = ERB.new(File.read(template))
    file.write(erb.result(binding))
  end
end

#create_opfObject



85
86
87
88
89
90
91
92
93
# File 'lib/cc2html/epub_builder.rb', line 85

def create_opf
  template = File.expand_path(TEMPLATE_DIR + 'descriptor.opf.erb', __FILE__)
  path = File.join(@content_dir = File.join(@dest_dir, 'content'), @file_name + '.opf')

  File.open(path, 'w') do |file|
    erb = ERB.new(File.read(template))
    file.write(erb.result(binding))
  end
end

#generateObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/cc2html/epub_builder.rb', line 13

def generate
  FileUtils.mkdir_p(@dest_dir)
  create_mimetype_file
  create_meta_inf
  FileUtils.mkdir_p(@content_dir)
  create_opf
  create_nav
  create_chapters
  copy_html_webresources
end