Class: ReVIEW::EPUBMaker::EPUBv3

Inherits:
EPUBCommon show all
Includes:
CallHook
Defined in:
lib/review/epubmaker/epubv3.rb

Overview

EPUBv3 is EPUB version 3 producer.

Constant Summary collapse

DC_ITEMS =
%w[title language date type format source description relation coverage subject rights]
CREATOR_ATTRIBUTES =
%w[a-adp a-ann a-arr a-art a-asn a-aqt a-aft a-aui a-ant a-bkp a-clb a-cmm a-csl a-dsr a-edt a-ill a-lyr a-mdc a-mus a-nrt a-oth a-pht a-prt a-red a-rev a-spn a-ths a-trc a-trl aut]
CONTRIBUTER_ATTRIBUTES =
%w[adp ann arr art asn aqt aft aui ant bkp clb cmm csl dsr edt ill lyr mdc mus nrt oth pbd pbl pht prt red rev spn ths trc trl]

Instance Attribute Summary

Attributes inherited from EPUBCommon

#config, #contents

Instance Method Summary collapse

Methods included from CallHook

#call_hook

Methods inherited from EPUBCommon

#call_hook, #colophon, #colophon_history, #container, #cover, #coverimage, #date_to_s, #flat_ncx, #h, #hierarchy_ncx, #isbn_hyphen, #join_with_separator, #legacy_cover_and_title_file, #mimetype, #mytoc, #opf_coverimage, #opf_path, #produce_write_common, #template_name, #titlepage

Constructor Details

#initialize(producer) ⇒ EPUBv3

Construct object with parameter hash config and message resource hash res.



26
27
28
29
30
31
32
# File 'lib/review/epubmaker/epubv3.rb', line 26

def initialize(producer)
  super
  @opf_prefix = {}
  if config['opf_prefix'].present?
    config['opf_prefix'].each { |k, v| @opf_prefix[k] = v }
  end
end

Instance Method Details

#coveritemObject



216
217
218
219
220
221
222
# File 'lib/review/epubmaker/epubv3.rb', line 216

def coveritem
  if config['cover']
    [Content.new(file: config['cover'], title: ReVIEW::I18n.t('covertitle'), level: 1, chaptype: 'cover')]
  else
    []
  end
end

#ncx(indentarray) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/review/epubmaker/epubv3.rb', line 197

def ncx(indentarray)
  ncx_main = if config['epubmaker']['flattoc'].nil?
               hierarchy_ncx('ol')
             else
               flat_ncx('ol', config['epubmaker']['flattocindent'])
             end

  @body = <<-EOT
  <nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
  <h1 class="toc-title">#{h(ReVIEW::I18n.t('toctitle'))}</h1>
#{ncx_main}  </nav>
EOT

  @title = h(ReVIEW::I18n.t('toctitle'))
  @language = config['language']
  @stylesheets = config['stylesheet']
  ReVIEW::Template.generate(path: template_name, binding: binding)
end

#opfObject

Return opf file content.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/review/epubmaker/epubv3.rb', line 35

def opf
  @opf_metainfo = opf_metainfo
  @opf_coverimage = opf_coverimage
  @opf_manifest = opf_manifest
  @opf_toc = opf_tocx
  @package_attrs = ''

  if @opf_prefix && @opf_prefix.size > 0
    prefixes_str = @opf_prefix.map { |k, v| %Q(#{k}: #{v}) }.join(' ')
    @package_attrs << %Q( prefix="#{prefixes_str}")
  end

  ReVIEW::Template.generate(path: './opf/epubv3.opf.erb', binding: binding)
end

#opf_contributersObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/review/epubmaker/epubv3.rb', line 121

def opf_contributers
  CONTRIBUTER_ATTRIBUTES.map do |role|
    next unless config[role]

    config[role].map.with_index do |v, i|
      case v
      when Hash
        refines = v.map { |name, val| { id: "#{role}-#{i}", property: name, scheme: nil, val: val } }.delete_if { |h| h[:property] == 'name' }
        contributer = {
          id: "#{role}-#{i}",
          val: v['name'],
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role }
          ].concat(refines)
        }
      else
        contributer = {
          id: "#{role}-#{i}",
          val: v,
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role }
          ]
        }
      end
      if %w[prt pbl].include?(role)
        contributer[:pub_id] = "pub-#{role}-#{i}"
        case v
        when Hash
          contributer[:pub_val] = v['name']
          pub_refines = v.map { |name, val| { id: "pub-#{role}-#{i}", property: name, scheme: nil, val: val } }.delete_if { |h| h[:property] == 'name' }
          contributer[:pub_refines] = [
            { id: "pub-#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role }
          ].concat(pub_refines)
        else
          contributer[:pub_val] = v
          contributer[:pub_refines] = [
            { id: "pub-#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: 'prt' }
          ]
        end
      end

      contributer
    end
  end.flatten.compact
end

#opf_creatorsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/review/epubmaker/epubv3.rb', line 92

def opf_creators
  CREATOR_ATTRIBUTES.map do |role|
    next unless config[role]

    config[role].map.with_index do |v, i|
      case v
      when Hash
        refines = v.map { |name, val| { id: "#{role.sub('a-', '')}-#{i}", property: name.to_s, scheme: nil, val: val } }.delete_if { |h| h[:property] == 'name' }

        {
          id: "#{role}-#{i}",
          val: v['name'],
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role.sub('a-', '') }
          ].concat(refines)
        }
      else
        {
          id: "#{role}-#{i}",
          val: v,
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role.sub('a-', '') }
          ]
        }
      end
    end
  end.flatten.compact
