Class: Epub

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-epub.rb

Constant Summary collapse

BANNED_META =
['office', 'amanuensis', 'calibre:user_metadata']

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Epub

Returns a new instance of Epub.



8
9
10
11
12
# File 'lib/ruby-epub.rb', line 8

def initialize(filename)
   @zip = Zip::ZipFile.open(filename)
   opf = get_opf(@zip)
   ()
end

Instance Method Details

#banned?(meta) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/ruby-epub.rb', line 42

def banned?(meta)    
 BANNED_META.each do |ban|
   if meta[0, ban.size] == ban
     return true
   end
 end
 return false
end

#cover_by_cover_idObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ruby-epub.rb', line 125

def cover_by_cover_id
 puts "by id"
 img_item = @opf.at_xpath("//manifest/item[@id='cover']")
 if img_item 
   img_url = @base_path + img_item['href']
   @image_cover = @zip.get_input_stream(img_url) {|f| f.read}
   return [@image_cover, img_item['href']]
 else
  return nil
 end
end

#cover_by_meta_coverObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby-epub.rb', line 110

def cover_by_meta_cover
 puts "enter meta cover"
 img_id = @opf.search("//meta[@name='cover']")
 if img_id && img_id.first
 img_item = @opf.search("//manifest/item[@id='"+ img_id.first['content'] + "']")
 if img_item && img_item.first
   img_url = @base_path + img_item.first['href']
   @image_cover = @zip.get_input_stream(img_url) {|f| f.read}
   return [@image_cover, img_item.first['href']]
 else
  return nil
 end
 end
end

#cover_imageObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby-epub.rb', line 68

def cover_image
  content, img_name = cover_by_meta_cover
  if !content
#       content, img_name = cover_by_cover_id
#     elsif !content
    content, img_name = cover_image_by_html
  end
 
  if content
    temp = Tempfile.new(['book_cover', File.extname(img_name)])
    temp.binmode
    temp.write(content)
    temp.flush
    if(temp.size > 0)
      return File.new(temp.path)
    else
      return nil
    end
   end
end

#cover_image_by_htmlObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby-epub.rb', line 89

def cover_image_by_html
 puts "enter by html"
 cover_item = @opf.search("//package/guide/reference[@type='cover']")
 if cover_item && cover_item.first
   cover_url = cover_item.first['href']
   doc_cover = Nokogiri::HTML @zip.get_input_stream(@base_path + cover_url)
   tab_path = cover_url.split('/')
   html_path = ''
   if tab_path.size > 1
      html_path = tab_path[0] + '/'
   end
   img_src = doc_cover.xpath('//img').first
   begin
   	@image_cover = @zip.get_input_stream(@base_path + html_path + img_src['src']) {|f| f.read}
     return [@image_cover, img_src['src']]
   rescue
     return nil
   end
 end
end

#get_metadataObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby-epub.rb', line 51

def ()      
 @opf.at_xpath("//metadata").children.each do |elem|
   #puts elem.inspect
   if elem.name != 'text'
     if elem.name == "meta" && !banned?(elem['name'])
       name_elem = elem['name'].tr(' ', '_').tr(':', '_').tr('.', '_')
       content_elem = elem['content']
     else
       name_elem = elem.name
       content_elem = elem.content
     end
      name_elem
     send(name_elem + '=', content_elem)
   end
 end
end

#get_opf(zipfile) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-epub.rb', line 28

def get_opf(zipfile)
   container_file = zipfile.get_input_stream("META-INF/container.xml")
   container = Nokogiri::XML container_file
   opf_path = container.at_css("rootfiles rootfile")['full-path']
   tab_path = opf_path.split('/')
   @base_path = ''
   if tab_path.size > 1 
      @base_path = tab_path[0] + '/'
   end
   opf_file = zipfile.get_input_stream(opf_path)
   @opf = Nokogiri::XML opf_file
   @opf.remove_namespaces!
end

#my_metadata(name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby-epub.rb', line 14

def (name)
  begin
    # getter
    define_singleton_method("#{name}=") do |val|
      instance_variable_set("@#{name}", val)  
    end 
    # setter
    define_singleton_method("#{name}") do
      instance_variable_get("@#{name}")
    end
    rescue
    end 
end