kindler 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -15,5 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- kindler_generated*
19
18
  .DS_Store
19
+ __*
@@ -1,3 +1,3 @@
1
1
  module Kindler
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/kindler.rb CHANGED
@@ -261,30 +261,51 @@ module Kindler
261
261
  @opf = contents
262
262
  end
263
263
 
264
+ def get_image_extname(image_data,url)
265
+ ext = File.extname('url')
266
+ if ext == ''
267
+ ext = case image_data.content_type
268
+ when /jpeg/i
269
+ '.jpg'
270
+ when /png/i
271
+ '.png'
272
+ when /gif/i
273
+ '.gif'
274
+ else
275
+ '.jpg'
276
+ end
277
+ end
278
+ ext
279
+ end
280
+
264
281
  def localize_images
265
282
  images_count = 1
266
283
  pages.each do |page|
267
284
  article = Nokogiri::HTML(page[:content])
268
285
  article.css('img').each do |img|
269
286
  begin
287
+ # get remote address
270
288
  image_remote_address = img.attr('src')
271
289
  unless image_remote_address.start_with?('http')
272
290
  image_remote_address = "http://#{URI(page[:url]).host}#{image_remote_address}"
273
291
  end
274
- image_local_address = File.join(tmp_dir,"#{images_count}#{File.extname(image_remote_address)}")
292
+ # get local address
293
+ image_data = open(image_remote_address)
294
+ image_extname = get_image_extname(image_data,image_remote_address)
295
+ image_local_address = File.join(tmp_dir,"#{images_count}#{image_extname}")
275
296
  # download images
276
297
  debug "begin fetch image #{image_remote_address}"
277
298
  debug "save to #{image_local_address}"
278
- `curl #{image_remote_address} > #{image_local_address}`
279
- # File.open(image_local_address,'wb') do |f|
280
- # f.write open(image_remote_address).read
281
- # end
299
+ #`curl #{image_remote_address} > #{image_local_address}`
300
+ File.open(image_local_address,'wb') do |f|
301
+ f.write image_data.read
302
+ end
282
303
  debug 'Image saved'
283
304
  # replace local url address
284
- img.attributes['src'].value = "#{images_count}#{File.extname(image_remote_address)}"
305
+ img.attributes['src'].value = "#{image_local_address}"
285
306
  page[:content] = article.inner_html
286
307
  # add to manifest
287
- local_images << "#{images_count}#{File.extname(image_remote_address)}"
308
+ local_images << "#{image_local_address}"
288
309
  images_count += 1
289
310
  rescue Exception => e
290
311
  debug "got error when fetch and save image: #{e}"
@@ -3,7 +3,7 @@ describe "Mobi book file generator" do
3
3
 
4
4
  after :all do
5
5
  puts '==== clear tmp files ==='
6
- `rm -rf ./__*`
6
+ #`rm -rf ./__*`
7
7
  end
8
8
 
9
9
  it "should have the title,author property" do
@@ -58,7 +58,7 @@ describe "Mobi book file generator" do
58
58
  book = Kindler::Book.new :title=>title,:author=>'mike',:debug=>true
59
59
  book.add_page :title=>'page1',:author=>'mike1',:content=>'this is the page 1',:wrap=>true
60
60
  book.add_page :title=>'page2',:author=>'mike1',:content=>'this is the page 2',:wrap=>true
61
- book.add_page :title=>'page3',:author=>'mike1',:content=>'<img src="http://media2.glamour-sales.com.cn/media/catalog/category/Stroili_banner_02.jpg"></img>this is the page 3',:wrap=>true
61
+ book.add_page :title=>'page3',:author=>'mike1',:content=>'<img src="http://media2.glamour-sales.com.cn/media/catalog/category/Stroili_banner_02.jpg"/>this is the page 3',:wrap=>true
62
62
  book.generate
63
63
  book.should be_generated
64
64
  File.should be_exist("./#{Kindler::Book::TMP_DIR_PREFIX}#{title}/1.jpg")
@@ -110,7 +110,7 @@ describe "Mobi book file generator" do
110
110
  book = Kindler::Book.new :title=>title,:author=>'mike',:debug=>true
111
111
  book.add_page :title=>'page1',:author=>'mike1',:content=>'this is the page 1',:wrap=>true
112
112
  book.add_page :title=>'page2',:author=>'mike1',:content=>'this is the page 2',:wrap=>true
113
- book.add_page :title=>'page3',:author=>'mike1',:url => 'http://media2.glamour-sales.com.cn/media/some_url',:content=>'<img src="/media/catalog/category/Stroili_banner_02.jpg"></img>this is the page 3',:wrap=>true
113
+ book.add_page :title=>'page3',:author=>'mike1',:url => 'http://media2.glamour-sales.com.cn/media/some_url',:content=>'<img src="/media/catalog/category/Stroili_banner_02.jpg"/>this is the page 3',:wrap=>true
114
114
  book.generate
115
115
  book.should be_generated
116
116
  File.should be_exist("./#{Kindler::Book::TMP_DIR_PREFIX}#{title}/1.jpg")
@@ -138,4 +138,15 @@ describe "Mobi book file generator" do
138
138
  File.should be_exist(File.expand_path(custom_dir))
139
139
  end
140
140
 
141
+ it "should get correct image type when no extname found" do
142
+ title = "no_extname_found_book"
143
+ book = Kindler::Book.new :title=>title,:author=>'mike',:debug=>true
144
+ image_url = "https://lh3.googleusercontent.com/Lpu3TQdWzvnJKkx4U4uyjJzQxvXSFTbwbb_Ni3XJp8stydrlKpI_VHbY2rAcphMMcrkq-wev2zKkSpbGSQr_T5BfrRuHYqlLQ-NTPmPk--mS2PYu_Rk"
145
+ book.add_page :title=>'page1',:author=>'mike1',:content=>'this is the page 1',:wrap=>true
146
+ book.add_page :title=>'page2',:author=>'mike1',:content=>'this is the page 2',:wrap=>true
147
+ book.add_page :title=>'page3',:author=>'mike1',:content=>"<img src='#{image_url}'/>this is the page 3",:wrap=>true
148
+ book.generate
149
+ book.should be_generated
150
+ end
151
+
141
152
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kindler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-08 00:00:00.000000000Z
12
+ date: 2012-04-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &2154641200 !ruby/object:Gem::Requirement
16
+ requirement: &2153362980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2154641200
24
+ version_requirements: *2153362980
25
25
  description: kindler is a rubygem allow you to generate kindle mobi book very easily
26
26
  email:
27
27
  - mike.d.1984@gmail.com