# encoding: utf-8 # = epubv3.rb -- EPUB version 3 producer. # # Copyright (c) 2010-2015 Kenshi Muto # # This program is free software. # You can distribute or modify this program under the terms of # the GNU LGPL, Lesser General Public License version 2.1. # For details of the GNU LGPL, see the file "COPYING". # require 'epubmaker/epubcommon' module EPUBMaker # EPUBv3 is EPUB version 3 producer. class EPUBv3 < EPUBCommon # Construct object with parameter hash +params+ and message resource hash +res+. def initialize(producer) super end # Return opf file content. def opf @opf_metainfo = opf_metainfo @opf_manifest = opf_manifest @opf_toc = opf_tocx tmplfile = File.expand_path('./opf/epubv3.opf.erb', ReVIEW::Template::TEMPLATE_DIR) tmpl = ReVIEW::Template.load(tmplfile) return tmpl.result(binding) end def opf_metainfo s = "" %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) @producer.params[item].each_with_index {|v, i| s << %Q[ #{CGI.escapeHTML(v.to_s)}\n] } else s << %Q[ #{CGI.escapeHTML(@producer.params[item].to_s)}\n] end end s << %Q[ #{@producer.params["modified"]}\n] # ID if @producer.params["isbn"].nil? s << %Q[ #{@producer.params["urnid"]}\n] else s << %Q[ #{@producer.params["isbn"]}\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 if @producer.params[role].nil? @producer.params[role].each_with_index do |v, i| if v.instance_of?(Hash) s << %Q[ #{CGI.escapeHTML(v["name"])}\n] s << %Q[ #{role.sub('a-', '')}\n] v.each_pair do |name, val| next if name == "name" s << %Q[ #{CGI.escapeHTML(val)}\n] end else s << %Q[ #{CGI.escapeHTML(v)}\n] s << %Q[ #{role.sub('a-', '')}\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 if @producer.params[role].nil? @producer.params[role].each_with_index do |v, i| if v.instance_of?(Hash) s << %Q[ #{CGI.escapeHTML(v["name"])}\n] s << %Q[ #{role}\n] v.each_pair do |name, val| next if name == "name" s << %Q[ #{CGI.escapeHTML(val)}\n] end else s << %Q[ #{CGI.escapeHTML(v)}\n] s << %Q[ #{role}\n] end if role == "prt" || role == "pbl" if v.instance_of?(Hash) s << %Q[ #{CGI.escapeHTML(v["name"])}\n] s << %Q[ #{role}\n] v.each_pair do |name, val| next if name == "name" s << %Q[ #{CGI.escapeHTML(val)}\n] end else s << %Q[ #{CGI.escapeHTML(v)}\n] s << %Q[ prt\n] end end end end s end def opf_manifest s = "" s << < EOT if @producer.params["coverimage"] @producer.contents.each do |item| if item.media =~ /\Aimage/ && File.basename(item.file) == @producer.params["coverimage"] s << %Q[ \n] item.id = nil break end 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[ \n] end s << %Q[ \n] s end def opf_tocx if @producer.params["epubmaker"]["cover_linear"] && @producer.params["epubmaker"]["cover_linear"] != "no" cover_linear = "yes" else cover_linear = "no" end s = "" if @producer.params["direction"] s << %Q[ \n] else s << %Q[ \n] end s << %Q[ \n] s << %Q[ \n] if @producer.params["toc"] @producer.contents.each do |item| next if item.media !~ /xhtml\+xml/ # skip non XHTML s << %Q[ \n] if item.notoc.nil? end s << %Q[ \n] s end def ncx(indentarray) if @producer.params["epubmaker"]["flattoc"].nil? ncx_main = hierarchy_ncx("ol") else ncx_main = flat_ncx("ol", @producer.params["epubmaker"]["flattocindent"]) end @body = <

#{@producer.res.v("toctitle")}

#{ncx_main} EOT @title = @producer.res.v("toctitle") @language = @producer.params['language'] @stylesheets = @producer.params["stylesheet"] tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) tmpl = ReVIEW::Template.load(tmplfile) return tmpl.result(binding) end # Produce EPUB file +epubfile+. # +basedir+ points the directory has contents. # +tmpdir+ defines temporary directory. def produce(epubfile, basedir, tmpdir) produce_write_common(basedir, tmpdir) File.open("#{tmpdir}/OEBPS/#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}", "w") {|f| @producer.ncx(f, @producer.params["epubmaker"]["ncxindent"]) } @producer.call_hook(@producer.params["epubmaker"]["hook_prepack"], tmpdir) export_zip(tmpdir, epubfile) end private # Return cover pointer for opf file def cover_in_opf s = "" if @producer.params["coverimage"] @producer.contents.each do |item| if item.media =~ /\Aimage/ && item.file =~ /#{@producer.params["coverimage"]}\Z/ s << < EOT break end end end s << < EOT s end end end