Class: EPUBMaker::EPUBv2
Overview
EPUBv2 is EPUB version 2 producer.
Direct Known Subclasses
Instance Method Summary collapse
-
#colophon ⇒ Object
Return colophon content.
-
#container ⇒ Object
Return container content.
-
#cover ⇒ Object
Return cover content.
-
#initialize(producer) ⇒ EPUBv2
constructor
Construct object with parameter hash
params
and message resource hashres
. -
#mimetype ⇒ Object
Return mimetype content.
-
#mytoc ⇒ Object
Return own toc content.
-
#ncx(indentarray) ⇒ Object
Return ncx content.
-
#opf ⇒ Object
Return opf file content.
-
#produce(epubfile, basedir, tmpdir) ⇒ Object
Produce EPUB file
epubfile
. -
#titlepage ⇒ Object
Return title (copying) content.
Constructor Details
#initialize(producer) ⇒ EPUBv2
Construct object with parameter hash params
and message resource hash res
.
20 21 22 |
# File 'lib/epubmaker/epubv2.rb', line 20 def initialize(producer) @producer = producer end |
Instance Method Details
#colophon ⇒ Object
Return colophon content.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/epubmaker/epubv2.rb', line 276 def colophon s = common_header s << <<EOT <title>#{@producer.res.v("colophontitle")}</title> </head> <body> <div class="colophon"> <p class="title">#{CGI.escapeHTML(@producer.params["title"])}</p> EOT if @producer.params["pubhistory"] s << %Q[ <div class="pubhistory">\n <p>#{@producer.params["pubhistory"].gsub(/\n/, "<br />")}</p>\n </div>\n] # FIXME: should be array? end s << %Q[ <table class="colophon">\n] s << %Q[ <tr><th>#{@producer.res.v("c-aut")}</th><td>#{CGI.escapeHTML(@producer.params["aut"])}</td></tr>\n] if @producer.params["aut"] s << %Q[ <tr><th>#{@producer.res.v("c-dsr")}</th><td>#{CGI.escapeHTML(@producer.params["dsr"])}</td></tr>\n] if @producer.params["dsr"] s << %Q[ <tr><th>#{@producer.res.v("c-ill")}</th><td>#{CGI.escapeHTML(@producer.params["ill"])}</td></tr>\n] if @producer.params["ill"] s << %Q[ <tr><th>#{@producer.res.v("c-edt")}</th><td>#{CGI.escapeHTML(@producer.params["edt"])}</td></tr>\n] if @producer.params["edt"] s << %Q[ <tr><th>#{@producer.res.v("c-prt")}</th><td>#{CGI.escapeHTML(@producer.params["prt"])}</td></tr>\n] if @producer.params["prt"] s << <<EOT </table> </div> </body> </html> EOT return s end |
#container ⇒ Object
Return container content.
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/epubmaker/epubv2.rb', line 189 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 return s end |
#cover ⇒ Object
Return cover content.
202 203 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 |
# File 'lib/epubmaker/epubv2.rb', line 202 def cover s = common_header s << <<EOT <title>#{CGI.escapeHTML(@producer.params["title"])}</title> </head> <body> 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 return s end |
#mimetype ⇒ Object
Return mimetype content.
25 26 27 |
# File 'lib/epubmaker/epubv2.rb', line 25 def mimetype return "application/epub+zip" end |
#mytoc ⇒ Object
Return own toc content.
306 307 308 309 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 |
# File 'lib/epubmaker/epubv2.rb', line 306 def mytoc s = common_header s << <<EOT <title>#{@producer.res.v("toctitle")}</title> </head> <body> <h1 class="toc-title">#{@producer.res.v("toctitle")}</h1> <ul class="toc-h1"> EOT # FIXME: indent 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> </body> </html> EOT return s end |
#ncx(indentarray) ⇒ Object
Return ncx content. indentarray
defines prefix string for each level.
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 |
# File 'lib/epubmaker/epubv2.rb', line 119 def ncx(indentarray) s = <<EOT <?xml version="1.0" encoding="UTF-8"?> <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1"> <head> <meta name="dtb:depth" content="1"/> <meta name="dtb:totalPageCount" content="0"/> <meta name="dtb:maxPageNumber" content="0"/> EOT if @producer.params["isbn"].nil? s << %Q[ <meta name="dtb:uid" content="#{@producer.params["urnid"]}"/>\n] else s << %Q[ <meta name="dtb:uid" content="#{@producer.params["isbn"]}"/>\n] end s << <<EOT </head> <docTitle> <text>#{CGI.escapeHTML(@producer.params["title"])}</text> </docTitle> <docAuthor> <text>#{@producer.params["aut"].nil? ? "" : CGI.escapeHTML(@producer.params["aut"].join(", "))}</text> </docAuthor> <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["tocfile"]}"/> </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]}#{item.title}</text> </navLabel> <content src="#{item.file}"/> </navPoint> EOT nav_count += 1 end s << <<EOT </navMap> </ncx> EOT return s end |
#opf ⇒ Object
Return opf file content.
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 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 |
# File 'lib/epubmaker/epubv2.rb', line 30 def opf s = <<EOT <?xml version="1.0" encoding="UTF-8"?> <package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId"> <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}>#{CGI.escapeHTML(i.to_s)}</dc:#{item}>\n]}.join else s << %Q[ <dc:#{item}>#{CGI.escapeHTML(@producer.params[item].to_s)}</dc:#{item}>\n] end end # ID if @producer.params["isbn"].nil? s << %Q[ <dc:identifier id="BookId">#{@producer.params["urnid"]}</dc:identifier>\n] else s << %Q[ <dc:identifier id="BookId" opf:scheme="ISBN">#{@producer.params["isbn"]}</dc:identifier>\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 do |v| s << %Q[ <dc:creator opf:role="#{role.sub('a-', '')}">#{CGI.escapeHTML(v)}</dc:creator>\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 do |v| s << %Q[ <dc:contributor opf:role="#{role}">#{CGI.escapeHTML(v)}</dc:contributor>\n] if role == "prt" s << %Q[ <dc:publisher>#{v}</dc:publisher>\n] end end end if @producer.params["coverimage"] @producer.contents.each do |item| if item.media =~ /\Aimage/ && item.file =~ /#{@producer.params["coverimage"]}\Z/ s << %Q[ <meta name="cover" content="#{item.id}"/>\n] break end end end s << %Q[ </metadata>\n] # manifest s << <<EOT <manifest> <item id="ncx" href="#{@producer.params["bookname"]}.ncx" media-type="application/x-dtbncx+xml"/> <item id="#{@producer.params["bookname"]}" href="#{@producer.params["cover"]}" media-type="application/xhtml+xml"/> EOT 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 id="#{item.id}" href="#{item.file}" media-type="#{item.media}"/>\n] end s << %Q[ </manifest>\n] # tocx s << %Q[ <spine toc="ncx">\n] s << %Q[ <itemref idref="#{@producer.params["bookname"]}" linear="no"/>\n] s << %Q[ <itemref idref="toc" />\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["tocfile"]}"/>\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.
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/epubmaker/epubv2.rb', line 356 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"]}.ncx", "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 |
#titlepage ⇒ Object
Return title (copying) content.
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 |
# File 'lib/epubmaker/epubv2.rb', line 237 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"])}</h2> EOT end if @producer.params["prt"] s << <<EOT <p> <br /> <br /> <br /> <br /> </p> <h3 class="tp-publisher">#{CGI.escapeHTML(@producer.params["prt"])}</h3> EOT end s << <<EOT </body> </html> EOT return s end |