Class: EPUBMaker::EPUBCommon

Inherits:
Object
  • Object
show all
Defined in:
lib/epubmaker/epubcommon.rb

Overview

EPUBCommon is the common class for EPUB producer.

Direct Known Subclasses

EPUBv2, EPUBv3

Instance Method Summary collapse

Constructor Details

#initialize(producer) ⇒ EPUBCommon

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



21
22
23
# File 'lib/epubmaker/epubcommon.rb', line 21

def initialize(producer)
  @producer = producer
end

Instance Method Details

#colophonObject

Return colophon content.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/epubmaker/epubcommon.rb', line 204

def colophon
  s = common_header
  s << <<EOT
  <title>#{@producer.res.v("colophontitle")}</title>
</head>
<body>
  <div class="colophon">
EOT

  if @producer.params["subtitle"].nil?
    s << <<EOT
<p class="title">#{CGI.escapeHTML(@producer.params["title"])}</p>
EOT
  else
    s << <<EOT
<p class="title">#{CGI.escapeHTML(@producer.params["title"])}<br /><span class="subtitle">#{CGI.escapeHTML(@producer.params["subtitle"])}</span></p>
EOT
  end

  if @producer.params["date"] || @producer.params["history"]
    s << %Q[    <div class="pubhistory">\n]
    if @producer.params["history"]
      @producer.params["history"].each_with_index do |items, edit|
        items.each_with_index do |item, rev|
          editstr = (edit == 0) ? ReVIEW::I18n.t("first_edition") : ReVIEW::I18n.t("nth_edition","#{edit+1}")
          revstr = ReVIEW::I18n.t("nth_impression", "#{rev+1}")
          if item =~ /\A\d+\-\d+\-\d+\Z/
            s << %Q[      <p>#{ReVIEW::I18n.t("published_by1", [date_to_s(item), editstr+revstr])}</p>\n]
          else
            # custom date with string
            item.match(/\A(\d+\-\d+\-\d+)[\s ](.+)/) do |m|
              s << %Q[      <p>#{ReVIEW::I18n.t("published_by3", [date_to_s(m[1]), m[2]])}</p>\n]
            end
          end
        end
      end
    else
      s << %Q[      <p>#{ReVIEW::I18n.t("published_by2", date_to_s(@producer.params["date"]))}</p>\n]
    end
    s << %Q[    </div>\n]
  end

  s << %Q[    <table class="colophon">\n]
  s << %Q[      <tr><th>#{@producer.res.v("aut")}</th><td>#{CGI.escapeHTML(@producer.params["aut"].join(", "))}</td></tr>\n] unless @producer.params["aut"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("csl")}</th><td>#{CGI.escapeHTML(@producer.params["csl"].join(", "))}</td></tr>\n] unless @producer.params["csl"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("trl")}</th><td>#{CGI.escapeHTML(@producer.params["trl"].join(", "))}</td></tr>\n] unless @producer.params["trl"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("dsr")}</th><td>#{CGI.escapeHTML(@producer.params["dsr"].join(", "))}</td></tr>\n] unless @producer.params["dsr"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("ill")}</th><td>#{CGI.escapeHTML(@producer.params["ill"].join(", "))}</td></tr>\n] unless @producer.params["ill"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("edt")}</th><td>#{CGI.escapeHTML(@producer.params["edt"].join(", "))}</td></tr>\n] unless @producer.params["edt"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("pbl")}</th><td>#{CGI.escapeHTML(@producer.params["pbl"].join(", "))}</td></tr>\n] unless @producer.params["pbl"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("prt")}</th><td>#{CGI.escapeHTML(@producer.params["prt"].join(", "))}</td></tr>\n] unless @producer.params["prt"].nil?
  s << %Q[      <tr><th>#{@producer.res.v("pht")}</th><td>#{CGI.escapeHTML(@producer.params["pht"].join(", "))}</td></tr>\n] unless @producer.params["pht"].nil?
  if @producer.params["isbn"].to_s =~ /\A\d{10}\Z/ || @producer.params["isbn"].to_s =~ /\A\d{13}\Z/
    isbn = nil
    str = @producer.params["isbn"].to_s
    if str.size == 10
      isbn = "#{str[0..0]}-#{str[1..5]}-#{str[6..8]}-#{str[9..9]}"
    else
      isbn = "#{str[0..2]}-#{str[3..3]}-#{str[4..8]}-#{str[9..11]}-#{str[12..12]}"
    end
    s << %Q[      <tr><th>ISBN</th><td>#{isbn}</td></tr>\n]
  end
  s << <<EOT
</table>
EOT
  if !@producer.params["rights"].nil? && @producer.params["rights"].size > 0
    s << %Q[    <p class="copyright">#{@producer.params["rights"].join("<br />")}</p>]
  end

  s << <<EOT
  </div>
</body>
</html>
EOT
  s
end

#containerObject

Return container content.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/epubmaker/epubcommon.rb', line 113

def container
  s = <<EOT
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
  <rootfiles>
<rootfile full-path="OEBPS/#{@producer.params["bookname"]}.opf" media-type="application/oebps-package+xml" />
  </rootfiles>
</container>
EOT
  s
end

#cover(type = nil) ⇒ Object

Return cover content.



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
# File 'lib/epubmaker/epubcommon.rb', line 126

def cover(type=nil)
  bodyext = type.nil? ? "" : " epub:type=\"#{type}\""

  s = common_header
  s << <<EOT
  <title>#{CGI.escapeHTML(@producer.params["title"])}</title>
</head>
<body#{bodyext}>
EOT
  if @producer.params["coverimage"].nil?
    s << <<EOT
<h1 class="cover-title">#{CGI.escapeHTML(@producer.params["title"])}</h1>
EOT
  else
    file = nil
    @producer.contents.each do |item|
      if item.media =~ /\Aimage/ && item.file =~ /#{@producer.params["coverimage"]}\Z/ # /
        file = item.file
        break
      end
    end
    raise "coverimage #{@producer.params["coverimage"]} not found. Abort." if file.nil?
    s << <<EOT
  <div id="cover-image" class="cover-image">
<img src="#{file}" alt="#{CGI.escapeHTML(@producer.params["title"])}" class="max"/>
  </div>
EOT
  end

  s << <<EOT
</body>
</html>
EOT
  s
end

#date_to_s(date) ⇒ Object



281
282
283
284
285
# File 'lib/epubmaker/epubcommon.rb', line 281

def date_to_s(date)
  require 'date'
  d = Date.parse(date)
  d.strftime(ReVIEW::I18n.t("date_format"))
end

#export_zip(tmpdir, epubfile) ⇒ Object



413
414
415
416
# File 'lib/epubmaker/epubcommon.rb', line 413

def export_zip(tmpdir, epubfile)
  Dir.chdir(tmpdir) {|d| `#{@producer.params["epubmaker"]["zip_stage1"]} #{epubfile} mimetype` }
  Dir.chdir(tmpdir) {|d| `#{@producer.params["epubmaker"]["zip_stage2"]} #{epubfile} META-INF OEBPS #{@producer.params["epubmaker"]["zip_addpath"]}` }
end

#flat_ncx(type, indent = nil) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
# File 'lib/epubmaker/epubcommon.rb', line 377

def flat_ncx(type, indent=nil)
  s = %Q[<#{type} class="toc-h1">\n]
  @producer.contents.each do |item|
    next if !item.notoc.nil? || item.level.nil? || item.file.nil? || item.title.nil? || item.level > @producer.params["toclevel"].to_i
    is = indent == true ? " " * item.level : ""
    s << %Q[<li><a href="#{item.file}">#{is}#{CGI.escapeHTML(item.title)}</a></li>\n]
  end
  s << %Q[</#{type}>\n]

  s
end

#hierarchy_ncx(type) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/epubmaker/epubcommon.rb', line 310

def hierarchy_ncx(type)
  require 'rexml/document'
  level = 1
  find_jump = nil
  has_part = nil
  toclevel = @producer.params["toclevel"].to_i

  # check part existance
  @producer.contents.each do |item|
    if item.notoc.nil? && item.chaptype == "part"
      has_part = true
      break
    end
  end

  if !has_part.nil?
    @producer.contents.each do |item|
      item.level += 1 if item.chaptype == "part" || item.chaptype == "body"
      item.notoc = true if (item.chaptype == "pre" || item.chaptype == "post") && !item.level.nil? && (item.level + 1 == toclevel) # FIXME: 部があるときに前後の処理が困難
    end
    toclevel += 1
  end

  doc = REXML::Document.new(%Q[<#{type} class="toc-h#{level}"><li /></#{type}>])
  doc.context[:attribute_quote] = :quote

  e = doc.root.elements[1] # first <li/>
  @producer.contents.each do |item|
    next if !item.notoc.nil? || item.level.nil? || item.file.nil? || item.title.nil? || item.level > toclevel

    if item.level == level
      e2 = e.parent.add_element("li")
      e = e2
    elsif item.level > level
      find_jump = true if (item.level - level) > 1
      # deeper
      (level + 1).upto(item.level) do |n|
        if e.size == 0
          # empty span for epubcheck
          e.attributes["style"] = "list-style-type: none;"
          es = e.add_element("span", {"style"=>"display:none;"})
          es.add_text(REXML::Text.new("&#xa0;", false, nil, true))
        end

        e2 = e.add_element(type, {"class" => "toc-h#{n}"})
        e3 = e2.add_element("li")
        e = e3
      end
      level = item.level
    elsif item.level < level
      # shallower
      (level - 1).downto(item.level) do |n|
        e = e.parent.parent
      end
      e2 = e.parent.add_element("li")
      e = e2
      level = item.level
    end
    e2 = e.add_element("a", {"href" => item.file})
    e2.add_text(REXML::Text.new(item.title, true))
  end

  warn "found level jumping in table of contents. consider to use 'epubmaker:flattoc: true' for strict ePUB validator." unless find_jump.nil?

  doc.to_s.gsub("<li/>", "").gsub("</li>", "</li>\n").gsub("<#{type} ", "\n" + '\&') # ugly
end

#legacy_cover_and_title_file(loadfile, writefile) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/epubmaker/epubcommon.rb', line 418

def legacy_cover_and_title_file(loadfile, writefile)
  s = common_header
  s << <<EOT
  <title>#{@producer.params["booktitle"]}</title>
</head>
<body>
EOT
  File.open(loadfile) do |f|
    f.each_line do |l|
      s << l
    end
  end
  s << <<EOT
</body>
</html>
EOT

  File.open(writefile, "w") do |f|
    f.puts s
  end
end

#mimetypeObject

Return mimetype content.



26
27
28
# File 'lib/epubmaker/epubcommon.rb', line 26

def mimetype
  "application/epub+zip"
end

#mytocObject

Return own toc content.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/epubmaker/epubcommon.rb', line 288

def mytoc
  s = common_header
  s << <<EOT
  <title>#{@producer.res.v("toctitle")}</title>
</head>
<body>
  <h1 class="toc-title">#{@producer.res.v("toctitle")}</h1>
EOT

  if @producer.params["epubmaker"]["flattoc"].nil?
    s << hierarchy_ncx("ul")
  else
    s << flat_ncx("ul", @producer.params["epubmaker"]["flattocindent"])
  end

  s << <<EOT
</body>
</html>
EOT
  s
end

#ncx_doctitleObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/epubmaker/epubcommon.rb', line 54

def ncx_doctitle
  <<EOT
  <docTitle>
<text>#{CGI.escapeHTML(@producer.params["title"])}</text>
  </docTitle>
  <docAuthor>
<text>#{@producer.params["aut"].nil? ? "" : CGI.escapeHTML(@producer.params["aut"].join(", "))}</text>
  </docAuthor>
EOT
end

#ncx_isbnObject



46
47
48
49
50
51
52
# File 'lib/epubmaker/epubcommon.rb', line 46

def ncx_isbn
  if @producer.params["isbn"].nil?
    %Q[    <meta name="dtb:uid" content="#{@producer.params["urnid"]}"/>\n]
  else
    %Q[    <meta name="dtb:uid" content="#{@producer.params["isbn"]}"/>\n]
  end
end

#ncx_navmap(indentarray) ⇒ Object



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
# File 'lib/epubmaker/epubcommon.rb', line 65

def ncx_navmap(indentarray)
  s = <<EOT
  <navMap>
<navPoint id="top" playOrder="1">
  <navLabel>
    <text>#{CGI.escapeHTML(@producer.params["title"])}</text>
  </navLabel>
  <content src="#{@producer.params["cover"]}"/>
</navPoint>
EOT

  nav_count = 2

  unless @producer.params["mytoc"].nil?
    s << <<EOT
<navPoint id="toc" playOrder="#{nav_count}">
  <navLabel>
    <text>#{@producer.res.v("toctitle")}</text>
  </navLabel>
  <content src="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}"/>
</navPoint>
EOT
    nav_count += 1
  end

  @producer.contents.each do |item|
    next if item.title.nil?
    indent = indentarray.nil? ? [""] : indentarray
    level = item.level.nil? ? 0 : (item.level - 1)
    level = indent.size - 1 if level >= indent.size
    s << <<EOT
<navPoint id="nav-#{nav_count}" playOrder="#{nav_count}">
  <navLabel>
    <text>#{indent[level]}#{CGI.escapeHTML(item.title)}</text>
  </navLabel>
  <content src="#{item.file}"/>
</navPoint>
EOT
    nav_count += 1
  end

  s << <<EOT
  </navMap>
EOT
  s
end

#opf_coverimageObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/epubmaker/epubcommon.rb', line 30

def opf_coverimage
  s = ""
  if @producer.params["coverimage"]
    file = nil
    @producer.contents.each do |item|
      if item.media =~ /\Aimage/ && item.file =~ /#{@producer.params["coverimage"]}\Z/
        s << %Q[    <meta name="cover" content="#{item.id}"/>\n]
        file = item.file
        break
      end
    end
    raise "coverimage #{@producer.params["coverimage"]} not found. Abort." if file.nil?
  end
  s
end

#produce_write_common(basedir, tmpdir) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/epubmaker/epubcommon.rb', line 389

def produce_write_common(basedir, tmpdir)
  File.open("#{tmpdir}/mimetype", "w") {|f| @producer.mimetype(f) }

  Dir.mkdir("#{tmpdir}/META-INF") unless File.exist?("#{tmpdir}/META-INF")
  File.open("#{tmpdir}/META-INF/container.xml", "w") {|f| @producer.container(f) }

  Dir.mkdir("#{tmpdir}/OEBPS") unless File.exist?("#{tmpdir}/OEBPS")
  File.open("#{tmpdir}/OEBPS/#{@producer.params["bookname"]}.opf", "w") {|f| @producer.opf(f) }

  if File.exist?("#{basedir}/#{@producer.params["cover"]}")
    FileUtils.cp("#{basedir}/#{@producer.params["cover"]}", "#{tmpdir}/OEBPS")
  else
    File.open("#{tmpdir}/OEBPS/#{@producer.params["cover"]}", "w") {|f| @producer.cover(f) }
  end

  @producer.contents.each do |item|
    next if item.file =~ /#/ # skip subgroup
    fname = "#{basedir}/#{item.file}"
    raise "#{fname} doesn't exist. Abort." unless File.exist?(fname)
    FileUtils.mkdir_p(File.dirname("#{tmpdir}/OEBPS/#{item.file}")) unless File.exist?(File.dirname("#{tmpdir}/OEBPS/#{item.file}"))
    FileUtils.cp(fname, "#{tmpdir}/OEBPS/#{item.file}")
  end
end

#titlepageObject

Return title (copying) content.



163
164
165
166
167
168
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
199
200
201
# File 'lib/epubmaker/epubcommon.rb', line 163

def titlepage
  s = common_header
  s << <<EOT
  <title>#{CGI.escapeHTML(@producer.params["title"])}</title>
</head>
<body>
  <h1 class="tp-title">#{CGI.escapeHTML(@producer.params["title"])}</h1>
EOT

  if @producer.params["aut"]
    s << <<EOT
  <p>
<br />
<br />
  </p>
  <h2 class="tp-author">#{CGI.escapeHTML(@producer.params["aut"].join(", "))}</h2>
EOT
  end

  publisher = @producer.params["pbl"] || @producer.params["prt"] # XXX Backward Compatiblity
  if publisher
    s << <<EOT
  <p>
<br />
<br />
<br />
<br />
  </p>
  <h3 class="tp-publisher">#{CGI.escapeHTML(publisher.join(", "))}</h3>
EOT
  end

  s << <<EOT
</body>
</html>
EOT

  s
end