Class: BbEPUB::Transform::Title

Inherits:
Bookbinder::Transform
  • Object
show all
Defined in:
lib/bb-epub/transform/title.rb

Overview

EPUB2 spec:

http://www.idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#Section2.2.1

EPUB3 spec:

http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-opf-dctitle

On title types:

“When the title-type value is drawn from a code list or other formal enumeration, the scheme attribute should be attached to identify its source. When a scheme is not specified, Reading Systems should recognize the following title type values: main, subtitle, short, collection, edition and expanded.”

Constant Summary collapse

TITLE_TYPES =
%w[main subtitle short collection edition expanded]

Instance Method Summary collapse

Instance Method Details

#dependenciesObject



22
23
24
# File 'lib/bb-epub/transform/title.rb', line 22

def dependencies
  [BbEPUB::Transform::Metadata, BbEPUB::Transform::NCX]
end

#from_map(package) ⇒ Object



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
# File 'lib/bb-epub/transform/title.rb', line 54

def from_map(package)
  titles = package.map['title']
  opf_doc = package.file(:opf).document
   = opf_doc.find('opf|metadata')
  seq = 0
  titles.each_pair { |type, title|
    title_tag = opf_doc.new_node('dc:title', :append => )
    title_tag.content = title
    if titles.length > 1
      seq += 1
      title_id = "dc-title-metadata-#{seq}"
      title_tag['id'] = title_id
      opf_doc.new_node('meta', :append => ) { |type_meta_tag|
        type_meta_tag.content = type
        type_meta_tag['property'] = 'title-type'
        type_meta_tag['refines'] = '#'+title_id
      }
      opf_doc.new_node('meta', :append => ) { |seq_meta_tag|
        seq_meta_tag.content = seq
        seq_meta_tag['property'] = 'display-seq'
        seq_meta_tag['refines'] = '#'+title_id
      }
    end
  }

  # Add it to the NCX if that file exists in the package
  package.if_file(:ncx) { |ncx_file|
    ncx_title = [titles['main'], titles['subtitle']].compact.join(': ')
    ncx_doc = ncx_file.document
    ncx_doc.new_node('docTitle') { |doc_title_tag|
      ncx_doc.new_node('text', :append => doc_title_tag) { |text_tag|
        text_tag.content = ncx_title
      }
      ncx_doc.find('ncx|head').add_next_sibling(doc_title_tag)
    }
  }
end

#to_map(package) ⇒ 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
# File 'lib/bb-epub/transform/title.rb', line 27

def to_map(package)
  package.map['title'] = titles = {}
  return  unless package.map['metadata']
  return  unless  = package.map['metadata']['title']
  title_data = .sort { |a, b|
    if a['display-seq'] && b['display-seq']
      a['display-seq']['@'].to_i <=> b['display-seq']['@'].to_i
    else
      0
    end
  }
  title_data.each { |tdata|
    t = tdata['@']
    type = tdata['title-type'] ? tdata['title-type']['@'] : 'main'
    next  unless TITLE_TYPES.include?(type)
    if titles[type]
      package.warn("Existing title for '#{type}' - discarding '#{t}'")
    else
      titles[type] = t
      .delete(tdata)
    end
  }
  # Now that we have "used" the raw metadata for titles, remove it.
  package.map['metadata'].delete('title')  if .empty?
end