Class: Peregrin::Ochook

Inherits:
Zhook
  • Object
show all
Defined in:
lib/formats/ochook.rb

Defined Under Namespace

Classes: DirectoryNotFound, IndexHTMLRootHasNoManifest, MissingManifest

Constant Summary collapse

FORMAT =
"Ochook"
MANIFEST_PATH =
"ochook.manifest"

Constants inherited from Zhook

Zhook::BODY_XPATH, Zhook::COVER_PATH, Zhook::FILE_EXT, Zhook::HEAD_XPATH, Zhook::INDEX_PATH

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book) ⇒ Ochook

Returns a new instance of Ochook.



49
50
51
52
# File 'lib/formats/ochook.rb', line 49

def initialize(book)
  super
  insert_manifest_attribute
end

Class Method Details

.read(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/formats/ochook.rb', line 29

def self.read(path)
  path = path.gsub(/\/$/, '')
  validate(path)
  book = Peregrin::Book.new
  book.add_component(INDEX_PATH, IO.read(File.join(path, INDEX_PATH)))
  Dir.glob(File.join(path, '**', '*')).each { |fpath|
    ex = [INDEX_PATH, MANIFEST_PATH]
    mpath = fpath.gsub(/^#{path}\//,'')
    unless File.directory?(fpath) || ex.include?(mpath)
      book.add_resource(mpath)
    end
  }
  book.read_resource_proc = lambda { |resource|
    IO.read(File.join(path, resource.src))
  }
  extract_properties_from_index(book)
  new(book)
end

.validate(path) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/formats/ochook.rb', line 6

def self.validate(path)
  path = path.gsub(/\/$/, '')
  unless File.directory?(path)
    raise DirectoryNotFound.new(path)
  end
  unless File.exists?(File.join(path, INDEX_PATH))
    raise MissingIndexHTML.new(path)
  end
  unless File.exists?(File.join(path, COVER_PATH))
    raise MissingCoverPNG.new(path)
  end
  unless File.exists?(File.join(path, MANIFEST_PATH))
    raise MissingManifest.new(path)
  end

  doc = Nokogiri::HTML::Document.parse(IO.read(File.join(path, INDEX_PATH)))
  raise IndexHTMLRootHasId.new(path)  if doc.root['id']
  unless doc.root['manifest'] = MANIFEST_PATH
    raise IndexHTMLRootHasNoManifest.new(path)
  end
end

Instance Method Details

#to_book(options = {}) ⇒ Object



85
86
87
88
# File 'lib/formats/ochook.rb', line 85

def to_book(options = {})
  remove_manifest_attribute
  super(options)
end

#write(dir) ⇒ Object



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
# File 'lib/formats/ochook.rb', line 55

def write(dir)
  FileUtils.rm_rf(dir)  if File.directory?(dir)
  FileUtils.mkdir_p(dir)

  # Index
  index_path = File.join(dir, INDEX_PATH)
  File.open(index_path, 'w') { |f| f << htmlize(index) }

  # Resources
  @book.resources.each { |resource|
    full_path = File.join(dir, resource.src)
    FileUtils.mkdir_p(File.dirname(full_path))
    File.open(full_path, 'w') { |f| f << @book.read_resource(resource) }
  }

  # Cover
  unless @book.cover == COVER_PATH
    cover_path = File.join(dir, COVER_PATH)
    File.open(cover_path, 'wb') { |f| f << to_png_data(@book.cover) }
    unless @book.resources.detect { |r| r.src == COVER_PATH }
      @book.add_resource(COVER_PATH)
    end
  end

  # Manifest
  manifest_path = File.join(dir, MANIFEST_PATH)
  File.open(manifest_path, 'w') { |f| f << manifest.join("\n") }
end