Module: Kindlerb

Defined in:
lib/kindlerb.rb,
lib/kindlerb/version.rb

Constant Summary collapse

VERSION =
'1.2.0'

Class Method Summary collapse

Class Method Details

.downloadObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kindlerb.rb', line 21

def self.download
  # use system kindlegen if available
  if system('kindlegen', out: :close)
    puts "Using system kindlegen"
    return
  end

  gem_path = Gem::Specification.find_by_name('kindlerb').gem_dir
  ext_dir = gem_path + '/ext/'
  bin_dir = gem_path + '/bin/'

  # Define Kindlegen download files for different OS's
  executable_filename = 'kindlegen'
  windows = false
  compressed_file = case RbConfig::CONFIG['host_os']
  when /mac|darwin/i
    extract = 'unzip '
    "KindleGen_Mac_i386_v2_9.zip"
  when /linux|cygwin/i
    extract = 'tar zxf '
    "kindlegen_linux_2.6_i386_v2_9.tar.gz"
  when /mingw32/i
    windows = true
    extract = 'unzip '
    executable_filename = 'kindlegen.exe'
    "kindlegen_win32_v2_9.zip"
  else
    STDERR.puts "Host OS is not supported!"
    exit(1)
  end

  url = 'http://kindlegen.s3.amazonaws.com/' + compressed_file

  # Download and extract the Kindlegen file into gem's /etc folder
  unless File.directory?(ext_dir)
    FileUtils.mkdir_p(ext_dir)
  end
  system 'curl ' + url + ' -o ' + ext_dir + compressed_file
  puts "Kindlegen downloaded: " + ext_dir + compressed_file
  system extract + ext_dir + compressed_file + ' -d ' + ext_dir

  # Move the executable_filename into gem's /bin folder
  unless File.directory?(bin_dir)
    FileUtils.mkdir_p(bin_dir)
  end
  moved = FileUtils.mv(ext_dir + executable_filename, bin_dir)
  puts "Kindlegen extracted to: " + bin_dir
  # Clean up ext folder
  if moved
    FileUtils.rm_rf(ext_dir)
  end

  # Give exec permissions to Kindlegen file
  exec_file = bin_dir + executable_filename
  if windows
    cmd = "icacls #{exec_file} /T /C /grant Everyone:(f)"
    system cmd
  else
    FileUtils.chmod 0754, exec_file
  end
  puts "Execution permissions granted to the user for Kindlegen executable"

end

.executableObject

Returns the full path to executable Kindlegen file



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

def self.executable
  # use system kindlegen if available
  return 'kindlegen' if system('kindlegen', out: :close)

  gem_path = Gem::Specification.find_by_name('kindlerb').gem_dir

  # Different extensions based on OS
  kindlegen = case RbConfig::CONFIG['host_os']
  when /mac|darwin/i
    "kindlegen"
  when /linux|cygwin/i
    "kindlegen"
  when /mingw32/i
    "kindlegen.exe"
  else
    return nil
  end

  exec_path = gem_path + '/bin/' + kindlegen

  return exec_path

end

.kindlegen_available?Boolean

Used for users to check whether Kindlerb can work

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kindlerb.rb', line 111

def self.kindlegen_available?
  kindlegen = self.executable
  case kindlegen
  when 'kindlegen'
    true
  when String
    File.exist?(kindlegen)
  else
    false
  end
end

.run(target_dir, verbose = false, compression_method = 'c2') ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/kindlerb.rb', line 123