end

#opf_dc_itemsObject



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/review/epubmaker/epubv3.rb', line 62

def opf_dc_items
  DC_ITEMS.map do |item|
    next unless config[item]

    case config[item]
    when Array
      config[item].map.with_index do |v, i|
        if v.is_a?(Hash)
          { tag: "dc:#{item}",
            id: "#{item}-#{i}",
            val: v['name'],
            refines: v.map { |name, val| { name: name, val: val } }.delete_if { |h| h[:name] == 'name' } }
        else
          { tag: "dc:#{item}", id: "#{item}-#{i}", val: v.to_s, refines: [] }
        end
      end
    when Hash
      { tag: "dc:#{item}",
        id: item.to_s,
        val: config[item]['name'],
        refines: config[item].map { |name, val| { name: name, val: val } }.delete_if { |h| h[:name] == 'name' } }
    else
      { tag: "dc:#{item}",
        id: item.to_s,
        val: config[item].to_s,
        refines: [] }
    end
  end.flatten.compact
end

#opf_manifestObject



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/review/epubmaker/epubv3.rb', line 167

def opf_manifest
  if config['coverimage']
    @coverimage = contents.find { |content| content.coverimage?(config['coverimage']) } # @coverimage can be nil
  end
  @items = if @coverimage
             contents.find_all { |content| content.file !~ /#/ && content.id != @coverimage.id } # skip subgroup, or @coverimage
           else
             contents.find_all { |content| content.file !~ /#/ }
           end

  ReVIEW::Template.generate(path: './opf/opf_manifest_epubv3.opf.erb', binding: binding)
end

#opf_metainfoObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/review/epubmaker/epubv3.rb', line 50

def opf_metainfo
  @dc_items = opf_dc_items

  # creator (should be array)
  @creators = opf_creators

  # contributor (should be array)
  @contributers = opf_contributers

  ReVIEW::Template.generate(path: './opf/opf_metainfo_epubv3.opf.erb', binding: binding)
end

#opf_tocxObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/review/epubmaker/epubv3.rb', line 180

def opf_tocx
  @cover_linear = if config['epubmaker']['cover_linear'] && config['epubmaker']['cover_linear'] != 'no'
                    'yes'
                  else
                    'no'
                  end
  @tocx_contents = []
  toc = nil
  contents.each do |item|
    next unless /xhtml\+xml/.match?(item.media) # skip non XHTML

    @tocx_contents << item
  end

  ReVIEW::Template.generate(path: './opf/opf_tocx_epubv3.opf.erb', binding: binding)
end

#produce(epubfile, work_dir, tmpdir, base_dir:) ⇒ Object

Produce EPUB file epubfile. work_dir points the directory has contents. tmpdir defines temporary directory.



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/review/epubmaker/epubv3.rb', line 227

def produce(epubfile, work_dir, tmpdir, base_dir:)
  @workdir = base_dir
  produce_write_common(work_dir, tmpdir)

  toc_file = "#{tmpdir}/OEBPS/#{config['bookname']}-toc.#{config['htmlext']}"
  File.write(toc_file, ncx(config['epubmaker']['ncxindent']))

  call_hook('hook_prepack', tmpdir, base_dir: @workdir)
  expoter = ReVIEW::EPUBMaker::ZipExporter.new(tmpdir, config)
  expoter.export_zip(epubfile)
end