kindler 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/kindler ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'optparse'
4
+ require 'open-uri'
5
+ require File.dirname(__FILE__) + "/../lib/kindler"
6
+ # get options
7
+ options = {:title=>'kindler_ebook',:urls=>ARGV}
8
+ option_parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage kindler url1 url2 url3 [-t my_book] [-d]"
10
+
11
+ opts.on("-d",'--debug','show debug infos') do |d|
12
+ options[:debug] = d
13
+ end
14
+
15
+ opts.on("-t",'--title','title of book') do |t|
16
+ options[:title] = t
17
+ end
18
+
19
+ opts.on_tail('-h','--help','show this message') do
20
+ puts opts
21
+ exit
22
+ end
23
+
24
+ end
25
+
26
+ exit unless ARGV.length > 0
27
+ book = Kindler::Book.new(options)
28
+ book.generate
29
+
@@ -1,3 +1,3 @@
1
1
  module Kindler
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/kindler.rb CHANGED
@@ -1,4 +1,5 @@
1
- require "kindler/version"
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__)+"/kindler/version"
2
3
  require "readability"
3
4
  require "open-uri"
4
5
  # require 'mini_magick'
@@ -15,12 +16,17 @@ module Kindler
15
16
  # @option urls [Array] urls to generate
16
17
  # @option title [String] book title
17
18
  # @option output_dir [String] directory want to generate
19
+ # @option debug [Boolean] whether puts debug infos
20
+ # @option keep_image [Boolean] whether keep images, default to true
18
21
  def initialize(options={})
19
22
  @urls = options[:urls] || {}
20
23
  @title = options[:title] || ''
21
24
  @output_dir = options[:output_dir] || './'
25
+ @keep_image = options[:keep_image] || true
26
+ @debug = options[:debug]
22
27
  raise KindlerError.new("urls option could not be empty") if @urls.empty?
23
28
  @author = options[:author] || ''
29
+ @images = []
24
30
  @doc_infos = {}
25
31
  # init doc infos by url
26
32
  @urls.each {|url| @doc_infos[url]= {} }
@@ -41,9 +47,11 @@ module Kindler
41
47
  make_generated_dirs
42
48
  # generate
43
49
  generate_html
50
+ localize_images
44
51
  generate_toc
45
52
  generate_opf
46
53
  generate_ncx
54
+ write_to_disk
47
55
  kindlegen
48
56
  # clear
49
57
  end
@@ -57,7 +65,7 @@ module Kindler
57
65
  # make sure kindlegen is installed
58
66
  # you can use "sudo brew install " to install it
59
67
  def kindlegen
60
- puts 'begin generate mobi'
68
+ debug 'begin generate mobi'
61
69
  system("kindlegen #{tmp_dir}/#{@title}.opf ")
62
70
  end
63
71
 
@@ -80,9 +88,8 @@ module Kindler
80
88
  end
81
89
  # append footer
82
90
  contents << "</ul></body></html>"
83
- File.open(file_path('contents'),'w') do |f|
84
- f.puts contents
85
- end
91
+
92
+ @toc = contents
86
93
  end
87
94
 
88
95
  # generate ncx , which is navigation
@@ -145,7 +152,7 @@ module Kindler
145
152
  files_count += 1
146
153
  end
147
154
  contents << "</navMap></ncx>"
148
- File.open("#{tmp_dir}/nav-contents.ncx",'w') { |f| f.puts contents }
155
+ @ncx = contents
149
156
  end
150
157
 
151
158
  # generate the opf, manifest of book,including all articles and images and css
@@ -189,7 +196,7 @@ module Kindler
189
196
  files_count += 1
190
197
  end
191
198
  contents << "</spine><guide><reference href='contents.html' type='toc' title='Table of Contents'/></guide></package>"
192
- File.open("#{tmp_dir}/#{@title}.opf",'w') {|f| f.puts contents}
199
+ @opf = contents
193
200
  end
194
201
 
195
202
  # generate every url to article in readable format
@@ -200,13 +207,32 @@ module Kindler
200
207
  infos[:content] = html_wrap(article.title,article.content)
201
208
  infos[:title] = article.title
202
209
  end
203
- # make html files
204
- files_count = 1
210
+ end
211
+
212
+ def localize_images
213
+ images_count = 1
205
214
  @doc_infos.each do |url,infos|
206
- File.open(file_path(files_count.to_s.rjust(3,'0')),'w') do |f|
207
- f.puts infos[:content]
215
+ article = Nokogiri::HTML(infos[:content])
216
+ article.css('img').each do |img|
217
+ image_remote_address = img.attr('src')
218
+ unless image_remote_address.start_with?('http')
219
+ image_remote_address = "http://#{URI(url).host}#{image_remote_address}"
220
+ end
221
+ image_local_address = File.join(tmp_dir,"#{images_count}#{File.extname(image_remote_address)}")
222
+ # download images
223
+ debug "begin fetch image #{image_remote_address}"
224
+ debug "save to #{image_local_address}"
225
+ File.open(image_local_address,'w') do |f|
226
+ f.puts open(image_remote_address).read
227
+ end
228
+ debug 'Image saved'
229
+ # replace local url address
230
+ img.attributes['src'].value = "#{images_count}#{File.extname(image_remote_address)}"
231
+ infos[:content] = article.inner_html
232
+ # add to manifest
233
+ @images << "#{images_count}#{File.extname(image_remote_address)}"
234
+ images_count += 1
208
235
  end