def self.run(target_dir, verbose = false, compression_method = 'c2')
  unless self.kindlegen_available?
    STDERR.puts "Kindlegen is not available, install it with `setupkindlerb`"
    exit(1)
  end

  opf_template = File.read(File.join(File.dirname(__FILE__), '..', "templates/opf.mustache"))
  ncx_template = File.read(File.join(File.dirname(__FILE__), '..', "templates/ncx.mustache"))
  contents_template = File.read(File.join(File.dirname(__FILE__), '..', "templates/contents.mustache"))
  section_template = File.read(File.join(File.dirname(__FILE__), '..', "templates/section.mustache"))
  masthead_gif = File.join(File.dirname(__FILE__), '..', "templates/masthead.gif")
  cover_gif = File.join(File.dirname(__FILE__), '..', "templates/cover-image.gif")

  playorder = 1

  images = []
  manifest_items = []

  base_dir = Pathname.new target_dir
  document_yml = base_dir.join "_document.yml"

  unless File.exist? document_yml
    
    puts "Usage: kindlerb [target file directory]"

    abort "Missing _document.yml. Your input file tree is not structured correctly. Please read the README."
  end

  document = YAML::load_file document_yml

  document[:spine_items] = []
  section_html_files = []

  sections_all = base_dir.join "sections", "*"

  sections = Dir[sections_all].entries.sort.map.with_index {|section_dir| 
    c = File.read(Pathname.new(section_dir) + '_section.txt')
    c.force_encoding("UTF-8")
    section_title = c.strip
    articles = Dir[Pathname.new(section_dir) + '*'].entries.select {|x| File.basename(x) !~ /section/}.sort
    section_html_files << (section_html_file = (Pathname.new(section_dir) + 'section.html').to_s)
    idref = "item-#{section_dir.gsub(/\D/, '')}"

    document[:spine_items] << {:idref => idref}
    manifest_items << {
      :href => section_html_file,
      :media => "application/xhtml+xml",
      :idref => idref
    }

    s = {
      :path => section_dir,
      :title => section_title.shorten(40),
      :playorder => (playorder += 1),
      :idref => idref,
      :href => Pathname.new(section_dir) + 'section.html',
      :articles => articles.map {|article_file|
            doc = Nokogiri::HTML(File.read(article_file, :encoding => 'UTF-8'))
            article_images = doc.search("img").map {|img| 
              mimetype =  img[:src] ? "image/#{File.extname(img[:src]).sub('.', '')}" : nil
              {:href => img[:src], :mimetype => mimetype}
            }
            images.push *article_images
            title = doc.search("html/head/title").map(&:inner_text).first || "no title"
            idref = "item-#{article_file.gsub(/\D/, '')}"
            document[:spine_items] << {:idref => idref}
            article = {
              :file => article_file,
              :href => article_file,
              :title => title, 
              :short_title => title.shorten(60),
              :author => doc.search("html/head/meta[@name=author]").map{|n|n[:content]}.first,
              :description => doc.search("html/head/meta[@name=description]").map{|n|n[:content]}.first,
              :playorder => (playorder += 1),
              :idref => idref
            }
            manifest_items << {
              :href => article[:file],
              :media => "application/xhtml+xml",
              :idref => article[:idref]
            }
            article
        }
    }

    # Generate the section html
    out = Mustache.render section_template, s
    File.open(section_html_file, "w") {|f| f.puts out}
    s

  }

  document[:first_article] = sections[0][:articles][0]
  document['sections'] = sections


  document[:manifest_items] = manifest_items + images.map.with_index {|img, idx| 
    {
      :href => img[:href],
      :media => img[:mimetype],
      :idref => "img-%03d" % idx
    }
  } 
  document[:cover_mimetype] ||= "image/gif"
 
  opf = Mustache.render opf_template, document
  opf_path = base_dir.join "kindlerb.opf"
  File.open(opf_path, "w") {|f| f.puts opf}
  puts "Wrote #{target_dir}/kindlerb.opf"

  # NCX
  ncx = Mustache.render ncx_template, document
  ncx_path = base_dir.join "nav-contents.ncx"
  File.open(ncx_path, "w") {|f| f.puts ncx}
  puts "Wrote #{target_dir}/nav-contents.ncx"

  # contents
  contents = Mustache.render contents_template, document
  contents_path = base_dir.join "contents.html"
  File.open(contents_path, "w") {|f| f.puts contents}
  puts "Wrote #{target_dir}/contents.html"

  outfile = document['mobi_outfile'] 
  infile = base_dir.join "kindlerb.opf"
  puts "Writing #{outfile}"
  cmd = self.executable + "#{' -verbose' if verbose} -#{compression_method} -o #{outfile} #{infile} && echo 'Wrote MOBI to #{outfile}'"
  puts cmd
  system cmd

  # If the system call returns anything other than nil, the call was successful
  # Because Kindlegen completes build successfully with warnings
  successful = $?.exitstatus.nil? ? false : true
  if successful
    return true
  else
    return false
  end

end