Module: EBPS::Conversion::Oebps

Defined in:
lib/ebps/conversion/oebps.rb

Defined Under Namespace

Classes: Factory, HtmlFactory, IndexFactory, NcxFactory, OpfFactory, XmlFactory

Class Method Summary collapse

Class Method Details

.add_appendix(appendix, ids, tmpdir) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/ebps/conversion/oebps.rb', line 406

def self.add_appendix appendix, ids, tmpdir
  pic = Text::Picture.new
  pic << File.read(appendix)
  chp = Text::Chapter.new
  chp.add_paragraph pic
  doc = Text::Document.new
  doc.add_chapter chp
  local_ids = []
  html, id = document_to_html doc, local_ids, tmpdir
  name = 'appendix.html'
  write tmpdir, name, html
  ids.push [name, id, '', local_ids]
end

.compile_azw(tmpdir, opf) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/ebps/conversion/oebps.rb', line 419

def self.compile_azw tmpdir, opf
  current_dir = Dir.pwd
  Dir.chdir tmpdir
  opfpath = File.join tmpdir, opf
  config = EBPS.config
  command = "#{config.kindlegen_path} #{opfpath} #{config.kindlegen_args} -o mobipocket.azw"
  # kindlegen returns status -1 if a warning has been issued, so system's
  # return value is no indication of success.
  system command
ensure
  Dir.chdir current_dir
end

.compile_epub(tmpdir, name) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/ebps/conversion/oebps.rb', line 431

def self.compile_epub tmpdir, name
  Zip::ZipOutputStream.open('/tmp/test.epub') do |zh|
    zh.put_next_entry('mimetype')
    zh << 'application/epub+zip'
    zh.put_next_entry('META-INF/container.xml')
    zh << <<-EOS
<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
    <rootfile full-path="OPS/#{name}" media-type="application/oebps-package+xml"/>
  </rootfiles>
</container>
    EOS
    Dir.foreach(tmpdir) do |entry|
      path = File.join tmpdir, entry
      if File.ftype(path) == 'file'
        zh.put_next_entry("OPS/#{entry}")
        zh << File.read(path)
      end
    end
  end
end

.document_filename(doc, count) ⇒ Object



453
454
455
456
457
# File 'lib/ebps/conversion/oebps.rb', line 453

def self.document_filename doc, count
  title = doc.title.dup
  title = 'part' if title.empty?
  filename sprintf("%i %s.html", count, title[0,32])
end

.document_to_html(doc, ids, tmpdir) ⇒ Object



458
459
460
461
# File 'lib/ebps/conversion/oebps.rb', line 458

def self.document_to_html doc, ids, tmpdir
  factory = HtmlFactory.new(doc, ids, tmpdir)
  [factory.to_html, factory.uid]
end

.export(docs, target, override_tmpdir = nil) ⇒ Object



462
463
464
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
500
501
502
# File 'lib/ebps/conversion/oebps.rb', line 462

def self.export docs, target, override_tmpdir=nil
  ids = []
  with_tmpdir do |tmpdir|
    tmpdir = override_tmpdir || tmpdir
    # HTML
    if EBPS.config.sort
      docs = docs.sort_by do |doc|
        doc.title.sortable
      end
    end
    docs.each_with_index do |doc, idx|
      count = idx.next
      local_ids = []
      html, id = document_to_html doc, local_ids, tmpdir
      name = document_filename doc, count
      write tmpdir, name, html
      ids.push [name, id, doc.title, local_ids]
    end
    # Indices
    ncx_ids = write_index ids, tmpdir
    # NCX
    xml = to_ncx docs, ncx_ids, tmpdir
    write tmpdir, 'toc.ncx', xml
    # Copy the Cover
    cover = EBPS.config.cover
    FileUtils.cp cover, File.join(tmpdir, File.basename(cover))
    # Copy the stylesheet
    style = EBPS.config.stylesheet
    FileUtils.cp style, File.join(tmpdir, File.basename(style))
    # Appendix
    if appendix = EBPS.config.appendix
      add_appendix appendix, ids, tmpdir
    end
    # OPF
    title = EBPS.config.title || docs.first.title
    name = filename("%s.opf" % title)
    xml = to_opf docs, ids, tmpdir
    write tmpdir, name, xml
    yield tmpdir, name
  end
end

.filename(name) ⇒ Object



503
504
505
# File 'lib/ebps/conversion/oebps.rb', line 503

def self.filename name
  name.gsub(/\s+/u, '_').gsub(/[^0-9a-z_.]/iu, '')
end

.to_index(key, keys, ids, tmpdir) ⇒ Object



506
507
508
# File 'lib/ebps/conversion/oebps.rb', line 506

def self.to_index key, keys, ids, tmpdir
  IndexFactory.new(key, keys, ids, tmpdir).to_html
end

.to_ncx(docs, ids, tmpdir) ⇒ Object



509
510
511
# File 'lib/ebps/conversion/oebps.rb', line 509

def self.to_ncx docs, ids, tmpdir
  NcxFactory.new(docs, ids, tmpdir).to_ncx
end

.to_opf(docs, ids, tmpdir) ⇒ Object



512
513
514
# File 'lib/ebps/conversion/oebps.rb', line 512

def self.to_opf docs, ids, tmpdir
  OpfFactory.new(docs, ids, tmpdir).to_opf
end

.with_tmpdir(&block) ⇒ Object



515
516
517
518
519
520
# File 'lib/ebps/conversion/oebps.rb', line 515

def self.with_tmpdir &block
  tmpdir = Dir.mktmpdir
  block.call tmpdir
ensure
  FileUtils.rm_r tmpdir unless EBPS.config.keep_tmpfiles
end

.write(tmpdir, name, data) ⇒ Object



521
522
523
524
525
526
# File 'lib/ebps/conversion/oebps.rb', line 521

def self.write tmpdir, name, data
  FileUtils.mkdir_p tmpdir
  path = File.join tmpdir, name
  File.open path, 'wb' do |fh| fh.puts data end
  path
end

.write_index(ids, tmpdir) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/ebps/conversion/oebps.rb', line 527

def self.write_index ids, tmpdir
  tbl = {}
  ids.each do |data|
    name, id, title, local_ids = data
    (tbl[title.sortable[0,1]] ||= []).push data
  end
  ncx_ids = []
  waypoints = []
  tbl.sort_by do |key, key_ids|
    /[a-z]/.match(key) ? key : "{#{key}"
  end.each do |key, key_ids|
    name = nil
    if EBPS.config.html_index
      name = "#{key}.html"
      html = to_index key, key_ids, ids, tmpdir
      write tmpdir, name, html
    else
      name, = key_ids.first
    end
    descr = "#{key.upcase} (#{key_ids.size})"
    nav_point = [ name, key, descr ]
    ncx_ids.push nav_point
    ## Kindle does not like nested nav_points for content that is not
    #  contained in the same file.
    if EBPS.config.kindle_quirks
      waypoints.concat key_ids
    else
      nav_point.push key_ids
    end
  end
  if EBPS.config.html_index
    html = to_index 'toc-html', ncx_ids, ids, tmpdir
    write tmpdir, 'toc.html', html
    ids.replace(ncx_ids + ids)
    ids.unshift [ 'toc.html', 'toc-html', EBPS.config.index_name ]
  end
  if EBPS.config.kindle_quirks
    ncx_ids.concat waypoints
  end
  ncx_ids
end