209
- files_count += 1
210
236
  end
211
237
  end
212
238
 
@@ -228,9 +254,13 @@ module Kindler
228
254
 
229
255
  # get readable document by url, using ruby-readability here
230
256
  def readable_article(url)
231
- puts "begin fetch url : #{url}"
257
+ debug "begin fetch url : #{url}"
232
258
  source = open(url).read
233
- Readability::Document.new(source)
259
+ if @keep_image
260
+ Readability::Document.new(source,:tags=>%w(div p img a),:attributes => %w[src href],:remove_empty_nodes => false)
261
+ else
262
+ Readability::Document.new(source)
263
+ end
234
264
  end
235
265
 
236
266
  # the dir path to generated files
@@ -244,11 +274,32 @@ module Kindler
244
274
  FileUtils.mkdir_p tmp_dir unless File.exist?(tmp_dir)
245
275
  end
246
276
 
277
+ def write_to_disk
278
+ File.open("#{tmp_dir}/nav-contents.ncx",'w') { |f| f.puts @ncx }
279
+ File.open(file_path('contents'),'w') {|f| f.puts @toc }
280
+ File.open("#{tmp_dir}/#{@title}.opf",'w') {|f| f.puts @opf}
281
+ # make html files
282
+ files_count = 1
283
+ @doc_infos.each do |url,infos|
284
+ File.open(file_path(files_count.to_s.rjust(3,'0')),'w') do |f|
285
+ f.puts infos[:content]
286
+ end
287
+ files_count += 1
288
+ end
289
+
290
+ end
291
+
247
292
  # exist to clear tmp files such as ncx,opf or html other than mobi file
248
293
  # keep them right now
249
294
  def clear_tmp_dirs
250
295
 
251
296
  end
252
297
 
298
+ def debug(str)
299
+ return unless @debug
300
+ Rails.logger.info(str) if defined?(Rails)
301
+ puts str
302
+ end
303
+
253
304
  end
254
305
  end
@@ -5,7 +5,7 @@ describe "Mobi html file generator" do
5
5
  title = "Test_book"
6
6
  book = Kindler::Book.new ({:urls=>["http://blog.farmostwood.net/643.html",
7
7
  "http://www.ifanr.com/69878","http://www.oneplus.info/archives/455"],
8
- :title=>title,:author=>'mike'})
8
+ :title=>title,:author=>'mike',:debug=>true})
9
9
  book.generate
10
10
  File.exist?(mobi_book_path(title)).should == true
11
11
  end
@@ -20,7 +20,7 @@ describe "Mobi html file generator" do
20
20
  urls << "http://tumblr.intranation.com/post/766290565/how-set-up-your-own-private-git-server-linux"
21
21
  urls << "http://antirez.com/post/what-is-wrong-with-2006-programming.html"
22
22
  urls << "http://fak3r.com/2009/09/14/howto-build-your-own-open-source-dropbox-clone/"
23
- book = Kindler::Book.new :urls=>urls,:title=>title,:author=>'mike'
23
+ book = Kindler::Book.new :urls=>urls,:title=>title,:author=>'mike',:debug=>true
24
24
  book.generate
25
25
  File.exist?(mobi_book_path(title)).should == true
26
26
  end
@@ -31,7 +31,7 @@ describe "Mobi html file generator" do
31
31
  urls << "http://www.wowsai.com/home/space.php?uid=1&do=blog&id=4362&classid=2"
32
32
  urls << "http://www.honeykennedy.com/2012/01/miss-moss-love-letters/"
33
33
  urls << "http://www.mysenz.com/?p=3692"
34
- book = Kindler::Book.new :urls=>urls,:title=>title,:author=>'mike',:output_dir=>'/Users/lidongbin/projects'
34
+ book = Kindler::Book.new :urls=>urls,:title=>title,:author=>'mike',:output_dir=>'/Users/lidongbin/projects',:debug=>true
35
35
  book.generate
36
36
  File.exist?(mobi_book_path(title,'/Users/lidongbin/projects')).should == true
37
37
  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.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mini_magick
16
- requirement: &2162549440 !ruby/object:Gem::Requirement
16
+ requirement: &2157488800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2162549440
24
+ version_requirements: *2157488800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ruby-readability
27
- requirement: &2162549020 !ruby/object:Gem::Requirement
27
+ requirement: &2157488380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,11 +32,12 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2162549020
35
+ version_requirements: *2157488380
36
36
  description: a simple gem to generate kindle mobi book
37
37
  email:
38
38
  - mike.d.1984@gmail.com
39
- executables: []
39
+ executables:
40
+ - kindler
40
41
  extensions: []
41
42
  extra_rdoc_files: []
42
43
  files:
@@ -45,11 +46,10 @@ files:
45
46
  - Guardfile
46
47
  - Rakefile
47
48
  - Readme.md
49
+ - bin/kindler
48
50
  - kindler.gemspec
49
51
  - lib/kindler.rb
50
- - lib/kindler/html_generator.rb
51
52
  - lib/kindler/railtie.rb
52
- - lib/kindler/toc_generator.rb
53
53
  - lib/kindler/version.rb
54
54
  - spec/cases/generator_spec.rb
55
55
  - spec/spec_helper.rb
@@ -1,7 +0,0 @@
1
- module Kindler
2
- module HtmlGenerator
3
- def g_html
4
-
5
- end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- module Kindler
2
- module TocGenerator
3
- def g_toc
4
-
5
- end
6
- end
7
- end