Class: Bookbinder::Transform::EPUB_CoverImage

Inherits:
Bookbinder::Transform show all
Defined in:
lib/bookbinder/transform/epub/cover_image.rb

Overview

The best source of information about wading through the EPUB cover image quagmire has always been Keith’s article on the Threepress blog:

http://blog.safaribooksonline.com/2009/11/20/best-practices-in-epub-cover-images/

He added an update for EPUB3, which follows the spec but is a bit easier to grok:

http://blog.safaribooksonline.com/2011/05/26/covers-in-epub3/

Instance Method Summary collapse

Instance Method Details

#dependenciesObject



14
15
16
17
18
19
# File 'lib/bookbinder/transform/epub/cover_image.rb', line 14

def dependencies
  [
    Bookbinder::Transform::EPUB_Resources,
    Bookbinder::Transform::
  ]
end

#from_map(package) ⇒ Object

Belt and braces: give the manifest item a property of ‘cover-image’, an ‘id’ of ‘cover-image’ (updating any idrefs) and create a meta tag with ‘name’=‘cover’ and ‘content’=‘cover-image’.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bookbinder/transform/epub/cover_image.rb', line 58

def from_map(package)
  return  unless package.map['cover'] && cover = package.map['cover']['front']
  opf_doc = package.file(:opf).document

  opf_doc.new_node('item', :append => 'opf|manifest') { |manifest_item_tag|
    manifest_item_tag['href'] = package.make_href(cover['path'])
    manifest_item_tag['media-type'] = cover['media-type']
    manifest_item_tag['id'] = 'cover-image'
    manifest_item_tag['properties'] = 'cover-image'
  }

  cover_id = package.make_id(cover['path'])
  opf_doc.each('[idref="'+cover_id+'"]') { |idref|
    idref['idref'] = cover_id
  }

  opf_doc.new_node('meta', :append => 'opf|metadata') { |cover_meta_tag|
    cover_meta_tag['name'] = 'cover'
    cover_meta_tag['content'] = 'cover-image'
  }
end

#to_map(package) ⇒ Object

If it’s EPUB3, the cover will be in the ‘properties’ attribute of the manifest item: ‘cover-image’

Otherwise, look for a manifest item with an ‘id’ of ‘cover-image’.

Or, look for a meta tag with a ‘name’ of ‘cover’, then find the manifest item that has the ‘id’ that matches meta’s ‘content’.

Set map to this item (and remove it from map).



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bookbinder/transform/epub/cover_image.rb', line 32

def to_map(package)
  opf_doc = package.file(:opf).document('r')
  cover_item = opf_doc.find('opf|manifest > opf|item[properties~="cover-image"]')
  cover_item ||= opf_doc.find('opf|manifest > opf|item[id="cover-image"]')
  cover_item ||= opf_doc.find('opf|manifest > opf|item[id="cover_image"]')
  cover_meta_props = (package.map['metadata'] || {}).delete('cover')
  if !cover_item && cover_meta_props && cover_meta_props.any?
    cover_image_id = cover_meta_props.first['content']['@']
    cover_item = opf_doc.find('opf|manifest > opf|item[id="'+cover_image_id+'"]')
  end
  covers = {}
  if cover_item
    rsrc = package.map['resources'].detect { |r|
      r['id'] == cover_item['id']
    }
    covers.update("front" => package.map['resources'].delete(rsrc))
  end
  package.map['cover'] = covers
end