lib/review/epubmaker.rb in review-1.4.0 vs lib/review/epubmaker.rb in review-1.5.0
- old
+ new
@@ -1,8 +1,8 @@
# encoding: utf-8
#
-# Copyright (c) 2010-2014 Kenshi Muto and Masayoshi Takahashi
+# Copyright (c) 2010-2015 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".
@@ -34,10 +34,11 @@
@params = @epub.params
end
def produce(yamlfile, bookname=nil)
load_yaml(yamlfile)
+ I18n.setup(@params["language"])
bookname = @params["bookname"] if bookname.nil?
booktmpname = "#{bookname}-epub"
log("Loaded yaml file (#{yamlfile}). I will produce #{bookname}.epub.")
@@ -46,42 +47,42 @@
basetmpdir = Dir.mktmpdir("#{bookname}-", Dir.pwd)
begin
log("Created first temporary directory as #{basetmpdir}.")
- log("Call hook_beforeprocess. (#{@params["hook_beforeprocess"]})")
- call_hook(@params["hook_beforeprocess"], basetmpdir)
+ log("Call hook_beforeprocess. (#{@params["epubmaker"]["hook_beforeprocess"]})")
+ call_hook(@params["epubmaker"]["hook_beforeprocess"], basetmpdir)
copy_stylesheet(basetmpdir)
copy_frontmatter(basetmpdir)
- log("Call hook_afterfrontmatter. (#{@params["hook_afterfrontmatter"]})")
- call_hook(@params["hook_afterfrontmatter"], basetmpdir)
+ log("Call hook_afterfrontmatter. (#{@params["epubmaker"]["hook_afterfrontmatter"]})")
+ call_hook(@params["epubmaker"]["hook_afterfrontmatter"], basetmpdir)
build_body(basetmpdir, yamlfile)
- log("Call hook_afterbody. (#{@params["hook_afterbody"]})")
- call_hook(@params["hook_afterbody"], basetmpdir)
+ log("Call hook_afterbody. (#{@params["epubmaker"]["hook_afterbody"]})")
+ call_hook(@params["epubmaker"]["hook_afterbody"], basetmpdir)
copy_backmatter(basetmpdir)
- log("Call hook_afterbackmatter. (#{@params["hook_afterbackmatter"]})")
- call_hook(@params["hook_afterbackmatter"], basetmpdir)
+ log("Call hook_afterbackmatter. (#{@params["epubmaker"]["hook_afterbackmatter"]})")
+ call_hook(@params["epubmaker"]["hook_afterbackmatter"], basetmpdir)
push_contents(basetmpdir)
- if !@params["verify_target_images"].nil?
+ if !@params["epubmaker"]["verify_target_images"].nil?
verify_target_images(basetmpdir)
copy_images(@params["imagedir"], basetmpdir)
else
copy_images(@params["imagedir"], "#{basetmpdir}/images")
end
copy_resources("covers", "#{basetmpdir}/images")
copy_resources("adv", "#{basetmpdir}/images")
copy_resources(@params["fontdir"], "#{basetmpdir}/fonts", @params["font_ext"])
- log("Call hook_aftercopyimage. (#{@params["hook_aftercopyimage"]})")
- call_hook(@params["hook_aftercopyimage"], basetmpdir)
+ log("Call hook_aftercopyimage. (#{@params["epubmaker"]["hook_aftercopyimage"]})")
+ call_hook(@params["epubmaker"]["hook_aftercopyimage"], basetmpdir)
@epub.import_imageinfo("#{basetmpdir}/images", basetmpdir)
@epub.import_imageinfo("#{basetmpdir}/fonts", basetmpdir, @params["font_ext"])
epubtmpdir = @params["debug"].nil? ? nil : "#{Dir.pwd}/#{booktmpname}"
@@ -108,35 +109,35 @@
@epub.contents.each do |content|
if content.media == "application/xhtml+xml"
File.open("#{basetmpdir}/#{content.file}") do |f|
Document.new(File.new(f)).each_element("//img") do |e|
- @params["force_include_images"].push(e.attributes["src"])
+ @params["epubmaker"]["force_include_images"].push(e.attributes["src"])
if e.attributes["src"] =~ /svg\Z/i
content.properties.push("svg")
end
end
end
elsif content.media == "text/css"
File.open("#{basetmpdir}/#{content.file}") do |f|
f.each_line do |l|
l.scan(/url\((.+?)\)/) do |m|
- @params["force_include_images"].push($1.strip)
+ @params["epubmaker"]["force_include_images"].push($1.strip)
end
end
end
end
end
- @params["force_include_images"] = @params["force_include_images"].sort.uniq
+ @params["epubmaker"]["force_include_images"] = @params["epubmaker"]["force_include_images"].sort.uniq
end
def copy_images(resdir, destdir, allow_exts=nil)
return nil unless File.exist?(resdir)
allow_exts = @params["image_ext"] if allow_exts.nil?
FileUtils.mkdir_p(destdir) unless FileTest.directory?(destdir)
- if !@params["verify_target_images"].nil?
- @params["force_include_images"].each do |file|
+ if !@params["epubmaker"]["verify_target_images"].nil?
+ @params["epubmaker"]["force_include_images"].each do |file|
unless File.exist?(file)
warn "#{file} is not found, skip." if file !~ /\Ahttp[s]?:/
next
end
basedir = File.dirname(file)
@@ -162,11 +163,11 @@
next if fname =~ /\A\./
if FileTest.directory?("#{resdir}/#{fname}")
recursive_copy_files("#{resdir}/#{fname}", "#{destdir}/#{fname}", allow_exts)
else
if fname =~ /\.(#{allow_exts.join("|")})\Z/i
- Dir.mkdir(destdir) unless File.exist?(destdir)
+ FileUtils.mkdir_p(destdir) unless File.exist?(destdir)
log("Copy #{resdir}/#{fname} to the temporary directory.")
FileUtils.cp("#{resdir}/#{fname}", destdir)
end
end
end
@@ -183,11 +184,13 @@
@tocdesc = Array.new
# toccount = 2 ## not used
basedir = Dir.pwd
base_path = Pathname.new(basedir)
- ReVIEW::Book.load(basedir).parts.each do |part|
+ book = ReVIEW::Book.load(basedir)
+ book.load_config(yamlfile)
+ book.parts.each do |part|
htmlfile = nil
if part.name.present?
if part.file?
build_chap(part, base_path, basetmpdir, yamlfile, true)
else
@@ -245,11 +248,11 @@
else
filename = Pathname.new(chap.path).relative_path_from(base_path).to_s
end
id = filename.sub(/\.re\Z/, "")
- if @params["rename_for_legacy"] && ispart.nil?
+ if @params["epubmaker"]["rename_for_legacy"] && ispart.nil?
if chap.on_PREDEF?
@precount += 1
id = sprintf("pre%02d", @precount)
elsif chap.on_APPENDIX?
@postcount += 1
@@ -279,26 +282,47 @@
stylesheet = ""
if @params["stylesheet"].size > 0
stylesheet = "--stylesheet=#{@params["stylesheet"].join(",")}"
end
+ ENV["REVIEWFNAME"] = filename
system("#{ReVIEW::MakerHelper.bindir}/review-compile --yaml=#{yamlfile} --target=html --level=#{level} --htmlversion=#{@params["htmlversion"]} --epubversion=#{@params["epubversion"]} #{stylesheet} #{@params["params"]} #{filename} > \"#{basetmpdir}/#{htmlfile}\"")
write_info_body(basetmpdir, id, htmlfile, ispart, chaptype)
end
+ def detect_properties(path)
+ properties = []
+ File.open(path) do |f|
+ doc = REXML::Document.new(f)
+ if REXML::XPath.first(doc, "//m:math", {'m' => 'http://www.w3.org/1998/Math/MathML'})
+ properties<< "mathml"
+ end
+ if REXML::XPath.first(doc, "//s:svg", {'s' => 'http://www.w3.org/2000/svg'})
+ properties<< "svg"
+ end
+ end
+ properties
+ end
+
def write_info_body(basetmpdir, id, filename, ispart=nil, chaptype=nil)
headlines = []
# FIXME:nonumを修正する必要あり
- Document.parse_stream(File.new("#{basetmpdir}/#{filename}"), ReVIEWHeaderListener.new(headlines))
+ path = File.join(basetmpdir, filename)
+ Document.parse_stream(File.new(path), ReVIEWHeaderListener.new(headlines))
+ properties = detect_properties(path)
+ prop_str = ""
+ if properties.present?
+ prop_str = ",properties="+properties.join(" ")
+ end
first = true
headlines.each do |headline|
headline["level"] = 0 if !ispart.nil? && headline["level"] == 1
if first.nil?
write_tochtmltxt(basetmpdir, "#{headline["level"]}\t#{filename}##{headline["id"]}\t#{headline["title"]}\tchaptype=#{chaptype}")
else
- write_tochtmltxt(basetmpdir, "#{headline["level"]}\t#{filename}\t#{headline["title"]}\tforce_include=true,chaptype=#{chaptype}")
+ write_tochtmltxt(basetmpdir, "#{headline["level"]}\t#{filename}\t#{headline["title"]}\tforce_include=true,chaptype=#{chaptype}#{prop_str}")
first = nil
end
end
end
@@ -306,10 +330,11 @@
File.open("#{basetmpdir}/#{@tochtmltxt}") do |f|
f.each_line do |l|
force_include = nil
customid = nil
chaptype = nil
+ properties = nil
level, file, title, custom = l.chomp.split("\t")
unless custom.nil?
# custom setting
vars = custom.split(/,\s*/)
vars.each do |var|
@@ -319,20 +344,25 @@
customid = v
when "force_include"
force_include = true
when "chaptype"
chaptype = v
+ when "properties"
+ properties = v
end
end
end
next if level.to_i > @params["toclevel"] && force_include.nil?
log("Push #{file} to ePUB contents.")
- if customid.nil?
- @epub.contents.push(Content.new("file" => file, "level" => level.to_i, "title" => title, "chaptype" => chaptype))
- else
- @epub.contents.push(Content.new("id" => customid, "file" => file, "level" => level.to_i, "title" => title, "chaptype" => chaptype))
+ hash = {"file" => file, "level" => level.to_i, "title" => title, "chaptype" => chaptype}
+ if customid.present?
+ hash["id"] = customid
end
+ if properties.present?
+ hash["properties"] = properties.split(" ")
+ end
+ @epub.contents.push(Content.new(hash))
end
end
end
def copy_stylesheet(basetmpdir)