Module: EpubSetup

Defined in:
lib/epub_setup.rb

Overview

EpubSetup making directories and moving files and whatnot

Instance Method Summary collapse

Instance Method Details

#make_skeleton(base_dir, epub_folder) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/epub_setup.rb', line 4

def make_skeleton(base_dir, epub_folder)
  @epub_folder = epub_folder
  @source_templates_dir = File.join(base_dir, 'templates')
  @target_meta_dir = File.join(@epub_folder, 'META-INF')
  @target_oebps_dir = File.join(@epub_folder, 'OEBPS')

  FileUtils.rm_rf @epub_folder if File.exists?(@epub_folder)
  FileUtils.mkdir_p @epub_folder
  FileUtils.mkdir_p @target_meta_dir
  FileUtils.mkdir_p @target_oebps_dir

  FileUtils.cp File.join(@source_templates_dir, 'mimetype'), @epub_folder
  FileUtils.cp File.join(@source_templates_dir, 'META-INF', 'container.xml'), @target_meta_dir

  # TODO - somehow detect these "asset" folders and files - 
  # for now they are these defaults: css, images, fonts
  FileUtils.cp Dir[File.join(@source_templates_dir, 'OEBPS', '*.css')], @target_oebps_dir
  FileUtils.cp_r File.join(@source_templates_dir, 'OEBPS', 'images'), @target_oebps_dir
  FileUtils.cp_r File.join(@source_templates_dir, 'OEBPS', 'fonts'), @target_oebps_dir

  # liquid templates for rest of files
  @chapter_liq_template = Liquid::Template.parse(File.read(File.join(@source_templates_dir, 'OEBPS', 'chapter.html.liquid'    )))
  @content_liq_template = Liquid::Template.parse(File.read(File.join(@source_templates_dir, 'OEBPS', 'content.opf.liquid'     )))
  @title_liq_template   = Liquid::Template.parse(File.read(File.join(@source_templates_dir, 'OEBPS', 'title.html.liquid'      )))
  @toc_liq_template     = Liquid::Template.parse(File.read(File.join(@source_templates_dir, 'OEBPS', 'toc.ncx.liquid'         )))
  @eob_liq_template     = Liquid::Template.parse(File.read(File.join(@source_templates_dir, 'OEBPS', 'end_of_book.html.liquid')))
end

#read_chapters(file_glob) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/epub_setup.rb', line 32

def read_chapters(file_glob)
  file_glob = File.expand_path(file_glob)
  puts "Reading files: #{file_glob} (#{Dir[file_glob].size} files found)"
  chapters = []

  Dir[file_glob].each do |txtfile|
    chapter = nil
    File.open(txtfile) do |f|
      chapter = Chapter.new(f.readlines)
      chapter.file_name = "#{File.basename(txtfile, '.txt')}.html"
    end
    chapters << chapter if chapter
  end

  # returns chapters as an array sorted by name
  chapters.sort_by { |c| [c.number, c.name, c.file_name] }
end

#write_templates(book) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/epub_setup.rb', line 50

def write_templates(book)
  book.chapters.each do |chapter|
    html_output = @chapter_liq_template.render 'chapter' => chapter
    puts "Writing: #{@epub_folder}/OEBPS/#{chapter.file_name}"
    File.open(File.join(@target_oebps_dir, chapter.file_name), "w") { |f| f.puts html_output }
  end

  puts "Writing: #{@epub_folder}/OEBPS/content.opf"
  File.open(File.join(@target_oebps_dir, 'content.opf'), "w") { |f| f.puts @content_liq_template.render('book' => book,
      'css_files'   => Dir[File.join(@source_templates_dir, 'OEBPS', '*.css')].map    { |f| File.basename(f) },
      'image_files' => Dir[File.join(@source_templates_dir, 'OEBPS', 'images', '*')].map { |f| File.basename(f) },
      'font_files'  => Dir[File.join(@source_templates_dir, 'OEBPS', 'fonts', '*')].map  { |f| File.basename(f) } ) }
  puts "Writing: #{@epub_folder}/OEBPS/title.html"
  File.open(File.join(@target_oebps_dir, 'title.html'), "w") { |f| f.puts @title_liq_template.render('book' => book) }
  puts "Writing: #{@epub_folder}/OEBPS/end_of_book.html"
  File.open(File.join(@target_oebps_dir, 'end_of_book.html'), "w") { |f| f.puts @eob_liq_template.render('book' => book) }
  puts "Writing: #{@epub_folder}/OEBPS/toc.ncx"
  File.open(File.join(@target_oebps_dir, 'toc.ncx'), "w") { |f| f.puts @toc_liq_template.render('book' => book) }
end