Class: EPUBMaker::EPUBv3

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

Overview

EPUBv3 is EPUB version 3 producer.

Instance Method Summary collapse

Methods inherited from EPUBv2

#colophon, #container, #cover, #initialize, #mimetype, #mytoc, #titlepage

Constructor Details

This class inherits a constructor from EPUBMaker::EPUBv2

Instance Method Details

#ncx(indentarray) ⇒ Object



18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/epubmaker/epubv3.rb', line 18

def ncx(indentarray)
  # FIXME: handle indentarray
  s = common_header
  s << <<EOT
  <title>#{@producer.res.v("toctitle")}</title>
</head>
<body>
  <nav epub:type="toc" id="toc">
  <h1 class="toc-title">#{@producer.res.v("toctitle")}</h1>
  <ul class="toc-h1">
EOT

  current = 1
  init_item = true
  @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
    if item.level > current
      s << %Q[\n<ul class="toc-h#{item.level}">\n]
      current = item.level
    elsif item.level < current
      (current - 1).downto(item.level) do |n|
        s << %Q[</li>\n</ul>\n]
      end
      s << %Q[</li>\n]
      current = item.level
    elsif init_item
      # noop
    else
      s << %Q[</li>\n]
    end
    s << %Q[<li><a href="#{item.file}">#{item.title}</a>]
    init_item = false
  end
  
  (current - 1).downto(1) do |n|
    s << %Q[</li>\n</ul>\n]
  end
  if !init_item
  s << %Q[</li>\n]
  end
  s << <<EOT
  </ul>
  </nav>
</body>
</html>
EOT
  return s
end

#opfObject

Return opf file content.



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
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
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
202
203
204
205
206
207
# File 'lib/epubmaker/epubv3.rb', line 112

def opf
  mathstr = @producer.params["mathml"].nil? ? "" : %Q[ properties="mathml"]
  s = <<EOT
<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId" xml:lang="#{@producer.params["language"]}" profile="http://www.idpf.org/epub/30/profile/package/">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
EOT
  %w[title language date type format source description relation coverage subject rights].each do |item|
    next if @producer.params[item].nil?
    if @producer.params[item].instance_of?(Array)
      s << @producer.params[item].map {|i| %Q[    <dc:#{item} prefer="#{item}">#{CGI.escapeHTML(i.to_s)}</dc:#{item}>\n]}.join
      s << @producer.params[item].map {|i| %Q[    <meta id="#{item}" property="dcterms:#{item}">#{CGI.escapeHTML(i.to_s)}</meta>\n]}.join
    else
      s << %Q[    <dc:#{item} prefer="#{item}">#{CGI.escapeHTML(@producer.params[item].to_s)}</dc:#{item}>\n]
      s << %Q[    <meta id="#{item}" property="dcterms:#{item}">#{CGI.escapeHTML(@producer.params[item].to_s)}</meta>\n]
    end
  end

  # ID
  if @producer.params["isbn"].nil?
    s << %Q[    <dc:identifier id="BookId" prefer="bookid">#{@producer.params["urnid"]}</dc:identifier>\n]
    s << %Q[    <meta property="dcterms:identifier" id="bookid">#{@producer.params["urnid"]}</meta>\n]
  else
    s << %Q[    <dc:identifier id="BookId" opf:scheme="ISBN" prefer="bookid">#{@producer.params["isbn"]}</dc:identifier>\n]
    s << %Q[    <meta property="dcterms:identifier" id="bookid" opf:scheme="ISBN">#{@producer.params["isbn"]}</meta>\n]
  end
  
  # creator
  %w[aut 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-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].each do |role|
    next if @producer.params[role].nil?
    @producer.params[role].each_with_index do |v, i|
      s << %Q[    <dc:creator opf:role="#{role.sub('a-', '')}" prefer="creator-#{i}">#{CGI.escapeHTML(v)}</dc:creator>\n]
      s << %Q[    <meta property="dcterms:creator" id="creator-#{i}" opf:role="#{role.sub('a-', '')}">#{CGI.escapeHTML(v)}</meta>\n]
    end
  end
  # contributor
  %w[adp ann arr art asn aqt aft aui ant bkp clb cmm dsr edt ill lyr mdc mus nrt oth pht prt red rev spn ths trc trl].each do |role|
    next if @producer.params[role].nil?
    @producer.params[role].each_with_index do |v, i|
      s << %Q[    <dc:contributor opf:role="#{role}" prefer="contributor-#{i}">#{CGI.escapeHTML(v)}</dc:contributor>\n]
      s << %Q[    <meta property="dcterms:contributor" id="contributor-#{i}" opf:role="#{role}">#{CGI.escapeHTML(v)}</meta>\n]

      if role == "prt"
        s << %Q[    <dc:publisher prefer="publisher">#{CGI.escapeHTML(v)}</dc:publisher>\n]
        s << %Q[    <meta property="dcterms:publisher" id="publisher">#{CGI.escapeHTML(v)}</meta>\n]
      end
    end
  end

  s << %Q[  </metadata>\n]
  
  # manifest
  s << <<EOT
  <manifest>
<item properties="nav#{mathstr.empty? ? '' : ' mathml'}" id="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}" href="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}" media-type="application/xhtml+xml"/>
<item id="#{@producer.params["bookname"]}" href="#{@producer.params["cover"]}" media-type="application/xhtml+xml"/>
EOT

  if @producer.params["coverimage"]
    @producer.contents.each do |item|
      if item.media =~ /\Aimage/ && item.file =~ /#{@producer.params["coverimage"]}\Z/
          s << %Q[    <item properties="cover-image" id="#{item.id}" href="#{item.file}" media-type="#{item.media}"/>\n]
        break
      end
    end
  end
  
#      s << %Q[    <item id="toc" href="#{@producer.params["tocfile"]}" media-type="application/xhtml+xml"/>\n] unless @producer.params["mytoc"].nil?
  
  @producer.contents.each do |item|
    next if item.file =~ /#/ # skip subgroup
    s << %Q[    <item#{mathstr} id="#{item.id}" href="#{item.file}" media-type="#{item.media}"/>\n]
  end
  s << %Q[  </manifest>\n]
  
  # tocx
  s << %Q[  <spine>\n]
  s << %Q[    <itemref idref="#{@producer.params["bookname"]}" linear="no"/>\n]
  s << %Q[    <itemref idref="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}" />\n] unless @producer.params["mytoc"].nil?
  
  @producer.contents.each do |item|
    next if item.media !~ /xhtml\+xml/ # skip non XHTML
    s << %Q[    <itemref idref="#{item.id}"/>\n] if item.notoc.nil?
  end
  s << %Q[  </spine>\n]
  
  # guide
  s << %Q[  <guide>\n]
  s << %Q[    <reference type="cover" title="#{@producer.res.v("covertitle")}" href="#{@producer.params["cover"]}"/>\n]
  s << %Q[    <reference type="title-page" title="#{@producer.res.v("titlepagetitle")}" href="#{@producer.params["titlepage"]}"/>\n] unless @producer.params["titlepage"].nil?
  s << %Q[    <reference type="toc" title="#{@producer.res.v("toctitle")}" href="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}"/>\n] unless @producer.params["mytoc"].nil?
  s << %Q[    <reference type="colophon" title="#{@producer.res.v("colophontitle")}" href="colophon.#{@producer.params["htmlext"]}"/>\n] unless @producer.params["colophon"].nil? # FIXME: path
  s << %Q[  </guide>\n]
  s << %Q[</package>\n]
  return s
end

#produce(epubfile, basedir, tmpdir) ⇒ Object

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



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

def produce(epubfile, 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) }
  File.open("#{tmpdir}/OEBPS/#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}", "w") {|f| @producer.ncx(f, @producer.params["ncxindent"]) }
#      File.open("#{tmpdir}/OEBPS/#{@producer.params["tocfile"]}", "w") {|f| @producer.mytoc(f) } unless @producer.params["mytoc"].nil?
  
  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
  
  # FIXME:colophon and titlepage should be included in @producer.contents.
  
  @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

  fork {
    Dir.chdir(tmpdir) {|d|
      exec("zip", "-0X", "#{epubfile}", "mimetype")
    }
  }
  Process.waitall
  fork {
    Dir.chdir(tmpdir) {|d|
      exec("zip", "-Xr9D", "#{epubfile}", "META-INF", "OEBPS")
    }
  }
  Process.waitall
end