lib/epubmaker/epubcommon.rb in review-1.7.2 vs lib/epubmaker/epubcommon.rb in review-2.0.0.beta1

- old
+ new

@@ -26,14 +26,10 @@ # Return mimetype content. def mimetype "application/epub+zip" end - def opf_path - "OEBPS/#{@producer.params["bookname"]}.opf" - end - def opf_coverimage s = "" if @producer.params["coverimage"] file = nil @producer.contents.each do |item| @@ -114,191 +110,204 @@ s end # Return container content. def container - @opf_path = opf_path - tmplfile = File.expand_path('./xml/container.xml.erb', ReVIEW::Template::TEMPLATE_DIR) - tmpl = ReVIEW::Template.load(tmplfile) - tmpl.result(binding) + 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 # Return cover content. def cover(type=nil) - @body_ext = type.nil? ? "" : " epub:type=\"#{type}\"" + bodyext = type.nil? ? "" : " epub:type=\"#{type}\"" - if @producer.params["coverimage"] - file = @producer.coverimage - if !file - raise "coverimage #{@producer.params["coverimage"]} not found. Abort." + 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 - @body = <<-EOT + 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 - else - @body = <<-EOT -<h1 class="cover-title">#{CGI.escapeHTML(@producer.params["title"])}</h1> - EOT +EOT end - @title = @producer.params['title'] - @language = @producer.params['language'] - @stylesheets = @producer.params["stylesheet"] - if @producer.params["htmlversion"].to_i == 5 - tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) - else - tmplfile = File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR) - end - tmpl = ReVIEW::Template.load(tmplfile) - tmpl.result(binding) + s << <<EOT +</body> +</html> +EOT + s end # Return title (copying) content. def titlepage - @title = @producer.params["title"] - - @body = <<EOT - <h1 class="tp-title">#{CGI.escapeHTML(@title)}</h1> + 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"] - @body << <<EOT + 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 - @body << <<EOT + s << <<EOT <p> <br /> <br /> <br /> <br /> </p> <h3 class="tp-publisher">#{CGI.escapeHTML(publisher.join(", "))}</h3> EOT end - @language = @producer.params['language'] - @stylesheets = @producer.params["stylesheet"] - if @producer.params["htmlversion"].to_i == 5 - tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) - else - tmplfile = File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR) - end - tmpl = ReVIEW::Template.load(tmplfile) - tmpl.result(binding) + s << <<EOT +</body> +</html> +EOT + + s end # Return colophon content. def colophon - @title = @producer.res.v("colophontitle") - @body = <<EOT + s = common_header + s << <<EOT + <title>#{@producer.res.v("colophontitle")}</title> +</head> +<body> <div class="colophon"> EOT if @producer.params["subtitle"].nil? - @body << <<EOT + s << <<EOT <p class="title">#{CGI.escapeHTML(@producer.params["title"])}</p> EOT else - @body << <<EOT + 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"] - @body << %Q[ <div class="pubhistory">\n] + 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/ - @body << %Q[ <p>#{ReVIEW::I18n.t("published_by1", [date_to_s(item), editstr+revstr])}</p>\n] + 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| - @body << %Q[ <p>#{ReVIEW::I18n.t("published_by3", [date_to_s(m[1]), m[2]])}</p>\n] + s << %Q[ <p>#{ReVIEW::I18n.t("published_by3", [date_to_s(m[1]), m[2]])}</p>\n] end end end end else - @body << %Q[ <p>#{ReVIEW::I18n.t("published_by2", date_to_s(@producer.params["date"]))}</p>\n] + s << %Q[ <p>#{ReVIEW::I18n.t("published_by2", date_to_s(@producer.params["date"]))}</p>\n] end - @body << %Q[ </div>\n] + s << %Q[ </div>\n] end - @body << %Q[ <table class="colophon">\n] - @body << %w[aut csl trl dsr ill edt pbl prt pht].map{ |role| - if @producer.params[role] - %Q[ <tr><th>#{@producer.res.v(role)}</th><td>#{CGI.escapeHTML(@producer.params[role].join(", "))}</td></tr>\n] - else - "" - end - }.join("") - + 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 - @body << %Q[ <tr><th>ISBN</th><td>#{isbn}</td></tr>\n] + s << %Q[ <tr><th>ISBN</th><td>#{isbn}</td></tr>\n] end - @body << %Q[ </table>\n] + s << <<EOT + </table> +EOT if !@producer.params["rights"].nil? && @producer.params["rights"].size > 0 - @body << %Q[ <p class="copyright">#{@producer.params["rights"].join("<br />")}</p>] + s << %Q[ <p class="copyright">#{@producer.params["rights"].join("<br />")}</p>] end - @body << %Q[ </div>\n] - @language = @producer.params['language'] - @stylesheets = @producer.params["stylesheet"] - if @producer.params["htmlversion"].to_i == 5 - tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) - else - tmplfile = File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR) - end - tmpl = ReVIEW::Template.load(tmplfile) - tmpl.result(binding) + s << <<EOT + </div> +</body> +</html> +EOT + s end def date_to_s(date) require 'date' d = Date.parse(date) d.strftime(ReVIEW::I18n.t("date_format")) end # Return own toc content. def mytoc - @title = @producer.res.v("toctitle") + s = common_header + s << <<EOT + <title>#{@producer.res.v("toctitle")}</title> +</head> +<body> + <h1 class="toc-title">#{@producer.res.v("toctitle")}</h1> +EOT - @body = %Q[ <h1 class="toc-title">#{@producer.res.v("toctitle")}</h1>\n] if @producer.params["epubmaker"]["flattoc"].nil? - @body << hierarchy_ncx("ul") + s << hierarchy_ncx("ul") else - @body << flat_ncx("ul", @producer.params["epubmaker"]["flattocindent"]) + s << flat_ncx("ul", @producer.params["epubmaker"]["flattocindent"]) end - @language = @producer.params['language'] - @stylesheets = @producer.params["stylesheet"] - if @producer.params["htmlversion"].to_i == 5 - tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) - else - tmplfile = File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR) - end - tmpl = ReVIEW::Template.load(tmplfile) - tmpl.result(binding) + s << <<EOT +</body> +</html> +EOT + s end def hierarchy_ncx(type) require 'rexml/document' level = 1 @@ -383,11 +392,11 @@ FileUtils.mkdir_p("#{tmpdir}/META-INF") File.open("#{tmpdir}/META-INF/container.xml", "w") {|f| @producer.container(f) } FileUtils.mkdir_p("#{tmpdir}/OEBPS") - File.open(File.join(tmpdir, opf_path), "w") {|f| @producer.opf(f) } + 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) } @@ -406,16 +415,24 @@ Dir.chdir(tmpdir) {|d| `#{@producer.params["epubmaker"]["zip_stage1"]} #{epubfile.shellescape} mimetype` } Dir.chdir(tmpdir) {|d| `#{@producer.params["epubmaker"]["zip_stage2"]} #{epubfile.shellescape} META-INF OEBPS #{@producer.params["epubmaker"]["zip_addpath"]}` } end def legacy_cover_and_title_file(loadfile, writefile) - @title = @producer.params["booktitle"] - s = "" + 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