Class: EPUBMaker::EPUBv3

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

Overview

EPUBv3 is EPUB version 3 producer.

Instance Method Summary collapse

Methods inherited from EPUBCommon

#colophon, #colophon_history, #container, #cover, #date_to_s, #flat_ncx, #hierarchy_ncx, #join_with_separator, #legacy_cover_and_title_file, #mimetype, #mytoc, #ncx_doctitle, #ncx_isbn, #ncx_navmap, #opf_coverimage, #opf_path, #produce_write_common, #titlepage

Constructor Details

#initialize(producer) ⇒ EPUBv3

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



18
19
20
21
22
23
24
# File 'lib/epubmaker/epubv3.rb', line 18

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

Instance Method Details

#ncx(indentarray) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/epubmaker/epubv3.rb', line 200

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

  @body = <<EOT
  <nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
  <h1 class="toc-title">#{CGI.escapeHTML(@producer.res.v('toctitle'))}</h1>
#{ncx_main}  </nav>
EOT

  @title = CGI.escapeHTML(@producer.res.v('toctitle'))
  @language = @producer.config['language']
  @stylesheets = @producer.config['stylesheet']
  tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
  ReVIEW::Template.load(tmplfile).result(binding)
end

#opfObject

Return opf file content.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/epubmaker/epubv3.rb', line 27

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

  tmplfile = File.expand_path('./opf/epubv3.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
  ReVIEW::Template.load(tmplfile).result(binding)
end

#opf_manifestObject



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
166
167
# File 'lib/epubmaker/epubv3.rb', line 139

def opf_manifest
  s = ''
  s << <<EOT
  <manifest>
<item properties="nav" id="#{@producer.config['bookname']}-toc.#{@producer.config['htmlext']}" href="#{@producer.config['bookname']}-toc.#{@producer.config['htmlext']}" media-type="application/xhtml+xml"/>
<item id="#{@producer.config['bookname']}" href="#{@producer.config['cover']}" media-type="application/xhtml+xml"/>
EOT

  if @producer.config['coverimage']
    @producer.contents.each do |item|
      next if !item.media.start_with?('image') || File.basename(item.file) != @producer.config['coverimage']
      s << %Q(    <item properties="cover-image" id="cover-#{item.id}" href="#{item.file}" media-type="#{item.media}"/>\n)
      item.id = nil
      break
    end
  end

  @producer.contents.each do |item|
    next if item.file =~ /#/ || item.id.nil? # skip subgroup, or id=nil (for cover)
    propstr = ''
    if item.properties.size > 0
      propstr = %Q( properties="#{item.properties.sort.uniq.join(' ')}")
    end
    s << %Q(    <item id="#{item.id}" href="#{item.file}" media-type="#{item.media}"#{propstr}/>\n)
  end
  s << %Q(  </manifest>\n)

  s
end

#opf_metainfoObject



43
44
45
46
47
48
49
50
51
52
53
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
91
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/epubmaker/epubv3.rb', line 43

def opf_metainfo
  s = ''
  %w[title language date type format source description relation coverage subject rights].each do |item|
    next unless @producer.config[item]
    if @producer.config[item].is_a?(Array)
      @producer.config[item].each_with_index do |v, i|
        if v.is_a?(Hash)
          s << %Q(    <dc:#{item} id="#{item}-#{i}">#{CGI.escapeHTML(v['name'])}</dc:#{item}>\n)
          v.each_pair do |name, val|
            next if name == 'name'
            s << %Q(    <meta refines="##{item}-#{i}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n)
          end
        else
          s << %Q(    <dc:#{item} id="#{item}-#{i}">#{CGI.escapeHTML(v.to_s)}</dc:#{item}>\n)
        end
      end
    elsif @producer.config[item].is_a?(Hash)
      s << %Q(    <dc:#{item} id="#{item}">#{CGI.escapeHTML(@producer.config[item]['name'])}</dc:#{item}>\n)
      @producer.config[item].each_pair do |name, val|
        next if name == 'name'
        s << %Q(    <meta refines="##{item}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n)
      end
    else
      s << %Q(    <dc:#{item} id="#{item}">#{CGI.escapeHTML(@producer.config[item].to_s)}</dc:#{item}>\n)
    end
  end

  s << %Q(    <meta property="dcterms:modified">#{@producer.config['modified']}</meta>\n)

  # ID
  if @producer.config['isbn'].nil?
    s << %Q(    <dc:identifier id="BookId">#{@producer.config['urnid']}</dc:identifier>\n)
  else
    s << %Q(    <dc:identifier id="BookId">#{@producer.config['isbn']}</dc:identifier>\n)
  end

  # creator (should be array)
  %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].each do |role|
    next unless @producer.config[role]
    @producer.config[role].each_with_index do |v, i|
      if v.is_a?(Hash)
        s << %Q(    <dc:creator id="#{role}-#{i}">#{CGI.escapeHTML(v['name'])}</dc:creator>\n)
        s << %Q(    <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role.sub('a-', '')}</meta>\n)
        v.each_pair do |name, val|
          next if name == 'name'
          s << %Q(    <meta refines="##{role.sub('a-', '')}-#{i}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n)
        end
      else
        s << %Q(    <dc:creator id="#{role}-#{i}">#{CGI.escapeHTML(v)}</dc:creator>\n)
        s << %Q(    <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role.sub('a-', '')}</meta>\n)
      end
    end
  end

  # contributor (should be array)
  %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].each do |role|
    next unless @producer.config[role]
    @producer.config[role].each_with_index do |v, i|
      if v.is_a?(Hash)
        s << %Q(    <dc:contributor id="#{role}-#{i}">#{CGI.escapeHTML(v['name'])}</dc:contributor>\n)
        s << %Q(    <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n)
        v.each_pair do |name, val|
          next if name == 'name'
          s << %Q(    <meta refines="##{role}-#{i}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n)
        end
      else
        s << %Q(    <dc:contributor id="#{role}-#{i}">#{CGI.escapeHTML(v)}</dc:contributor>\n)
        s << %Q(    <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n)
      end

      if %w[prt pbl].include?(role)
        if v.is_a?(Hash)
          s << %Q(    <dc:publisher id="pub-#{role}-#{i}">#{CGI.escapeHTML(v['name'])}</dc:publisher>\n)
          s << %Q(    <meta refines="#pub-#{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n)
          v.each_pair do |name, val|
            next if name == 'name'
            s << %Q(    <meta refines="#pub-#{role}-#{i}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n)
          end
        else
          s << %Q(    <dc:publisher id="pub-#{role}-#{i}">#{CGI.escapeHTML(v)}</dc:publisher>\n)
          s << %Q(    <meta refines="#pub-#{role}-#{i}" property="role" scheme="marc:relators">prt</meta>\n)
        end
      end
    end
  end

  ## add custom <meta> element
  if @producer.config['opf_meta'].present?
    @producer.config['opf_meta'].each do |k, v|
      s << %Q(    <meta property="#{k}">#{CGI.escapeHTML(v)}</meta>\n)
    end
  end

  s
end

#opf_tocxObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/epubmaker/epubv3.rb', line 169

def opf_tocx
  if @producer.config['epubmaker']['cover_linear'] && @producer.config['epubmaker']['cover_linear'] != 'no'
    cover_linear = 'yes'
  else
    cover_linear = 'no'
  end

  s = ''
  if @producer.config['direction']
    s << %Q(  <spine page-progression-direction="#{@producer.config['direction']}">\n)
  else
    s << %Q(  <spine>\n)
  end
  s << %Q(    <itemref idref="#{@producer.config['bookname']}" linear="#{cover_linear}"/>\n)

  toc = nil
  @producer.contents.each do |item|
    next if item.media !~ /xhtml\+xml/ # skip non XHTML
    if toc.nil? && item.chaptype != 'pre'
      if @producer.config['toc']
        s << %Q(    <itemref idref="#{@producer.config['bookname']}-toc.#{@producer.config['htmlext']}" />\n)
      end
      toc = true
    end
    s << %Q(    <itemref idref="#{item.id}"/>\n)
  end
  s << %Q(  </spine>\n)

  s
end

#produce(epubfile, basedir, tmpdir) ⇒ Object

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



223
224
225
226
227
228
229
230
231
# File 'lib/epubmaker/epubv3.rb', line 223

def produce(epubfile, basedir, tmpdir)
  produce_write_common(basedir, tmpdir)

  File.open("#{tmpdir}/OEBPS/#{@producer.config['bookname']}-toc.#{@producer.config['htmlext']}", 'w') { |f| @producer.ncx(f, @producer.config['epubmaker']['ncxindent']) }

  @producer.call_hook(@producer.config['epubmaker']['hook_prepack'], tmpdir)
  expoter = EPUBMaker::ZipExporter.new(tmpdir, @producer.config)
  expoter.export_zip(epubfile)
end