Class: ReVIEW::EPUBMaker
- Includes:
- EPUBMaker, REXML
- Defined in:
- lib/review/epubmaker.rb
Defined Under Namespace
Classes: ReVIEWHeaderListener
Instance Method Summary collapse
- #build_body(basetmpdir, yamlfile) ⇒ Object
- #build_chap(chap, base_path, basetmpdir, yamlfile, ispart = nil) ⇒ Object
- #build_part(part, basetmpdir, htmlfile) ⇒ Object
- #build_titlepage(basetmpdir, htmlfile) ⇒ Object
- #call_hook(filename, *params) ⇒ Object
- #copy_backmatter(basetmpdir) ⇒ Object
- #copy_frontmatter(basetmpdir) ⇒ Object
- #copy_images(resdir, destdir, allow_exts = nil) ⇒ Object
- #copy_resources(resdir, destdir, allow_exts = nil) ⇒ Object
- #copy_stylesheet(basetmpdir) ⇒ Object
- #detect_properties(path) ⇒ Object
- #footer ⇒ Object
- #header(title) ⇒ Object
-
#initialize ⇒ EPUBMaker
constructor
A new instance of EPUBMaker.
- #load_yaml(yamlfile) ⇒ Object
- #log(s) ⇒ Object
- #produce(yamlfile, bookname = nil) ⇒ Object
- #push_contents(basetmpdir) ⇒ Object
- #recursive_copy_files(resdir, destdir, allow_exts) ⇒ Object
- #verify_target_images(basetmpdir) ⇒ Object
- #write_buildlogtxt(basetmpdir, htmlfile, reviewfile) ⇒ Object
- #write_info_body(basetmpdir, id, filename, ispart = nil, chaptype = nil) ⇒ Object
- #write_tochtmltxt(basetmpdir, s) ⇒ Object
Constructor Details
#initialize ⇒ EPUBMaker
Returns a new instance of EPUBMaker.
20 21 22 23 24 |
# File 'lib/review/epubmaker.rb', line 20 def initialize @epub = nil @tochtmltxt = "toc-html.txt" @buildlogtxt = "build-log.txt" end |
Instance Method Details
#build_body(basetmpdir, yamlfile) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/review/epubmaker.rb', line 177 def build_body(basetmpdir, yamlfile) @precount = 0 @bodycount = 0 @postcount = 0 @manifeststr = "" @ncxstr = "" @tocdesc = Array.new # toccount = 2 ## not used basedir = Dir.pwd base_path = Pathname.new(basedir) 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 htmlfile = "part_#{part.number}.#{@params["htmlext"]}" build_part(part, basetmpdir, htmlfile) title = ReVIEW::I18n.t("part", part.number) title += ReVIEW::I18n.t("chapter_postfix") + part.name.strip if part.name.strip.present? write_tochtmltxt(basetmpdir, "0\t#{htmlfile}\t#{title}\tchaptype=part") write_buildlogtxt(basetmpdir, htmlfile, "") end end part.chapters.each do |chap| build_chap(chap, base_path, basetmpdir, yamlfile, nil) end end end |
#build_chap(chap, base_path, basetmpdir, yamlfile, ispart = nil) ⇒ Object
234 235 236 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/review/epubmaker.rb', line 234 def build_chap(chap, base_path, basetmpdir, yamlfile, ispart=nil) filename = "" chaptype = "body" if !ispart.nil? chaptype = "part" elsif chap.on_PREDEF? chaptype = "pre" elsif chap.on_APPENDIX? chaptype = "post" end if !ispart.nil? filename = chap.path else filename = Pathname.new(chap.path).relative_path_from(base_path).to_s end id = filename.sub(/\.re\Z/, "") 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 id = sprintf("post%02d", @postcount) else @bodycount += 1 id = sprintf("chap%02d", @bodycount) end end htmlfile = "#{id}.#{@params["htmlext"]}" write_buildlogtxt(basetmpdir, htmlfile, filename) log("Create #{htmlfile} from #{filename}.") level = @params["secnolevel"] # TODO: It would be nice if we can modify level in PART, PREDEF, or POSTDEF. # But we have to care about section number reference (@<hd>) also. # # if !ispart.nil? # level = @params["part_secnolevel"] # else # level = @params["pre_secnolevel"] if chap.on_PREDEF? # level = @params["post_secnolevel"] if chap.on_APPENDIX? # end 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 |
#build_part(part, basetmpdir, htmlfile) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/review/epubmaker.rb', line 213 def build_part(part, basetmpdir, htmlfile) log("Create #{htmlfile} from a template.") File.open("#{basetmpdir}/#{htmlfile}", "w") do |f| f.puts header(CGI.escapeHTML(@params["booktitle"])) f.puts "<div class=\"part\">\n<h1 class=\"part-number\">\#{ReVIEW::I18n.t(\"part\", part.number)}</h1>\n" if part.name.strip.present? f.puts "<h2 class=\"part-title\">\#{part.name.strip}</h2>\n" end f.puts "</div>\n" f.puts end end |
#build_titlepage(basetmpdir, htmlfile) ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/review/epubmaker.rb', line 401 def build_titlepage(basetmpdir, htmlfile) File.open("#{basetmpdir}/#{htmlfile}", "w") do |f| f.puts header(CGI.escapeHTML(@params["booktitle"])) f.puts "<div class=\"titlepage\">\n<h1 class=\"tp-title\">\#{CGI.escapeHTML(@params[\"booktitle\"])}</h1>\n" if @params["aut"] f.puts "<h2 class=\"tp-author\">\#{@params[\"aut\"].join(\", \")}</h2>\n" end if @params["prt"] f.puts "<h3 class=\"tp-publisher\">\#{@params[\"prt\"].join(\", \")}</h3>\n" end f.puts "</div>\n" f.puts end end |
#call_hook(filename, *params) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/review/epubmaker.rb', line 98 def call_hook(filename, *params) if !filename.nil? && File.exist?(filename) && FileTest.executable?(filename) if ENV["REVIEW_SAFE_MODE"].to_i & 1 > 0 warn "hook is prohibited in safe mode. ignored." else system(filename, *params) end end end |
#copy_backmatter(basetmpdir) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/review/epubmaker.rb', line 427 def copy_backmatter(basetmpdir) if @params["profile"] FileUtils.cp(@params["profile"], "#{basetmpdir}/#{File.basename(@params["profile"])}") write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["profile"])}\t#{@epub.res.v("profiletitle")}\tchaptype=post") end if @params["advfile"] FileUtils.cp(@params["advfile"], "#{basetmpdir}/#{File.basename(@params["advfile"])}") write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["advfile"])}\t#{@epub.res.v("advtitle")}\tchaptype=post") end if @params["colophon"] if @params["colophon"].instance_of?(String) # FIXME:このやり方はやめる? FileUtils.cp(@params["colophon"], "#{basetmpdir}/colophon.#{@params["htmlext"]}") else File.open("#{basetmpdir}/colophon.#{@params["htmlext"]}", "w") {|f| @epub.colophon(f) } end write_tochtmltxt(basetmpdir, "1\tcolophon.#{@params["htmlext"]}\t#{@epub.res.v("colophontitle")}\tchaptype=post") end if @params["backcover"] FileUtils.cp(@params["backcover"], "#{basetmpdir}/#{File.basename(@params["backcover"])}") write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["backcover"])}\t#{@epub.res.v("backcovertitle")}\tchaptype=post") end end |
#copy_frontmatter(basetmpdir) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/review/epubmaker.rb', line 378 def copy_frontmatter(basetmpdir) FileUtils.cp(@params["cover"], "#{basetmpdir}/#{File.basename(@params["cover"])}") if !@params["cover"].nil? && File.exist?(@params["cover"]) if @params["titlepage"] if @params["titlefile"].nil? build_titlepage(basetmpdir, "titlepage.#{@params["htmlext"]}") else FileUtils.cp(@params["titlefile"], "#{basetmpdir}/titlepage.#{@params["htmlext"]}") end write_tochtmltxt(basetmpdir, "1\ttitlepage.#{@params["htmlext"]}\t#{@epub.res.v("titlepagetitle")}\tchaptype=pre") end if !@params["originaltitlefile"].nil? && File.exist?(@params["originaltitlefile"]) FileUtils.cp(@params["originaltitlefile"], "#{basetmpdir}/#{File.basename(@params["originaltitlefile"])}") write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["originaltitlefile"])}\t#{@epub.res.v("originaltitle")}\tchaptype=pre") end if !@params["creditfile"].nil? && File.exist?(@params["creditfile"]) FileUtils.cp(@params["creditfile"], "#{basetmpdir}/#{File.basename(@params["creditfile"])}") write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["creditfile"])}\t#{@epub.res.v("credittitle")}\tchaptype=pre") end end |
#copy_images(resdir, destdir, allow_exts = nil) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/review/epubmaker.rb', line 133 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["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) FileUtils.mkdir_p("#{destdir}/#{basedir}") unless FileTest.directory?("#{destdir}/#{basedir}") log("Copy #{file} to the temporary directory.") FileUtils.cp(file, "#{destdir}/#{basedir}") end else recursive_copy_files(resdir, destdir, allow_exts) end end |
#copy_resources(resdir, destdir, allow_exts = nil) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/review/epubmaker.rb', line 153 def copy_resources(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) recursive_copy_files(resdir, destdir, allow_exts) end |
#copy_stylesheet(basetmpdir) ⇒ Object
369 370 371 372 373 374 375 376 |
# File 'lib/review/epubmaker.rb', line 369 def copy_stylesheet(basetmpdir) if @params["stylesheet"].size > 0 @params["stylesheet"].each do |sfile| FileUtils.cp(sfile, basetmpdir) @epub.contents.push(Content.new("file" => sfile)) end end end |
#detect_properties(path) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/review/epubmaker.rb', line 293 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 |
#footer ⇒ Object
501 502 503 504 505 506 |
# File 'lib/review/epubmaker.rb', line 501 def "</body>\n</html>\n" end |
#header(title) ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/review/epubmaker.rb', line 465 def header(title) # titleはすでにエスケープ済みと想定 s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" if @params["htmlversion"] == 5 s << "<!DOCTYPE html>\n<html xml:lang='ja' xmlns:ops='http://www.idpf.org/2007/ops' xmlns='http://www.w3.org/1999/xhtml'>\n<head>\n<meta charset=\"UTF-8\" />\n" else s << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n<html xml:lang='ja' xmlns:ops='http://www.idpf.org/2007/ops' xmlns='http://www.w3.org/1999/xhtml'>\n<head>\n<meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />\n<meta http-equiv='Content-Style-Type' content='text/css' />\n" end if @params["stylesheet"].size > 0 @params["stylesheet"].each do |sfile| s << "<link rel='stylesheet' type='text/css' href='\#{sfile}' />\n" end end s << "<meta content='Re:VIEW' name='generator'/>\n<title>\#{title}</title>\n</head>\n<body>\n" end |
#load_yaml(yamlfile) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/review/epubmaker.rb', line 30 def load_yaml(yamlfile) @params = ReVIEW::Configure.values.merge(YAML.load_file(yamlfile)) # FIXME:設定がRe:VIEW側とepubmaker/producer.rb側の2つに分かれて面倒 @epub = Producer.new(@params) @epub.load(yamlfile) @params = @epub.params end |
#log(s) ⇒ Object
26 27 28 |
# File 'lib/review/epubmaker.rb', line 26 def log(s) puts s unless @params["debug"].nil? end |
#produce(yamlfile, bookname = nil) ⇒ Object
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 |
# File 'lib/review/epubmaker.rb', line 37 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.") File.unlink("#{bookname}.epub") if File.exist?("#{bookname}.epub") FileUtils.rm_rf(booktmpname) if @params["debug"] && File.exist?(booktmpname) basetmpdir = Dir.mktmpdir("#{bookname}-", Dir.pwd) begin log("Created first temporary directory as #{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["epubmaker"]["hook_afterfrontmatter"]})") call_hook(@params["epubmaker"]["hook_afterfrontmatter"], basetmpdir) build_body(basetmpdir, yamlfile) log("Call hook_afterbody. (#{@params["epubmaker"]["hook_afterbody"]})") call_hook(@params["epubmaker"]["hook_afterbody"], basetmpdir) copy_backmatter(basetmpdir) log("Call hook_afterbackmatter. (#{@params["epubmaker"]["hook_afterbackmatter"]})") call_hook(@params["epubmaker"]["hook_afterbackmatter"], basetmpdir) push_contents(basetmpdir) 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["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}" Dir.mkdir(booktmpname) unless @params["debug"].nil? log("Call ePUB producer.") @epub.produce("#{bookname}.epub", basetmpdir, epubtmpdir) log("Finished.") ensure FileUtils.remove_entry_secure basetmpdir if @params["debug"].nil? end end |
#push_contents(basetmpdir) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/review/epubmaker.rb', line 329 def push_contents(basetmpdir) 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| k, v = var.split("=") case k when "id" 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.") 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 |
#recursive_copy_files(resdir, destdir, allow_exts) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/review/epubmaker.rb', line 160 def recursive_copy_files(resdir, destdir, allow_exts) Dir.open(resdir) do |dir| dir.each do |fname| 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 FileUtils.mkdir_p(destdir) unless File.exist?(destdir) log("Copy #{resdir}/#{fname} to the temporary directory.") FileUtils.cp("#{resdir}/#{fname}", destdir) end end end end end |
#verify_target_images(basetmpdir) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/review/epubmaker.rb', line 108 def verify_target_images(basetmpdir) @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["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["epubmaker"]["force_include_images"].push($1.strip) end end end end end @params["epubmaker"]["force_include_images"] = @params["epubmaker"]["force_include_images"].sort.uniq end |
#write_buildlogtxt(basetmpdir, htmlfile, reviewfile) ⇒ Object
459 460 461 462 463 |
# File 'lib/review/epubmaker.rb', line 459 def write_buildlogtxt(basetmpdir, htmlfile, reviewfile) File.open("#{basetmpdir}/#{@buildlogtxt}", "a") do |f| f.puts "#{htmlfile},#{reviewfile}" end end |
#write_info_body(basetmpdir, id, filename, ispart = nil, chaptype = nil) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/review/epubmaker.rb', line 307 def write_info_body(basetmpdir, id, filename, ispart=nil, chaptype=nil) headlines = [] # FIXME:nonumを修正する必要あり 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}#{prop_str}") first = nil end end end |
#write_tochtmltxt(basetmpdir, s) ⇒ Object
453 454 455 456 457 |
# File 'lib/review/epubmaker.rb', line 453 def write_tochtmltxt(basetmpdir, s) File.open("#{basetmpdir}/#{@tochtmltxt}", "a") do |f| f.puts s end end |