Class: EPUBMaker::EPUBCommon
Overview
EPUBCommon is the common class for EPUB producer.
Instance Method Summary collapse
-
#colophon ⇒ Object
Return colophon content.
- #colophon_history ⇒ Object
-
#container ⇒ Object
Return container content.
-
#cover(type = nil) ⇒ Object
Return cover content.
- #date_to_s(date) ⇒ Object
- #flat_ncx(type, indent = nil) ⇒ Object
- #h(str) ⇒ Object
- #hierarchy_ncx(type) ⇒ Object
-
#initialize(producer) ⇒ EPUBCommon
constructor
Construct object with parameter hash
configand message resource hashres. - #join_with_separator(value, sep) ⇒ Object
- #legacy_cover_and_title_file(loadfile, writefile) ⇒ Object
-
#mimetype ⇒ Object
Return mimetype content.
-
#mytoc ⇒ Object
Return own toc content.
- #ncx_doctitle ⇒ Object
- #ncx_isbn ⇒ Object
- #ncx_navmap(indentarray) ⇒ Object
- #opf_coverimage ⇒ Object
- #opf_path ⇒ Object
- #produce_write_common(basedir, tmpdir) ⇒ Object
-
#titlepage ⇒ Object
Return title (copying) content.
Constructor Details
#initialize(producer) ⇒ EPUBCommon
Construct object with parameter hash config and message resource hash res.
23 24 25 26 27 |
# File 'lib/epubmaker/epubcommon.rb', line 23 def initialize(producer) @body_ext = '' @producer = producer @body_ext = nil end |
Instance Method Details
#colophon ⇒ Object
Return colophon content.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 |
# File 'lib/epubmaker/epubcommon.rb', line 219 def colophon @title = h(@producer.res.v('colophontitle')) @body = " <div class=\"colophon\">\n" if @producer.config['subtitle'].nil? @body << "<p class=\"title\">\#{h(@producer.config.name_of('title'))}</p>\n" else @body << "<p class=\"title\">\#{h(@producer.config.name_of('title'))}<br /><span class=\"subtitle\">\#{h(@producer.config.name_of('subtitle'))}</span></p>\n" end @body << colophon_history if @producer.config['date'] || @producer.config['history'] @body << %Q( <table class="colophon">\n) @body << @producer.config['colophon_order'].map do |role| if @producer.config[role] %Q( <tr><th>#{h(@producer.res.v(role))}</th><td>#{h(join_with_separator(@producer.config.names_of(role), ReVIEW::I18n.t('names_splitter')))}</td></tr>\n) else '' end end.join @body << %Q( <tr><th>ISBN</th><td>#{@producer.isbn_hyphen}</td></tr>\n) if @producer.isbn_hyphen @body << %Q( </table>\n) if @producer.config['rights'] && !@producer.config['rights'].empty? @body << %Q( <p class="copyright">#{join_with_separator(@producer.config.names_of('rights').map { |m| h(m) }, '<br />')}</p>\n) end @body << %Q( </div>\n) @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] tmplfile = if @producer.config['htmlversion'].to_i == 5 File.('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) else File.('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR) end tmpl = ReVIEW::Template.load(tmplfile) tmpl.result(binding) end |
#colophon_history ⇒ Object
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 |
# File 'lib/epubmaker/epubcommon.rb', line 264 def colophon_history buf = '' buf << %Q( <div class="pubhistory">\n) if @producer.config['history'] @producer.config['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).to_s) revstr = ReVIEW::I18n.t('nth_impression', (rev + 1).to_s) if item =~ /\A\d+-\d+-\d+\Z/ buf << %Q( <p>#{ReVIEW::I18n.t('published_by1', [date_to_s(item), editstr + revstr])}</p>\n) elsif item =~ /\A(\d+-\d+-\d+)[\s ](.+)/ # custom date with string item.match(/\A(\d+-\d+-\d+)[\s ](.+)/) do |m| buf << %Q( <p>#{ReVIEW::I18n.t('published_by3', [date_to_s(m[1]), m[2]])}</p>\n) end else # free format buf << %Q( <p>#{item}</p>\n) end end end else buf << %Q( <p>#{ReVIEW::I18n.t('published_by2', date_to_s(@producer.config['date']))}</p>\n) end buf << %Q( </div>\n) buf end |
#container ⇒ Object
Return container content.
126 127 128 129 130 131 |
# File 'lib/epubmaker/epubcommon.rb', line 126 def container @opf_path = opf_path tmplfile = File.('./xml/container.xml.erb', ReVIEW::Template::TEMPLATE_DIR) tmpl = ReVIEW::Template.load(tmplfile) tmpl.result(binding) end |
#cover(type = nil) ⇒ Object
Return cover content.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/epubmaker/epubcommon.rb', line 134 def cover(type = nil) @body_ext = type.nil? ? '' : %Q( epub:type="#{type}") if @producer.config['coverimage'] file = @producer.coverimage raise "coverimage #{@producer.config['coverimage']} not found. Abort." unless file @body = " <div id=\"cover-image\" class=\"cover-image\">\n<img src=\"\#{file}\" alt=\"\#{h(@producer.config.name_of('title'))}\" class=\"max\"/>\n </div>\n EOT\n else\n @body = <<-EOT\n<h1 class=\"cover-title\">\#{h(@producer.config.name_of('title'))}</h1>\n EOT\n if @producer.config['subtitle']\n @body << <<-EOT\n<h2 class=\"cover-subtitle\">\#{h(@producer.config.name_of('subtitle'))}</h2>\n EOT\n end\n end\n\n @title = h(@producer.config.name_of('title'))\n @language = @producer.config['language']\n @stylesheets = @producer.config['stylesheet']\n tmplfile = if @producer.config['htmlversion'].to_i == 5\n File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)\n else\n File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR)\n end\n tmpl = ReVIEW::Template.load(tmplfile)\n tmpl.result(binding)\nend\n" |
#date_to_s(date) ⇒ Object
292 293 294 295 296 |
# File 'lib/epubmaker/epubcommon.rb', line 292 def date_to_s(date) require 'date' d = Date.parse(date) d.strftime(ReVIEW::I18n.t('date_format')) end |
#flat_ncx(type, indent = nil) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/epubmaker/epubcommon.rb', line 391 def flat_ncx(type, indent = nil) s = %Q(<#{type} class="toc-h1">\n) @producer.contents.each do |item| next if !item.notoc.nil? || item.level.nil? || item.file.nil? || item.title.nil? || item.level > @producer.config['toclevel'].to_i is = indent == true ? ' ' * item.level : '' s << %Q(<li><a href="#{item.file}">#{is}#{h(item.title)}</a></li>\n) end s << %Q(</#{type}>\n) s end |
#h(str) ⇒ Object
29 30 31 |
# File 'lib/epubmaker/epubcommon.rb', line 29 def h(str) CGI.escapeHTML(str) end |
#hierarchy_ncx(type) ⇒ Object
320 321 322 323 324 325 326 327 328 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/epubmaker/epubcommon.rb', line 320 def hierarchy_ncx(type) require 'rexml/document' level = 1 find_jump = nil has_part = nil toclevel = @producer.config['toclevel'].to_i # check part existance @producer.contents.each do |item| next if item.notoc || item.chaptype != 'part' has_part = true break end if has_part @producer.contents.each do |item| if item.chaptype == 'part' && item.level > 0 # sections in part item.level -= 1 end # down level for part and chaps. pre, appendix, post are preserved if item.chaptype == 'part' || item.chaptype == 'body' item.level += 1 end end toclevel += 1 end doc = REXML::Document.new(%Q(<#{type} class="toc-h#{level}"><li /></#{type}>)) doc.context[:attribute_quote] = :quote e = doc.root.elements[1] # first <li/> @producer.contents.each do |item| next if !item.notoc.nil? || item.level.nil? || item.file.nil? || item.title.nil? || item.level > toclevel if item.level == level e2 = e.parent.add_element('li') e = e2 elsif item.level > level find_jump = true if (item.level - level) > 1 # deeper (level + 1).upto(item.level) do |n| if e.size == 0 # empty span for epubcheck e.attributes['style'] = 'list-style-type: none;' es = e.add_element('span', 'style' => 'display:none;') es.add_text(REXML::Text.new(' ', false, nil, true)) end e2 = e.add_element(type, 'class' => "toc-h#{n}") e3 = e2.add_element('li') e = e3 end level = item.level elsif item.level < level # shallower (level - 1).downto(item.level) { e = e.parent.parent } e2 = e.parent.add_element('li') e = e2 level = item.level end e2 = e.add_element('a', 'href' => item.file) e2.add_text(REXML::Text.new(item.title, true)) end warn %Q(found level jumping in table of contents. consider to use 'epubmaker:flattoc: true' for strict ePUB validator.) unless find_jump.nil? doc.to_s.gsub('<li/>', '').gsub('</li>', "</li>\n").gsub("<#{type} ", "\n" + '\&') # ugly end |
#join_with_separator(value, sep) ⇒ Object
441 442 443 444 445 446 447 |
# File 'lib/epubmaker/epubcommon.rb', line 441 def join_with_separator(value, sep) if value.is_a?(Array) value.join(sep) else value end end |
#legacy_cover_and_title_file(loadfile, writefile) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/epubmaker/epubcommon.rb', line 427 def legacy_cover_and_title_file(loadfile, writefile) @title = @producer.config['booktitle'] s = '' File.open(loadfile) do |f| f.each_line do |l| s << l end end File.open(writefile, 'w') do |f| f.puts s end end |
#mimetype ⇒ Object
Return mimetype content.
34 35 36 |
# File 'lib/epubmaker/epubcommon.rb', line 34 def mimetype 'application/epub+zip' end |
#mytoc ⇒ Object
Return own toc content.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/epubmaker/epubcommon.rb', line 299 def mytoc @title = h(@producer.res.v('toctitle')) @body = %Q( <h1 class="toc-title">#{h(@producer.res.v('toctitle'))}</h1>\n) if @producer.config['epubmaker']['flattoc'].nil? @body << hierarchy_ncx('ul') else @body << flat_ncx('ul', @producer.config['epubmaker']['flattocindent']) end @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] tmplfile = if @producer.config['htmlversion'].to_i == 5 File.('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) else File.('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR) end tmpl = ReVIEW::Template.load(tmplfile) tmpl.result(binding) end |
#ncx_doctitle ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/epubmaker/epubcommon.rb', line 67 def ncx_doctitle " <docTitle>\n<text>\#{h(@producer.config['title'])}</text>\n </docTitle>\n <docAuthor>\n<text>\#{@producer.config['aut'].nil? ? '' : h(join_with_separator(@producer.config['aut'], ReVIEW::I18n.t('names_splitter')))}</text>\n </docAuthor>\n" end |
#ncx_isbn ⇒ Object
62 63 64 65 |
# File 'lib/epubmaker/epubcommon.rb', line 62 def ncx_isbn uid = @producer.config['isbn'] || @producer.config['urnid'] %Q( <meta name="dtb:uid" content="#{uid}"/>\n) end |
#ncx_navmap(indentarray) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/epubmaker/epubcommon.rb', line 78 def ncx_navmap(indentarray) s = " <navMap>\n<navPoint id=\"top\" playOrder=\"1\">\n <navLabel>\n <text>\#{h(@producer.config['title'])}</text>\n </navLabel>\n <content src=\"\#{@producer.config['cover']}\"/>\n</navPoint>\n" nav_count = 2 unless @producer.config['mytoc'].nil? s << "<navPoint id=\"toc\" playOrder=\"\#{nav_count}\">\n <navLabel>\n <text>\#{h(@producer.res.v('toctitle'))}</text>\n </navLabel>\n <content src=\"\#{@producer.config['bookname']}-toc.\#{@producer.config['htmlext']}\"/>\n</navPoint>\n" nav_count += 1 end @producer.contents.each do |item| next if item.title.nil? indent = indentarray.nil? ? [''] : indentarray level = item.level.nil? ? 0 : (item.level - 1) level = indent.size - 1 if level >= indent.size s << "<navPoint id=\"nav-\#{nav_count}\" playOrder=\"\#{nav_count}\">\n <navLabel>\n <text>\#{indent[level]}\#{h(item.title)}</text>\n </navLabel>\n <content src=\"\#{item.file}\"/>\n</navPoint>\n" nav_count += 1 end s << " </navMap>\n" s end |
#opf_coverimage ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/epubmaker/epubcommon.rb', line 42 def opf_coverimage s = '' if @producer.config['coverimage'] file = nil @producer.contents.each do |item| if !item.media.start_with?('image') || item.file !~ /#{@producer.config['coverimage']}\Z/ next end s << %Q( <meta name="cover" content="#{item.id}"/>\n) file = item.file break end if file.nil? raise "coverimage #{@producer.config['coverimage']} not found. Abort." end end s end |
#opf_path ⇒ Object
38 39 40 |
# File 'lib/epubmaker/epubcommon.rb', line 38 def opf_path "OEBPS/#{@producer.config['bookname']}.opf" end |
#produce_write_common(basedir, tmpdir) ⇒ Object
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/epubmaker/epubcommon.rb', line 403 def produce_write_common(basedir, tmpdir) File.open("#{tmpdir}/mimetype", 'w') { |f| @producer.mimetype(f) } 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) } if File.exist?("#{basedir}/#{@producer.config['cover']}") FileUtils.cp("#{basedir}/#{@producer.config['cover']}", "#{tmpdir}/OEBPS") else File.open("#{tmpdir}/OEBPS/#{@producer.config['cover']}", 'w') { |f| @producer.cover(f) } end @producer.contents.each do |item| next if item.file =~ /#/ # skip subgroup fname = "#{basedir}/#{item.file}" raise "#{fname} doesn't exist. Abort." unless File.exist?(fname) FileUtils.mkdir_p(File.dirname("#{tmpdir}/OEBPS/#{item.file}")) FileUtils.cp(fname, "#{tmpdir}/OEBPS/#{item.file}") end end |
#titlepage ⇒ Object
Return title (copying) content. NOTE: this method is not used yet.
see lib/review/epubmaker.rb#build_titlepage
171 172 173 174 175 176 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 212 213 214 215 216 |
# File 'lib/epubmaker/epubcommon.rb', line 171 def titlepage @title = h(@producer.config.name_of('title')) @body = " <h1 class=\"tp-title\">\#{@title}</h1>\n" if @producer.config['subtitle'] @body << " <h2 class=\"tp-subtitle\">\#{h(@producer.config.name_of('subtitle'))}</h2>\n" end if @producer.config['aut'] @body << " <p>\n<br />\n<br />\n </p>\n <h2 class=\"tp-author\">\#{h(join_with_separator(@producer.config.names_of('aut'), ReVIEW::I18n.t('names_splitter')))}</h2>\n" end publisher = @producer.config.names_of('pbl') if publisher @body << " <p>\n<br />\n<br />\n<br />\n<br />\n </p>\n <h3 class=\"tp-publisher\">\#{h(join_with_separator(publisher, ReVIEW::I18n.t('names_splitter')))}</h3>\n" end @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] tmplfile = if @producer.config['htmlversion'].to_i == 5 File.('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) else File.('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR) end tmpl = ReVIEW::Template.load(tmplfile) tmpl.result(binding) end |