Class: Bookbinder::Operations

Inherits:
Object
  • Object
show all
Defined in:
lib/bookbinder/operations.rb

Class Method Summary collapse

Class Method Details

.convert(src_path, dest_path) ⇒ Object

This will take the ebook at src_path, map it, then convert it to the format recognized in dest_path. Be aware that ANYTHING already at dest_path will be replaced.



56
57
58
59
60
61
62
63
64
# File 'lib/bookbinder/operations.rb', line 56

def convert(src_path, dest_path)
  src_klass = recognize(src_path)
  dest_klass = recognize(dest_path)
  src_pkg = src_klass.read(src_path)
  dest_pkg = src_pkg.export(dest_klass)
  dest_pkg.write(dest_path)
  Bookbinder::Scratch.cleanup
  [src_pkg, dest_pkg]
end

.map(path) ⇒ Object



22
23
24
# File 'lib/bookbinder/operations.rb', line 22

def map(path)
  JSON.pretty_generate(validate(path).map)
end

.normalize(path) ⇒ Object

This will REPLACE the ebook with itself, after POSSIBLY LOSSY parsing by Bookbinder. Be careful! If you want to copy-and-normalize, use convert.



43
44
45
46
47
48
49
# File 'lib/bookbinder/operations.rb', line 43

def normalize(path)
  pkg_klass = recognize(path)
  pkg = pkg_klass.read(path)
  pkg.write(path)
  Bookbinder::Scratch.cleanup
  pkg
end

.package_classesObject



5
6
7
8
9
# File 'lib/bookbinder/operations.rb', line 5

def package_classes
  ObjectSpace.each_object(Class).select { |klass|
    klass < Bookbinder::Package
  }
end

.recognize(path) ⇒ Object

This inspects a path and gives you the package class that probably can read or write it.



15
16
17
18
19
# File 'lib/bookbinder/operations.rb', line 15

def recognize(path)
  package_classes.detect { |klass|
    klass.recognize(path)
  } || raise(Bookbinder::UnknownFormat, path)
end

.validate(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/bookbinder/operations.rb', line 27

def validate(path)
  pkg_klass = recognize(path)
  pkg = pkg_klass.read(path)
  # TODO: Warnings?
  # pkg.warnings.each { |warn|
  #   STDERR.puts("#{warn[:transform]}##{warn[:method]}: #{warn[:message]}")
  # }
  Bookbinder::Scratch.cleanup
  pkg
end