# encoding: utf-8 # = epubv2.rb -- EPUB version 2 producer. # # Copyright (c) 2010-2016 Kenshi Muto and Masayoshi Takahashi # # 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' require 'cgi' require 'epubmaker/zip_exporter' module EPUBMaker # EPUBv2 is EPUB version 2 producer. class EPUBv2 < 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_coverimage = opf_coverimage @opf_manifest = opf_manifest @opf_toc = opf_tocx tmplfile = File.expand_path('./opf/epubv2.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 unless @producer.params[item] if @producer.params[item].kind_of?(Array) s << @producer.params.names_of(item).map {|i| %Q[ #{CGI.escapeHTML(i)}\n]}.join else s << %Q[ #{CGI.escapeHTML(@producer.params.name_of(item))}\n] end end # 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[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 unless @producer.params[role] @producer.params.names_of(role).each do |v| s << %Q[ #{CGI.escapeHTML(v)}\n] end end # contributor (should be array) %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 unless @producer.params[role] @producer.params.names_of(role).each do |v| s << %Q[ #{CGI.escapeHTML(v)}\n] if role == "prt" s << %Q[ #{v}\n] end end end s end def opf_manifest s = "" s << < EOT s << %Q[ \n] if @producer.params["toc"] && @producer.params["mytoc"] @producer.contents.each do |item| next if item.file =~ /#/ # skip subgroup 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 = "" s << %Q[ \n] s << %Q[ \n] s << %Q[ \n] unless @producer.params["mytoc"].nil? @producer.contents.each do |item| next if item.media !~ /xhtml\+xml/ # skip non XHTML s << %Q[ \n] end s << %Q[ \n] s end # Return ncx content. +indentarray+ has prefix marks for each level. def ncx(indentarray) @ncx_isbn = ncx_isbn @ncx_doctitle = ncx_doctitle @ncx_navmap = ncx_navmap(indentarray) tmplfile = File.expand_path('./ncx/epubv2.ncx.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"]}.ncx", "w") {|f| @producer.ncx(f, @producer.params["epubmaker"]["ncxindent"]) } File.open("#{tmpdir}/OEBPS/#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}", "w") {|f| @producer.mytoc(f) } unless @producer.params["mytoc"].nil? @producer.call_hook(@producer.params["epubmaker"]["hook_prepack"], tmpdir) expoter = EPUBMaker::ZipExporter.new(tmpdir, @producer.params) expoter.export_zip(epubfile) end end end