Class: Peregrin::Main

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

Defined Under Namespace

Classes: UnknownFileFormat

Class Method Summary collapse

Class Method Details

.convert(src_path, dest_path, src_klass = nil, dest_klass = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/peregrin.rb', line 70

def self.convert(src_path, dest_path, src_klass = nil, dest_klass = nil)
  src_klass ||= format_for_path(src_path)
  dest_klass ||= format_for_path(dest_path)

  src_ook = src_klass.read(src_path)

  # FIXME: how do we do these options? User-specified? Dest-format-specified?
  options = {}
  options[:componentize] = true  if dest_klass == Peregrin::Epub
  book = src_ook.to_book(options)

  dest_ook = dest_klass.new(book)
  dest_ook.write(dest_path)
  validate(dest_path)
end

.inspect(path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/peregrin.rb', line 87

def self.inspect(path)
  klass = format_for_path(path)
  ook = klass.read(path)
  book = ook.to_book
  puts "[#{klass::FORMAT}]"
  puts "\nCover\n  #{book.cover.src}"
  puts "\nComponents [#{book.components.size}]"
  book.components.each { |cmpt| puts "  #{cmpt.src}" }
  puts "\nResources [#{book.resources.size}]"
  book.resources.each { |res| puts "  #{res.src}" }
  puts "\nChapters"
  book.chapters.each { |chp| print_chapter_title(chp, "- ") }
  puts "\nProperties [#{book.properties.size}]"
  book.properties.each { |property|
    puts "  #{property.key}: #{property.value}"  unless property.value.empty?
  }
  true
end

.run(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/peregrin.rb', line 32

def self.run(args)
  if args.size == 1
    src = args.first
    validate(src) and inspect(src)
  elsif args.size == 2
    src, dest = args
    validate(src) and convert(src, dest) and inspect(dest)
  else
    usage
  end
end

.usageObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/peregrin.rb', line 45

def self.usage
  puts "Peregrin [http://ochook.org/peregrin]"
  puts "Version: #{VERSION}"
  puts "A tool for inspecting Zhooks, Ochooks and EPUB ebooks,"
  puts "and converting between them."
  puts ""
  puts "Usage: peregrin srcpath [destpath]"
  puts ""
  puts "If one path given, validates ebook at that path and outputs analysis."
  puts "If two paths given, converts from srcpath to destpath and outputs "
  puts "analysis of converted ebook."
end

.validate(path) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/peregrin.rb', line 59

def self.validate(path)
  klass = format_for_path(path)
  klass.validate(path)
  true
rescue UnknownFileFormat => e
  exit_with("Unknown file format: #{path}")
rescue => e
  exit_with("Invalid #{klass::FORMAT}: #{path}", "Reason - #{e}")
end