Class: CC2HTML::EpubBuilder
- Defined in:
- lib/cc2html/epub_builder.rb
Constant Summary collapse
- TEMPLATE_DIR =
'../templates/epub/'
Instance Method Summary collapse
- #compress_into_epub ⇒ Object
- #copy_images ⇒ Object
- #copy_referenced_html_webresources ⇒ Object
- #create_chapters ⇒ Object
- #create_meta_inf ⇒ Object
- #create_mimetype_file ⇒ Object
- #create_nav ⇒ Object
- #create_opf ⇒ Object
- #generate ⇒ Object
-
#initialize(manifest, dest_name, zip_file, opts = {}) ⇒ EpubBuilder
constructor
A new instance of EpubBuilder.
Constructor Details
#initialize(manifest, dest_name, zip_file, opts = {}) ⇒ EpubBuilder
Returns a new instance of EpubBuilder.
5 6 7 8 9 10 11 12 |
# File 'lib/cc2html/epub_builder.rb', line 5 def initialize(manifest, dest_name, zip_file, opts={}) super(manifest, dest_name) @file_name = 'cc_book' @content_dir = File.join(@dest_name, 'content') @zip_file = zip_file @items_with_resource = @manifest.organizations.organization.item.all_items.select{|i|i.resource} @leave_in_folder = opts["leave_unzipped"] end |
Instance Method Details
#compress_into_epub ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cc2html/epub_builder.rb', line 27 def compress_into_epub unless @leave_in_folder epub = @dest_name + '.epub' FileUtils.rm(epub) if File.exists?(epub) Zip::File.open(epub, Zip::File::CREATE) do |zipfile| Dir["#{@dest_name}/**/**"].each do |file| file_path = file.sub(@dest_name+'/', '') zipfile.add(file_path, file) end end FileUtils.rmtree(@dest_name) end end |
#copy_images ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cc2html/epub_builder.rb', line 88 def copy_images return unless @zip_file.end_with?('zip') || @zip_file.end_with?('imscc') Zip::File.open(@zip_file) do |zipfile| @resources.each do |resource| if resource.type == 'webcontent' && resource.href && resource.href.start_with?('web_resources') && File.extname(resource.href) =~ /gif|jpg|png/i path = File.join(@content_dir, resource.href) FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') do |file| entry = zipfile.get_entry(resource.href) file << entry.get_input_stream.read end end end end end |
#copy_referenced_html_webresources ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cc2html/epub_builder.rb', line 71 def copy_referenced_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') path = File.join(@content_dir = File.join(@dest_name, 'content'), item.identifierref + '.html') File.open(path, 'w') do |file| entry = zipfile.get_entry(item.resource.href) val = entry.get_input_stream.read file << val.gsub('%24IMS_CC_FILEBASE%24', 'web_resources') # file << entry.get_input_stream.read end end end end end |
#create_chapters ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cc2html/epub_builder.rb', line 57 def create_chapters template = File.(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, 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_inf ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cc2html/epub_builder.rb', line 106 def = File.join(@dest_name, 'META-INF') FileUtils.mkdir_p() File.open(File.join(, 'container.xml'), 'w') do |f| f << <<-XML <?xml version="1.0"?> <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> <rootfiles> <rootfile full-path="content/#{@file_name}.opf" media-type="application/oebps-package+xml"/> </rootfiles> </container> XML end end |
#create_mimetype_file ⇒ Object
41 42 43 44 45 |
# File 'lib/cc2html/epub_builder.rb', line 41 def create_mimetype_file File.open(File.join(@dest_name, 'mimetype'), 'w') do |f| f << "application/epub+zip\n" end end |
#create_nav ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/cc2html/epub_builder.rb', line 47 def create_nav template = File.(TEMPLATE_DIR + 'navigation.html.erb', __FILE__) path = File.join(@content_dir, 'navigation.html') File.open(path, 'w') do |file| erb = ERB.new(File.read(template)) file.write(erb.result(binding)) end end |
#create_opf ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/cc2html/epub_builder.rb', line 121 def create_opf template = File.(TEMPLATE_DIR + 'descriptor.opf.erb', __FILE__) path = File.join(@content_dir, @file_name + '.opf') File.open(path, 'w') do |file| erb = ERB.new(File.read(template)) file.write(erb.result(binding)) end end |
#generate ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/cc2html/epub_builder.rb', line 14 def generate FileUtils.mkdir_p(@dest_name) create_mimetype_file FileUtils.mkdir_p(@content_dir) create_opf create_nav create_chapters copy_images copy_referenced_html_webresources compress_into_epub end |