Class: EpubTools::AddChapters

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/epub_tools/add_chapters.rb

Overview

Moves new chapters into an unpacked EPUB

Instance Method Summary collapse

Methods included from Loggable

#log

Constructor Details

#initialize(options = {}) ⇒ AddChapters

Initializes the class

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :chapters_dir (String)

    Directory from which to move the xhtml chapters. It assumes the directory will contain one or more files named chapter_XX.xhtml, where XX is a number. (default: ‘./chapters’)

  • :epub_dir (String)

    Unpacked EPUB directory to move the chapters to. It should be the same directory that contains the package.opf and nav.xhtml files. (default: ‘./epub/OEBPS’)

  • :verbose (Boolean)

    Whether to log progress to STDOUT (default: false)



19
20
21
22
23
24
25
26
27
# File 'lib/epub_tools/add_chapters.rb', line 19

def initialize(options = {})
  @chapters_dir = File.expand_path(options[:chapters_dir] || './chapters')
  @epub_dir = File.expand_path(options[:oebps_dir] || './epub/OEBPS')
  @opf_file = File.join(@epub_dir, 'package.opf')
  @nav_file = File.join(@epub_dir, 'nav.xhtml')
  @verbose = options[:verbose] || false

  validate_directories!
end

Instance Method Details

#runArray<String>

It works like this:

  • First, the *.xhtml files are moved from chapters_dir over to epub_dir

  • Then, new entries will be added to the manifest and spine of the EPUB’s package.opf file. It will sort the files by extracting the chapter number.

  • Finally, it will update the nav.xhtml file with the new chapters. Note that if there’s a file named chapter_0.xhtml, it will be added to the nav.xhtml as the Prologue.

Returns:

  • (Array<String>)

    List of moved chapter filenames



36
37
38
39
40
41
42
# File 'lib/epub_tools/add_chapters.rb', line 36

def run
  moved_files = move_chapters
  update_package_opf(moved_files)
  update_nav_xhtml(moved_files)
  moved_files.each { |f| log("Moved: #{f}") }
  moved_files
end